• Post Reply Bookmark Topic Watch Topic
  • New Topic

Math.PI comparision  RSS feed

 
vu lee
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any idea why this condition fail due to missing precision


Thanks
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't know what you mean; the code will compile, and the comparison will succeed. Do you observe otherwise? Tell us more details.
 
Campbell Ritchie
Marshal
Posts: 55759
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happens depends on what is in between.
 
vu lee
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In debug mode, I saw the value of one variable was 3.14159265358979323846 but the other was 3.1415926535897. It didn't happen all the time though.
 
Campbell Ritchie
Marshal
Posts: 55759
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Those figures from debug mode might not be precise. Is there any code which does arithmetic with pi in that block?
 
vu lee
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Neither synchronized nor Lock, but the comparision occured in different thread. I'm in training, will post my code.

Thanks


 
Rob Spoor
Sheriff
Posts: 21092
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should hardly ever use == or != with flout or double comparison, just because of the missing precision. The following is better:
Another option is using Double/Float.compare:
 
Campbell Ritchie
Marshal
Posts: 55759
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried that last night with a little arithmetic in between, and in all cases the two values appeared to be identical; it didn't print ???

But there might be something in between the lines shown which alters the value of pi; please tell us the details of what you are doing with pi in the interim.

And that is a splendid spelling error, Rob
 
vu lee
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob. The Double.compare() has resolved my issues.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!