in reply to int() function
15/100 is periodic number in binary (0.00 1001 1001 1001 ...) just like 1/3 is periodic in decimal (0.333...) . As such, it can't accurately be stored as a floating point number since that would take infinite storage. The number you think you have isn't quite what you think it is.
(Upd: syphilis points out %.100g naturally remove the trailing zeros.)$ perl -e'CORE::say sprintf("%.100f", 0.15) =~ s/0+\z//r' 0.1499999999999999944488848768742172978818416595458984375 $ perl -e'CORE::say sprintf("%.100f", (1.15*170)+0.50) =~ s/0+\z//r' 195.999999999999971578290569595992565155029296875
See What Every Programmer Should Know About Floating-Point Arithmetic. It covers how to handle this.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: int() function
by syphilis (Archbishop) on Oct 27, 2020 at 11:09 UTC | |
by jszinger (Scribe) on Oct 27, 2020 at 15:43 UTC | |
by syphilis (Archbishop) on Oct 28, 2020 at 00:16 UTC | |
by ikegami (Patriarch) on Oct 30, 2020 at 11:58 UTC |
In Section
Seekers of Perl Wisdom