Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: While loop with addition goes weird

by jimpudar (Pilgrim)
on Apr 08, 2018 at 16:57 UTC ( #1212544=note: print w/replies, xml ) Need Help??


in reply to While loop with addition goes weird

Hello chiel,

You should definitely read the pages LanX linked to. The second especially is required reading for anyone who wants to write a computer program.

However, I did want to point out you can use the bignum pragma to get Perl 5 to do what you want:

$ perl -e ' use warnings; use bignum; my $size = 0.01; my $step_size = 0.01; while($size < 5){ $size = $size + $step_size; print $size."\n"; } print "FINAL: ".$size."\n";' ... 4.94 4.95 4.96 4.97 4.98 4.99 5 FINAL: 5

Best,

Jim

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1212544]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2020-10-20 12:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (210 votes). Check out past polls.

    Notices?