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.
$ 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
( Upd: syphilis points out %.100g naturally remove the trailing zeros.)
See What Every Programmer Should Know About Floating-Point Arithmetic. It covers how to handle this.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|