This is really what I was hoping someone would come up with. I expected the barrage of mathematical proofs that prove, without a doubt the .9 repeating is indeed equal to 1. However, in a computer, you
can't repeat forever. This concept, although true in the word of theoretical mathematics, does not hold true in the world of computers. Attempt this simple script:
#!/usr/bin/perl -w
use strict;
my $num1 = .9999999999999999;
my $num2 = 1;
print "num1 = $num1\n";
print "num2 = $num2\n";
print "The numbers are equal" if $num2 == $num1;
print "The numbers are not equal" if $num2 != $num1;
You'll notice that these two numbers are indeed unequal. You're probably thinking big deal, obviously, $num1 doesn't repeat forever so they shouldn't be equal. Try the script again, but add a single 9 to the end of $num1. You should find the results more interesting. (I'm by no means a Perl guru and am not sure if this will work the same from one Perl installation to another. I am using Perl 5.6.1 on a Win32 platform.) In that case, the two values, even though $num1
still does not repeat forever, are considered equal.
What I had come up with, as a refutation to the argument, was that whether .9 repeating is equal to 1 is dependent upon your context. In a mathematical world, it is obviously true. Within a computer, however, it is an entirely different case.
- Sherlock
Update: Please note, I am not attempting to say that .9 repeating is
not equal to 1 simply because a computer can't represent .9 repeating. I was simply trying to point out that there is some level of discrepency between theoretical mathematics and the mathematics done in a computer/calculator. I was really just hoping this post would cause a few of you (and myself) to think about how you use floating point numbers and make you more aware of the rounding errors that can take place.
Skepticism is the source of knowledge as much as knowledge is the source of skepticism.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.