This week's book giveaway is in the Artificial Intelligence forum.
We're giving away four copies of Pragmatic AI and have Noah Gift on-line!
See this thread for details.
Win a copy of Pragmatic AI this week in the Artificial Intelligence forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

comparision between double and long  RSS feed

 
Ranch Hand
Posts: 445
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why 1234567891.00000001 == (long)1234567891.00000001 returns true even though they are fals
but 123456789.00000001 == (long)123456789.00000001 returns true..

Also,
How do i know whether one number is pure decimal or number.
What I mean pure decimal number is after point(.) if it has 00001 or some other number is pure decimal. And I want to consider 1000.000 (all 0 after point) as long. How do I make that difference without writing some custom function. Is that possible?
 
author
Sheriff
Posts: 23571
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

why 1234567891.00000001 == (long)1234567891.00000001 returns true even though they are fals
but 123456789.00000001 == (long)123456789.00000001 returns true..



It's an issue with precision. Floating point values are not that accurate in terms of precision.

Actually, you didn't even need to cast it to a long value...

"123456789.00000001 == 123456789.0" should return true.

Henry
[ July 21, 2006: Message edited by: Henry Wong ]
 
Rizwan Mohammad
Ranch Hand
Posts: 445
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

123456789.00000001 == 123456789.0" should return true.



But I want them as false
123456789.00000001 == 123456789.0000000 should return me false. How do I do that?
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about BigDecimal?

 
Rizwan Mohammad
Ranch Hand
Posts: 445
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you remove string and give numbers as double instead of string it says equal(compare method returns 0)
BigDecimal bd1 = new BigDecimal(1234567891.00000001);
BigDecimal bd2 = new BigDecimal(1234567891);
bd1.compareTo(bd2) returns 0. and prints BD1 == BD2
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you need to understand, Rizwan my comrade, is that this number 123456789.00000001 cannot be expressed as double, simply because double cannot hold that small precision number. Then it simply discards it the extra precision.

So, if you create the BigDecimal using the same double value that is causing you trouble, you will get the same result back again.

Use Strings to hold the decimal value and then use the compareTo method to determine if one number is bigger, smaller or equal to the other.

Be careful with the equals() method. Read the javadoc API about it.
[ July 21, 2006: Message edited by: Edwin Dalorzo ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!