#### Do you have a question? Post it now! No Registration Necessary. Now with pictures!

## Re: Simple loop error

You would need something like

for (my $i = 0; $i <= 1; $i = sprintf("%.2f", $i + 0.01)) {

print "$i\n";

}

Since you should have "use strict" turned on in all of your Perl programs!

Necessary for what?

Necessary for whatever it is that you are doing with those numbers?

Only you can answer that.

Necessary for having the loop terminate after getting to one?

No, it is not necessary.

The standard way of dealing with an equality test of floating point

numbers is to instead test whether the number is within a (small)

range of the number you want to test for equality.

Say within one-thousandth of the number you want to test for, so:

for (my $i = 0; $i <= 1.001; $i += 0.01) {

print "$i\n";

}

--

Tad McClellan

email: perl -le "print scalar reverse qq/moc.liamg0cm.j.dat/"

## Re: Simple loop error

Wrong. It goes from 0.8100000000000004973799150320701301097869873046875

to 0.8200000000000005062616992290713824331760406494140625

Change this to

printf("%.60f\n", $i);

to see what's going on.

No. Use something like this:

for (my $ii = 0; $ii <= 100; $ii++)) {

my $i = $ii / 100;

print "$i\n";

}

or more idiomatically:

for my $ii (0 .. 100) {

my $i = $ii / 100;

print "$i\n";

}

More generally, if you want to want to go from $begin to $end in $steps

steps, use:

for my $i (0 .. $steps) {

my $value = $begin + ($end - $begin) * $i / $steps;

...

}

As a general rule, with floating point arithmetic, try to avoid repeated

addition and subtraction.

hp

## Re: Simple loop error

I'd like to call this an ancient myth instead of ancient knowledge. It's

complete and utter bullshit. If you need to know whether two numbers are

equal, use ==. If you just need to know whether two numbers are similar,

compare the difference to some epsilon. That doesn't have anything to do

with whether you are using IEEE Standard 754 floating point numbers or

not. It doesn't even have anything to do with whether you are using

floating point - the same is true for fixed point or even integers. It's

just that FP numbers are typically used for values for which the exact

value is not only irrelevant but inherently unknowable, so a comparison

for equality doesn't make sense (or the other way around: If you need to

represent exact values, FP is probably the wrong type).

And it's irrelevant in this case because he

***didn't***compare for

equality.

hp

## Re: Simple loop error

at 01:20 PM, bks@panix.com (Bradley K. Sherman) said:

No; "<=" is not an equality operator.

Oddly enough, you quoted the relevant text from the original message,

which clearly reads "<=".

--

Shmuel (Seymour J.) Metz, SysProg and JOAT <http://patriot.net/~shmuel

Unsolicited bulk E-mail subject to legal action. I reserve the

right to publicly post or ridicule any abusive E-mail. Reply to

domain Patriot dot net user shmuel+news to contact me. Do not

reply to spamtrap@library.lspace.org

## Re: Simple loop error

at 11:27 PM, bks@panix.com (Bradley K. Sherman) said:

That much is correct.

Which part of 'No; "<=" is not an equality operator.' don't you

understand?

--

Shmuel (Seymour J.) Metz, SysProg and JOAT <http://patriot.net/~shmuel

Unsolicited bulk E-mail subject to legal action. I reserve the

right to publicly post or ridicule any abusive E-mail. Reply to

domain Patriot dot net user shmuel+news to contact me. Do not

reply to spamtrap@library.lspace.org

## Re: Simple loop error

at 08:26 PM, bks@panix.com (Bradley K. Sherman) said:

ObHaddocksEyes Names are labels, not descriptions. Just because the string

"<=" has the substring "=" doesn't make it the name of an equality

operator or an assignment operator.

Which is likewise not an equality operator.

In the first case there is only one operator and it is

***not***an equality

operator any more than it is an assignment operator.

--

Shmuel (Seymour J.) Metz, SysProg and JOAT <http://patriot.net/~shmuel

Unsolicited bulk E-mail subject to legal action. I reserve the

right to publicly post or ridicule any abusive E-mail. Reply to

domain Patriot dot net user shmuel+news to contact me. Do not

reply to spamtrap@library.lspace.org

## Re: Simple loop error

wrote:

Does not.

At least not in a way that matters for floating point.

Let's get down to machine level: say we have a constant float $a, and a

value $b which is the result of a lengthy calculation. $a is exact,

because we wrote it down as a constant in the source code, and took good

care that it is a number that can actually be represented in binary

floating point without any error. $a=1.0 will do fine, but it does not

matter. Being floating point numbers, $b will most definitely not be

exact, but will shift around the theoretical target value by some margin

of error.

Let us assume a benign algorithm, constructed by a wise mathemagician,

which guarantees that $b will always end up in a well known interval

around $a. Let us assume that this interval is simply $a itself surrounded

by the 4 next larger or smaller numbers that can be represented as IEEE

floating point numbers (this assumption is conceptually well defined since

IEEE FP numbers are rather strict in this respect, i.e. every number that

can actually be represented in IEEE FP has exactly one, and not several,

legal representations). Let's also assume that each of those 9 results is

as likely as the others. Then we can write little diagrams where "*" means

"one of the close neighbours" and "!" means "exactly $a as represented in

IEEE FP". Underneath, we write "1" or "0" depending on the actual result

of the operator.

We do that for each of the operators "==", "<=" and "<":

For $a==$b, expected result TRUE:

*** ***

*** ***!

*** ***

*** ***

0 0 0 0 1 0 0 0 0

*=> in 8 out of 9 cases, $a == $b will turn out FALSE. (This operator does*

not matter to this thought experiment, but is included for fun)

For $a<$b, expected result FALSE:

*** ***

*** ***!

*** ***

*** ***

0 0 0 0 0 1 1 1 1

*=> in 4 out of 9 cases, "$a < $b" will turn out TRUE.*

For $a<=$b, expected result TRUE:

*** ***

*** ***!

*** ***

*** ***

0 0 0 0 1 1 1 1 1

*=> in 4 out of 9 cases, $a<=$b will turn out FALSE. ..... hey, that's the*

same probability!

The important point is the fact that the error percentage for "<" and "<="

is exactly the same. This holds for all cases where you use "<" and "<="

in the context of floating points: you can safely exchange "<" vs. "<=" in

most FP algorithms and it will not matter at all (exceptions, especially

constructed ones, nonwithstanding).

(NB: while not related to the question at hand, this also shows why == and

< / <= are very different beasts for floating point numbers. For larger

amounts of possible values for $b (larger error margin), the error

percentage of == quickly approaches 100%, while the percentage for < and

<= approximates 50%. While the usefulness of == quickly degrades to zero,

the usefulness of < and <= always stay around 50%, even if $b has an

arbitrarily large (!) error interval (centered around $a with equal

distribution, of course). And in the opposite extreme, where $b can take 3

possible values, == is wrong in 2 out of 3 cases, while < and <= are wrong

in 1 out of 3 cases.)

I love math. ;-)

## Re: Simple loop error

at 03:31 PM, bks@panix.com (Bradley K. Sherman) said:

No. On Earth it test for less-than-or-equal, a single Boolean operator. On

***your***planet it performs two separate tests.

--

Shmuel (Seymour J.) Metz, SysProg and JOAT <http://patriot.net/~shmuel

Unsolicited bulk E-mail subject to legal action. I reserve the

right to publicly post or ridicule any abusive E-mail. Reply to

domain Patriot dot net user shmuel+news to contact me. Do not

reply to spamtrap@library.lspace.org

## Re: Simple loop error

at 02:21 PM, bks@panix.com (Bradley K. Sherman) said:

I did. You wrote, incorrectly, "So, in short, on your planet, in Perl,

'<=' does not test for equality, while here on Earth, it does." and that

is what I responded to.

--

Shmuel (Seymour J.) Metz, SysProg and JOAT <http://patriot.net/~shmuel

Unsolicited bulk E-mail subject to legal action. I reserve the

right to publicly post or ridicule any abusive E-mail. Reply to

domain Patriot dot net user shmuel+news to contact me. Do not

reply to spamtrap@library.lspace.org

## Re: Simple loop error

No, only for those, who missed the first class in Basics of Computer

Numerics:

"Thou shalt not test floating point numbers for equality"

I'm still amazed how often this topic comes up. See 'perldoc -q 999' for

a very brief introduction why this is a bad idea and otherwise check the

archives for previous discussions of this topic or a see a book about

Computer Numerics or even basic introduction about about the difference

between decimal and binary numbers.

jue

#### Site Timeline

- » FAQ 4.65 How can I get the unique keys from two hashes?
- — Next thread in » PERL Discussions

- » FAQ 4.62 What's the difference between "delete" and "undef" with hashes?
- — Previous thread in » PERL Discussions

- » s suffix question
- — Newest thread in » PERL Discussions

- » SSD partition alignment revisited
- — The site's Newest Thread. Posted in » Computer Hardware