Problems with floating-point operations
Author Message
Problems with floating-point operations

I have a specific problem with the division by 100. A short
example will explain:

-----------------------------
#!/usr/local/bin/perl

for(100..200)
{
print "Value: \t\$_, \t".(\$_/100)."\n";

Quote:
}

-----------------------------

Now an excerpt of the result shows:
Value:  178,    1.78
Value:  179,    1.79
Value:  180,    1.8
Value:  181,    1.8100000000000001
Value:  182,    1.8200000000000001
Value:  183,    1.8300000000000001
Value:  184,    1.8400000000000001
Value:  185,    1.8500000000000001
Value:  186,    1.8600000000000001
Value:  187,    1.8700000000000001
Value:  188,    1.8799999999999999
Value:  189,    1.8899999999999999
Value:  190,    1.8999999999999999
Value:  191,    1.9099999999999999

Now my question:

How can I get perl to an exact result e.g: 183/100= 1.83 ?????

-wjs
--
Werner J. Schoenfeldinger          Department  for  Applied Computer Science
Univ. of Economics and BA, Vienna/Austria

Fri, 14 Mar 1997 03:29:10 GMT
Problems with floating-point operations

Quote:
>I have a specific problem with the division by 100. A short
>example will explain:
>Value:  181,    1.8100000000000001

[.. etc ..]

Quote:
>Now my question:
>How can I get perl to an exact result e.g: 183/100= 1.83 ?????

Long answer: decimal fractions are not exactly representable by the
floating point model which most of today's computers use, which
is (excuse the TeX notation ;-)

fp:= s 2^{e} sum_{i=0}^{n} d_{i} 2^{-i}

where s is either -1 or +1, e is the exponent, and d_0 .. d_n are
the digits of the number.

You can either

- round to the next number, e.g. with printf("%10.2f")

- implement a BCD - kind of arithmetic on top of the "bigint" package

You'll probably want to rewrite the bigint package to take advantage
--

The joy of engineering is to find a straight line on a double
logarithmic diagram.

Fri, 14 Mar 1997 03:53:59 GMT
Problems with floating-point operations
Quote:

>>How can I get perl to an exact result e.g: 183/100= 1.83 ?????

--- in perl4.  At least the default printing
is fixed in perl5, though the reliance on underlying binary
representation is still there.

\$ perl5 -le 'print 183/100;'
1.83
\$

--

Dept. of Physics
Lafayette College, Easton PA 18042

Fri, 14 Mar 1997 23:40:26 GMT
Problems with floating-point operations

Quote:
>I have a specific problem with the division by 100. A short
>example will explain:

>-----------------------------
>#!/usr/local/bin/perl

>for(100..200)
>{
>    print "Value: \t\$_, \t".(\$_/100)."\n";
>}
>-----------------------------

>Now an excerpt of the result shows:
>Value:  178,    1.78
>Value:  179,    1.79
>Value:  180,    1.8
>Value:  181,    1.8100000000000001
>Value:  182,    1.8200000000000001
>Value:  183,    1.8300000000000001
>Value:  184,    1.8400000000000001
>Value:  185,    1.8500000000000001
>Value:  186,    1.8600000000000001
>Value:  187,    1.8700000000000001
>Value:  188,    1.8799999999999999
>Value:  189,    1.8899999999999999
>Value:  190,    1.8999999999999999
>Value:  191,    1.9099999999999999

>Now my question:

>How can I get perl to an exact result e.g: 183/100= 1.83 ?????

Easy, just change the first line to read

#!/usr/local/bin/perl5

:-)

Value:  178,    1.78
Value:  179,    1.79
Value:  180,    1.8
Value:  181,    1.81
Value:  182,    1.82
Value:  183,    1.83
Value:  184,    1.84
Value:  185,    1.85
Value:  186,    1.86
Value:  187,    1.87
Value:  188,    1.88
Value:  189,    1.89
Value:  190,    1.9
Value:  191,    1.91

Quote:
>Werner J. Schoenfeldinger          Department  for  Applied Computer Science

Regards,
Tim Bunce.

Sat, 15 Mar 1997 01:56:44 GMT

 Page 1 of 1 [ 4 post ]

Relevant Pages