newbie question

S

soulshriek

hey

i just started learning perl 2 days ago.

and i have a little question and i can't find the answer on the net.

i tested this little script

#!/usr/bin/perl

use warnings;
use strict;

my $var = 0;

until ($var == 1.2) #
{
print "$var\n";
$var += 0.1;
}

and it does what it's surposed to do

but when i change

until ($var == 1.2) into until ($var == 1.1)

something strange happens

my perl version is 5.8.3 btw

could someone please explain this to me, i like to know why stuff happens

because everything happens for a reason

greetz

Soulshriek
 
J

Jürgen Exner

soulshriek said:
i just started learning perl 2 days ago.
and i have a little question and i can't find the answer on the net.

You were looking at the wrong place. Your problem has nothing to do with
Perl.
i tested this little script [...]
until ($var == 1.2) #

You must have missed the first day of the "Basics of Computer Numerics"
class.

==> Thou shalt not use equality tests for floating point numbers

Futher details see e.g. "perldoc -q 999" and google.com
Hint: instead of comparing for equality you can test if the numbers are
within an epsilon distance of each other.

jue
 
C

Chris Mattern

soulshriek said:
hey

i just started learning perl 2 days ago.

and i have a little question and i can't find the answer on the net.

i tested this little script

#!/usr/bin/perl

use warnings;
use strict;

my $var = 0;

until ($var == 1.2) #
{
print "$var\n";
$var += 0.1;
}

and it does what it's surposed to do

By the grace of God and great good luck. *Never* test a floating-point
number for equality; floating-point numbers are approximations, and
may not line up equal when they should
but when i change

until ($var == 1.2) into until ($var == 1.1)

something strange happens

You got an infinite loop, right?
my perl version is 5.8.3 btw

could someone please explain this to me, i like to know why stuff happens

Comparing floating-point numbers for equality does not work
reliably; don't do it. In the above example, use "until ($var >= 1.2)"
(or 1.1). Then your program will always come to halt. In fact,
in the above example, you could use "until ($var >=1.15)" (or
1.05). Now, not only will your program always come to a halt, it
will always come to a halt where you expect it to. Always remember
to make allowances for the imprecision of float-point.
because everything happens for a reason

greetz

Soulshriek

--
Christopher Mattern

"Which one you figure tracked us?"
"The ugly one, sir."
"...Could you be more specific?"
 
A

Anno Siegel

Jürgen Exner said:
soulshriek said:
i just started learning perl 2 days ago.
and i have a little question and i can't find the answer on the net.

You were looking at the wrong place. Your problem has nothing to do with
Perl.
i tested this little script [...]
until ($var == 1.2) #

You must have missed the first day of the "Basics of Computer Numerics"
class.

==> Thou shalt not use equality tests for floating point numbers

Futher details see e.g. "perldoc -q 999" and google.com
Hint: instead of comparing for equality you can test if the numbers are
within an epsilon distance of each other.

To end a loop like that, I'd use

until ( $var >= 1.1 )

That way you don't have to use and (the hard part) decide about an epsilon.
In fact, I tend to control loops with inequalities rather than equalities
even with integers, just in case an index is somehow skipped.

Anno
 
T

Tad McClellan

soulshriek said:
i just started learning perl 2 days ago.


Have you seen the Posting Guidelines that are posted here frequently?

but when i change

until ($var == 1.2) into until ($var == 1.1)

something strange happens
^^^^^^^^^^^^^^^^^

That information does not help us solve your problem.

A careful description of the symptoms is required if you expect
a reasonably accurate diagnosis.

What does "something strange" mean when you say it?

What did you observe that was "strange"?

Don't make us guess...

could someone please explain this to me, i like to know why stuff happens
because everything happens for a reason


To see what is happening, print the output like this instead:

printf "%30.20f\n", $var;
 

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

Similar Threads

newbie question 3
Newbie question 2
Regex basic question 2
Iframe link overlapping text 4
Newbie Question 9
Test::More with suexec 0
How to minimize server load when program is run 5
BEGIN, eval and $^S 4

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top