• 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

comparision between double and long

 
Ranch Hand
Posts: 445
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
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

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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What about BigDecimal?

 
Rizwan Mohammad
Ranch Hand
Posts: 445
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic