No such thing as a small change PerlMonks

### Insights sought into floating point division strangeness

by EvdB (Deacon)
 on Feb 07, 2006 at 17:32 UTC Need Help??
EvdB has asked for the wisdom of the Perl Monks concerning the following question:

Hello, whilst putting together a reporting page I found that some of my numbers were wrong - I was getting 99% even though it should have been 100%. Prodding around and writing some test scripts I found that it was related to floating point division and sprintf rounding down. This code demonstrates some oddness:

```foreach my \$int ( 1 .. 600 ) {
my \$number = \$int / \$int;
next if \$number == 1;
warn "\$int / \$int != 1 (== \$number)\n";
}

produces:

49 / 49 != 1 (== 1)
98 / 98 != 1 (== 1)
103 / 103 != 1 (== 1)
107 / 107 != 1 (== 1)
161 / 161 != 1 (== 1)
187 / 187 != 1 (== 1)
196 / 196 != 1 (== 1)
197 / 197 != 1 (== 1)
206 / 206 != 1 (== 1)
214 / 214 != 1 (== 1)
237 / 237 != 1 (== 1)
...

Now I know that \$num / \$num != 1 sometimes because of floating point considerations - but I am keen to know more. For instance why is \$number always displayed as '1'? Also why do the hits seem to happen more often as the numbers get larger ( twice for 1..100, eleven times for 401..500)?

Replies are listed 'Best First'.
Re: Insights sought into floating point division strangeness
by swampyankee (Parson) on Feb 07, 2006 at 17:44 UTC

The reason you're getting output like
103 / 103 ! = 1 (==1)
is that the default format used for printing is not showing enough precision; 1 +/- epsilon is getting rounded to 1.

You can either reset \$# to something like "%15.12f" or use a printf with an explicit format.

emc

" When in doubt, use brute force." — Ken Thompson
Re: Insights sought into floating point division strangeness
by samtregar (Abbot) on Feb 07, 2006 at 18:58 UTC
That is very odd. Those aren't floats and neither should the result be. What does this output:

```  use Devel::Peek;
Dump(\$int);
Dump(\$number);

That will show whether \$int and \$number are being stored as IVs (as they obviously should be) or NVs.

-sam

Re: Insights sought into floating point division strangeness
by Roy Johnson (Monsignor) on Feb 07, 2006 at 17:46 UTC
I get no output at all from that, and I wouldn't expect any. Floating point error doesn't creep in when you're dividing something by itself. Both the numerator and denominator will have exactly the same internal representation.

Caution: Contents may have been coded under pressure.
Re: Insights sought into floating point division strangeness
by socketdave (Curate) on Feb 07, 2006 at 17:44 UTC
I don't see this behavior on 5.8.4 on Linux.
Nothing is printed with Perl v5.8.6 built for i386-freebsd-64int
Re: Insights sought into floating point division strangeness
by BrowserUk (Pope) on Feb 07, 2006 at 17:55 UTC

What version/platform of perl are you running on?

I don't see, nor would I expect to see any "hits" from this?

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Insights sought into floating point division strangeness
by BrowserUk (Pope) on Feb 07, 2006 at 18:51 UTC

Also, is this a self-built (or self configured) perl, or a vendored install?

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Insights sought into floating point division strangeness
by spiritway (Vicar) on Feb 08, 2006 at 01:37 UTC

I've got perl 5.8.7 on Windows (ActiveState) - and I get no output. I'm wondering whether there is some OS-specific issue where your numbers (or some of them) are being converted to floats silently.

I'm also wondering whether it would help to try something like this:

```use strict;
use warnings;

my \$number_one=1;
foreach my \$int ( 1 .. 600 ) {
my \$number = \$int / \$int;
next if \$number == \$number_one;
warn "\$int / \$int != 1 (== \$number)\n";
}

I'm thinking that by comparing variables to another variable, instead of a literal constant, you may get everyone to be in the same form, and thus avoid this problem.

Re: Insights sought into floating point division strangeness
by EvdB (Deacon) on Feb 08, 2006 at 09:17 UTC

I think that the reason I am getting this odd behaviour is that perl was compiled with fastmath support. It is now being taken out so I'll test again later.

perl was compiled with fastmath support

-ffast-math was one of the options from your -V log that caused me ask how your Perl was configured/built. The other was the -march=pentium2, which is probably perfectly valid, but seems way back level unless it's a very old machine or the -mcpu=athlon-xp requires it?

If your looking at your build configuration, it might be worth questioning these two also.

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
As the perpetrator of the aforementioned perl, we have a standard set of perl binaries across each os/arch, and as the lowest spec machine we have in service is a PII everything still gets -march=pentium2. The -ffast-math was probably a little over enthusiastic. Amusingly enough checking an alpha box shows it gets compiled with -mieee which ensure it has (slower) IEE 754 compliant behaviour, and so does not show this up. Needless to say our perl binaries are now !-ffast-math ...

Create A New User
Node Status?
node history
Node Type: perlquestion [id://528568]
Approved by Corion
help
Chatterbox?
 [ambrus]: Petroza had trouble posting yesterday, but has posted Issues Fetching URL with a variable token since. [mz2255]: Yes, just edited the scratchpad. Not sure if I'm doing something wrong, it's my first time. [ambrus]: No, you're not doing anything wrong. It's just that our automatic spam filters confuse you with the spammers who post advertisments for online shops of counterfeit branded clothing.

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (11)
As of 2017-10-19 15:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
My fridge is mostly full of:

Results (255 votes). Check out past polls.

Notices?