Reg- Floating point variables
Reg- Floating point variables
am 12.11.2007 15:13:47 von 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
Re: Reg- Floating point variables
am 12.11.2007 15:58:01 von smallpond
On Nov 12, 9:13 am, Nithy wrote:
> 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
Re: Reg- Floating point variables
am 12.11.2007 16:23:45 von jurgenex
Nithy wrote:
> 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
Re: Reg- Floating point variables
am 12.11.2007 16:30:55 von 1usa
Nithy wrote in news:1194876827.609769.271960
@z24g2000prh.googlegroups.com:
> #!/usr/bin/perl
> #This program increments the value from x.1 to x.9
for my $i ( 1 .. 9 ) {
$x += $i/10;
}
> Why is it so?
perldoc -q 999
Sinan
--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)
clpmisc guidelines:
Re: Reg- Floating point variables
am 12.11.2007 17:39:49 von Sherm Pendley
Nithy writes:
> 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?
This is a FAQ. Have a look at:
perldoc -q 999
> Please let me know your suggestion.
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."
sherm--
--
WV News, Blogging, and Discussion: http://wv-www.com
Cocoa programming in Perl: http://camelbones.sourceforge.net
Re: Reg- Floating point variables
am 13.11.2007 06:18:08 von Nithy
On Nov 12, 9:39 pm, Sherman Pendley wrote:
> Nithy writes:
> > 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?
>
> This is a FAQ. Have a look at:
>
> perldoc -q 999
>
> > Please let me know your suggestion.
>
> 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."
>
>
>
> sherm--
>
> --
> WV News, Blogging, and Discussion:http://wv-www.com
> Cocoa programming in Perl:http://camelbones.sourceforge.net
Thanks Sherm..
Re: Reg- Floating point variables
am 13.11.2007 06:18:44 von Nithy
On Nov 12, 8:30 pm, "A. Sinan Unur" <1...@llenroc.ude.invalid> wrote:
> Nithy wrote in news:1194876827.609769.271960
> @z24g2000prh.googlegroups.com:
>
> > #!/usr/bin/perl
> > #This program increments the value from x.1 to x.9
>
> for my $i ( 1 .. 9 ) {
> $x += $i/10;
>
> }
> > Why is it so?
>
> perldoc -q 999
>
> Sinan
>
> --
> A. Sinan Unur <1...@llenroc.ude.invalid>
> (remove .invalid and reverse each component for email address)
> clpmisc guidelines:
Thanks Sinan..
Re: Reg- Floating point variables
am 13.11.2007 06:19:13 von Nithy
On Nov 12, 8:23 pm, "Jürgen Exner" wrote:
> Nithy wrote:
> > 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 =3D 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 po=
int
> numbers has it quirks in Perl, too, just like in any other standard
> programming language.
>
> jue
Thanks Jue..
Re: Reg- Floating point variables
am 13.11.2007 06:20:18 von Nithy
On Nov 12, 7:58 pm, smallpond wrote:
> On Nov 12, 9:13 am, Nithy wrote:
>
>
>
> > 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
Thanks for your prompt response Smallpond.