|
The Java "float" data type is based on a specific binary representation (IEEE 745 I think). But saying that they are actually decimal is slightly misleading... you see them represented as decimal, because Java converts them to human-readable form before displaying them. You can run into problems if you don't distinguish between internal and external representations of objects. And in the case of "float" variables, thinking of dividing by 100 as a two digit shift is the external-representation view, which may not be precisely mirrored in the internal representation because of possible binary round-off. If you write a Java program that starts with "float x = 0" and then executes "x = x + 0.01" in a loop, you'll see round-off happening very quickly. In fact in my test, after only 6 iterations x has become 0.059999995. This wouldn't happen in a system of decimal numbers. (And in fact 8.23999999999993753 is a rational number, because it can be expressed as a fraction, but the mathematical definition of "rational" is an irrelevance I should not have bothered with in my earlier post.) -----Original Message----- From: James Donkin [mailto:James@DuMaurier.co.uk] Sent: March 3, 2001 01:12 To: JAVA400-L@midrange.com Subject: Re: Decimal rounding of a float data type in Java. <snip> Java data types are decimal not binary. The underlying implementation might be binary but the float is a decimal number - dividing it by one hundred is a two digit shift. > And by the way, > 323/34 is no more irrational than 422/100. Both are rational numbers. true - but is 8.23999999999993753 ? +--- | 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.