Reg- Floating point variables

N

Nithy

Hi,
Can anyone help me in floating point round off error. I have the
code as shown below.

#!/usr/bin/perl
#This program increments the value from x.1 to x.9

$count = 0;
$out = 0;
while($out == 0)
{
print ("count = ",$count,"\n");
$count=$count + 0.1;
if ($count == 0.9)
{
$out=1;
}

If I run this code, it doesn't terminate when $count reach 0.9. Its
keep on increasing. And after 5.9 its showing 5.99999999999999 & so
on..
But If I limits it within 0.8(if $count = 0.1 - 0.7), it gives the
proper output. Why is it so?

Please let me know your suggestion.

Thanks in advance.

Regards,
Nithy
 
S

smallpond

Hi,
Can anyone help me in floating point round off error. I have the
code as shown below.

#!/usr/bin/perl
#This program increments the value from x.1 to x.9

$count = 0;
$out = 0;
while($out == 0)
{
print ("count = ",$count,"\n");
$count=$count + 0.1;
if ($count == 0.9)
{
$out=1;

}

If I run this code, it doesn't terminate when $count reach 0.9. Its
keep on increasing. And after 5.9 its showing 5.99999999999999 & so
on..
But If I limits it within 0.8(if $count = 0.1 - 0.7), it gives the
proper output. Why is it so?

Please let me know your suggestion.

Thanks in advance.

Regards,
Nithy



Never try to test for exactly equal to a floating point number.
It doesn't work. Floating point numbers are approximate.
Change your code to
if ($count >= 0.9)
--S
 
J

Jürgen Exner

Nithy said:
If I run this code, it doesn't terminate when $count reach 0.9. Its
keep on increasing. And after 5.9 its showing 5.99999999999999 & so
on..
But If I limits it within 0.8(if $count = 0.1 - 0.7), it gives the
proper output. Why is it so?

You must have missed "Basics of Computer Numerics":
Thou shalt not test for equal on floating point numbers

'perldoc -q 999' gives a very brief introduction of why using floating point
numbers has it quirks in Perl, too, just like in any other standard
programming language.

jue
 
N

Nithy

This is a FAQ. Have a look at:

perldoc -q 999


In a nutshell, some numbers cannot be exactly represented in binary, for
the same reason that numbers such as one-third cannot be exactly represented
in decimal.

For this reason, it's best to avoid direct equality tests on floating-point
numbers. For example, you could rewrite your test of $count to:

if ($count >= 0.9) { ... }

Another common technique is to measure the difference (aka delta) between a
float and a known value, and consider them equal if the delta is small enough:

if (abs(0.9 - $count) < 0.000001) { ... }

For more (much more) detail, have a look at "What every computer scientist
should know about floating-point arithmetic."

<http://docs.sun.com/source/806-3568/ncg_goldberg.html>

sherm--

Thanks Sherm..
 
N

Nithy

You must have missed "Basics of Computer Numerics":
Thou shalt not test for equal on floating point numbers

'perldoc -q 999' gives a very brief introduction of why using floating point
numbers has it quirks in Perl, too, just like in any other standard
programming language.

jue
Thanks Jue..
 
N

Nithy

Never try to test for exactly equal to a floating point number.
It doesn't work. Floating point numbers are approximate.
Change your code to
if ($count >= 0.9)
--S

Thanks for your prompt response Smallpond.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top