Do you know where your variables are? PerlMonks

### Comment on

 Need Help??

This is the result of storing floating point numbers in base two. Consider the fraction 1/3rd. Expressed in decimal, that's 0.33 (repeating forever). Add 0.33 three times, and you get 0.99, not one. You can extend to as many decimal places as you like, you'll never get .3333333333333333333333333 * 3 to add up to 1.0. Well, in base 2, the fraction 1/10th has a non-terminating expansion. So 0.1 has to be stored as 0.09999999999999999 or something similar, and when you add that up ten times you won't get to one.

It's only shocking because 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.

Of course this is not unique to Perl. Consider the following C++ code:

```#include <iostream>
#include <iomanip>

int main () {

double n = 0.0;
for( int i = 0; i != 10; i++ ) {
n += 0.1;
std::cout << std::setprecision(16) << std::fixed << n << std::endl
+;
}

if( n != 1.0 )
std::cout << "Whoops, " << n << " isn't equal to 1.0." << std::end
+l;

return 0;
}

...outputs...

```0.1000000000000000
0.2000000000000000
0.3000000000000000
0.4000000000000000
0.5000000000000000
0.6000000000000000
0.7000000000000000
0.7999999999999999
0.8999999999999999
0.9999999999999999
Whoops, 0.9999999999999999 isn't equal to 1.0.

Dave

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

• 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.
• Posts may use any of the Perl Monks Approved HTML tags:
a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, 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, 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?

Create A New User
Chatterbox?
 [atcroft]: Lady_Aleena: I found the L syntax, which describes linking to a perl manual page, and L to link to a web page. The reason I asked was because I was on search.cpan.org looking at a module, [atcroft]: and it referred to several other modules, but the links went to metacpan.org. I found that a bit of a surprise, and it got me wondering if there was a "standard" way to link to other modules in the documentation on CPAN. [Lady_Aleena]: atcroft. maybe look at raw PODs. There is also L and some might be linking directly to meta::cpan. [Lady_Aleena]: Oops, didn't read the L right.

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2017-05-27 03:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
My favorite model of computation is ...

Results (192 votes). Check out past polls.