note
davido
<p><b>It's only shocking because</b> you grew up with base-ten and are so accustomed to seeing the problem that you don't notice it until it turns up in another base system.</p>
<p><b>...and</b> because you haven't read [doc://perlfaq4#Why_am_I_getting_long_decimals_(eg,_19.9499999999999)_instead_of_the_numbers_I_should_be_getting_(eg,_19.95)?|perlfaq4: Why am I getting long decimals.....], and [doc://perlfaq4#Why_is_int()_broken?|perlfaq4: Why is int() broken?]</p>
<p><b>Some explanation:</b> [wikipedia://rational_number|Rational numbers] do not always have a [wikipedia://Floating_point#Representable_numbers.2C_conversion_and_rounding|terminating expansion] for a given base. In base ten, we are so accustomed to seeing 1/3rd expressed as .33 (maybe with more significant digits) that we hardly even think about it. But the fact is that .33333333333 is not equal to 1/3rd. You cannot represent 1/3rd in a finite number of digits using a decimal (base ten) representation. You could say that 1/3rd has an [wikipedia://Repeating_decimal#Repeating_decimals_as_an_infinite_series|infinite expansion], or a non-terminating expansion in base ten. In base ten, any rational number that can be represented as <c>k/((2^n)*(5^m))</c> would be a number that has a terminating expansion. 4.39 is <c>439/((2^2)*(5^2))</c>, which fits that formula nicely, and so we get a terminating expansion from 439/100 to 4.39.</p>
<p>In base two (binary), the same applies, but with a different formula; some numbers cannot be represented in a finite number of binary digits. It turns out that in base two, any rational number with a denominator that is a power of two <em>will have</em> a terminal expansion. But all other rational numbers will not. So n/2, n/4, n/8, n/16, n/32, n/64... those could all be represented in binary format in a finite number of binary digits. Or to put it in terms normalized to the previous formula, <c>k/2^n</c> would represent a number that has a terminating expansion in base 2. But 4.39 is 439/100's, which is a rational number that can <em>not</em> be represented in binary with a finite number of digits.</p>
<p>That being the case, computer does what we do when we see 1/3rd in base ten; it approximates. It stores the number as some binary value that is as close as possible to 4.39 in binary format. Just as .33 is slightly less than 1/3rd, 4.39 stores as slightly less than 4.39.
</p>
<p>So the computer stores 4.39 as 4.38999999999999. What happens when you ask for 4.38999999999999 * 100? You get 438.99999999999. And when you ask for the integer value of 438.9999999999, you get 438.
</p>
<p>This is not a Perl problem. You would be equally shocked by the "imprecision" of Basic, Pascal, C, Modula-II, C++, and just about every other language that doesn't attempt to hide the truth from you.</p>
<p><b>An illustration:</b> Try this little exercise:</p>
<c>
perl -MO=Deparse -e 'print 4.39 * 100'
perl -MO=Deparse -e 'print int( 4.39 * 100 )'
</c>
<p>The output should be something like:</p>
<c>
print 438.99999999999994;
</c>
<p>...and...</p>
<c>
print 438;
</c>
<p>So in the first example, the multiplication is indeed storing the number as something slightly less than 439, but because [doc://print] quietly rounds that to 439 you don't notice. However, [doc://int] wouldn't be doing a very good job if it quietly rounded when you asked for truncation (which is what int is supposed to do). So it's taking that 438.9999999999999994 and truncating the floating point portion, just like it's supposed to.</p>
<p><b>Other reading</b>:</p>
<ul><li>[http://brad.chalfan.net/binq/#term|Binary Expansions]</li>
<li>[http://download.oracle.com/docs/cd/E19957-01/806-3568/ncg_goldberg.html|What Every Computer Scientist Should Know About Floating-Point Arithmetic]</li>
</ul>
<div class="pmsig"><div class="pmsig-281137">
<br /><p>Dave</p>
</div></div>
932020
932020