morgon has asked for the wisdom of the Perl Monks concerning the following question:
could someone please explain this behaviour of the following snippet to me?
This prints on my 5.18.1 on Debian:for my $i (1..20) { print "$i: "; print (1-1/2000)**$i; print " - "; print (1-1/2000)**int($i); print " - "; print "" . (1-1/2000)**$i; print "\n"; }
So for some reason I get the correct numerical value of the expression (1-1/2000)**$i only when I force stringification...1: 0.9995 - 0.9995 - 0.9995 2: 0.9995 - 0.9995 - 0.99900025 3: 0.9995 - 0.9995 - 0.998500749875 4: 0.9995 - 0.9995 - 0.998001499500063 5: 0.9995 - 0.9995 - 0.997502498750313 6: 0.9995 - 0.9995 - 0.997003747500938 7: 0.9995 - 0.9995 - 0.996505245627187 8: 0.9995 - 0.9995 - 0.996006993004374 9: 0.9995 - 0.9995 - 0.995508989507872 10: 0.9995 - 0.9995 - 0.995011235013118 11: 0.9995 - 0.9995 - 0.994513729395611 12: 0.9995 - 0.9995 - 0.994016472530913 13: 0.9995 - 0.9995 - 0.993519464294648 14: 0.9995 - 0.9995 - 0.993022704562501 15: 0.9995 - 0.9995 - 0.99252619321022 16: 0.9995 - 0.9995 - 0.992029930113615 17: 0.9995 - 0.9995 - 0.991533915148558 18: 0.9995 - 0.9995 - 0.991038148190984 19: 0.9995 - 0.9995 - 0.990542629116888 20: 0.9995 - 0.9995 - 0.99004735780233
Why is that?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: strange arithmetic
by BrowserUk (Patriarch) on Jan 09, 2015 at 19:57 UTC | |
|
Re: strange arithmetic
by kennethk (Abbot) on Jan 09, 2015 at 20:42 UTC | |
by morgon (Priest) on Jan 09, 2015 at 21:37 UTC | |
by RonW (Parson) on Jan 09, 2015 at 22:44 UTC | |
|
Re: strange arithmetic
by nlwhittle (Beadle) on Jan 09, 2015 at 19:30 UTC | |
by AnomalousMonk (Archbishop) on Jan 09, 2015 at 21:23 UTC | |
|
Re: strange arithmetic
by hippo (Archbishop) on Jan 10, 2015 at 12:53 UTC | |
|
Re: strange arithmetic
by shmem (Chancellor) on Jan 10, 2015 at 19:32 UTC |