• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Is this a precision error or is there something wrong with my code?

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am teaching myself Java and am trying to write a function that will determine all of the perfect squares between 1 and 100 but am running into a problem...

Here's my code:



and here is the output:

run:
0.0
1.0 is a perfect square.
0.0
4.0 is a perfect square.
0.0
9.0 is a perfect square.
0.0
11.0 is a perfect square.
0.0
14.0 is a perfect square.
0.0
16.0 is a perfect square.
0.0
17.0 is a perfect square.
0.0
21.0 is a perfect square.
0.0
22.0 is a perfect square.
0.0
25.0 is a perfect square.
0.0
27.0 is a perfect square.
0.0
30.0 is a perfect square.
0.0
33.0 is a perfect square.
0.0
34.0 is a perfect square.
0.0
35.0 is a perfect square.
0.0
36.0 is a perfect square.
0.0
39.0 is a perfect square.
0.0
41.0 is a perfect square.
0.0
42.0 is a perfect square.
0.0
44.0 is a perfect square.
0.0
46.0 is a perfect square.
0.0
47.0 is a perfect square.
0.0
49.0 is a perfect square.
0.0
53.0 is a perfect square.
0.0
54.0 is a perfect square.
0.0
55.0 is a perfect square.
0.0
56.0 is a perfect square.
0.0
57.0 is a perfect square.
0.0
62.0 is a perfect square.
0.0
64.0 is a perfect square.
0.0
67.0 is a perfect square.
0.0
68.0 is a perfect square.
0.0
69.0 is a perfect square.
0.0
70.0 is a perfect square.
0.0
71.0 is a perfect square.
0.0
74.0 is a perfect square.
0.0
79.0 is a perfect square.
0.0
81.0 is a perfect square.
0.0
83.0 is a perfect square.
0.0
84.0 is a perfect square.
0.0
85.0 is a perfect square.
0.0
86.0 is a perfect square.
0.0
88.0 is a perfect square.
0.0
90.0 is a perfect square.
0.0
91.0 is a perfect square.
0.0
93.0 is a perfect square.
0.0
98.0 is a perfect square.
0.0
99.0 is a perfect square.
0.0
100.0 is a perfect square.

There are 49 perfect squares between 1 and 100.
BUILD SUCCESSFUL (total time: 6 seconds)

Which is clearly wrong. Is there something wrong with my code or is this due to inherent imprecision in the double type or the Math.sqrt function?

Any help on this would be greatly appreciated.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Van Breemen wrote:
There are 49 perfect squares between 1 and 100.
BUILD SUCCESSFUL (total time: 6 seconds)

Which is clearly wrong. Is there something wrong with my code or is this due to inherent imprecision in the double type or the Math.sqrt function?



From a short review ... Yes, it is due to the imprecision of floating point numbers. If floating point was completely precise, then your application should be reporting 100 perfect squares between 1 and 100.

Having said that, if you also think that 100 perfect squares is the wrong answer, then that part is due to your code ...

Henry
 
David Van Breemen
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
lol true that, with perfect precision then would always evaluate to zero. I know I could use Math.pow() for the correct answer, but I wanted to see if it could be done based on the square root rounding error example in my book. I guess not. Thanks for the answer.
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Avoid Math#pow for squares as far as possible. Use x * x instead. Probably quicker and more precise, until you suffer an overflow. And integer arithmetic does not suffer the sort of imprecision you are seeing.
 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I always thought a perfect square was one in which the square root of that number was an integer.

I might suggest you turn it around. Rather than starting from the square and taking the square root. If you start from the root you can avoid a whole lot of calculations and checking.



 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stefan Evans wrote:I always thought a perfect square was one in which the square root of that number was an integer.



It is. This is why I mentioned that there is also something wrong with the code ...

Henry
reply
    Bookmark Topic Watch Topic
  • New Topic