Results 1 to 4 of 4
- 03-25-2010, 08:41 PM #1
Member
- Join Date
- Feb 2010
- Posts
- 20
- Rep Power
- 0
- 03-25-2010, 08:50 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
Imagine the following 'game': you have the numbers 1/2, 1/4, 1/8 ... 1/2^52. Given an arbitrary number d select the numbers (at most once) and add their values and try to make that sum equal to d; if you want you can multiply or divide your sum by two as many times as you want. That's how IEEE754 floating point numbers work. You can't make the number 0.2 with that 'game'.
An analogy in decimal: you can't make the number 1/3 given a finit number of decimals. That's why you get that 'funny' result. The cure: for representations of those floating point numbers that 'look better' have a look at the DecimalFormat class.
kind regards,
Jos
- 03-26-2010, 09:32 AM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
And if you need accurate decimal representations (say for finance stuff) use BigDecimal.
- 03-26-2010, 10:40 AM #4
Similar Threads
-
Weird output
By gandalf5166 in forum New To JavaReplies: 2Last Post: 02-28-2010, 09:17 PM -
Seriously weird output
By gandalf5166 in forum Java AppletsReplies: 4Last Post: 02-27-2010, 04:16 AM -
Check if double is double
By marshalthrone in forum New To JavaReplies: 8Last Post: 09-30-2009, 02:51 PM -
non-static method add(double,double) cannot be referenced from a static context
By cravi85 in forum Java SoftwareReplies: 5Last Post: 03-21-2009, 09:32 PM -
Double.valueOf() vs Double.parseDouble()
By greenbean in forum New To JavaReplies: 10Last Post: 01-12-2009, 08:39 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks