• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
David Van Breemen
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • 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.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • 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.
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!