|
I expect you didn't notice the original source of this thread, where the questioner was asking why he or she got something like 8.23999999999993755 instead of 8.24, after a simple calculation. This IS how floating point numbers work, regardless of your innumerate denial. Floating point numbers work in binary, not in decimal. So dividing by 100 is not "just a two-digit shift" in floating point, that would be dividing by 4. And by the way, 323/34 is no more irrational than 422/100. Both are rational numbers. -----Original Message----- From: James Donkin [mailto:James@DuMaurier.co.uk] Sent: March 2, 2001 01:25 To: JAVA400-L@midrange.com Subject: Re: Decimal rounding of a float data type in Java. > Well, not exactly. Your "average" doesn't become 4.22, it becomes something > that's extremely close to 4.22. Floating point numbers can't express *any* > fraction exactly unless it's a power of 0.5. So don't be surprised if you > do "System.out.println(average)" and you find something like > "4.2200000000033775" appearing. e.g. average = 422 / 100; This will always give 4.22. The time you get problems is with 323 / 34 or some other fraction which generates an irrational number. Saying that float average = 422 / 100; will give 4.22000032 is like saying that float average = 4.22; will leave average = 4.2200042. Dividing by 100 just generates a two digit shift. +--- | This is the JAVA/400 Mailing List! | To submit a new message, send your mail to JAVA400-L@midrange.com. | To subscribe to this list send email to JAVA400-L-SUB@midrange.com. | To unsubscribe from this list send email to JAVA400-L-UNSUB@midrange.com. | Questions should be directed to the list owner: joe@zappie.net +---
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.