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
| [reply] [d/l] |
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
| [reply] [d/l] |
Re: Insights sought into floating point division strangeness
by Roy Johnson (Monsignor) on Feb 07, 2006 at 17:46 UTC
|
| [reply] |
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. | [reply] [d/l] |
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.
Cheers for the comments.
| [reply] |
|
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.
| [reply] [d/l] [select] |
|
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 ...
| [reply] |
|
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. | [reply] |
|
This is my setup (5.8.7 on NetBSD):
| [reply] [d/l] |
|
Nothing is printed with Perl v5.8.6 built for i386-freebsd-64int
| [reply] |
Re: Insights sought into floating point division strangeness
by BrowserUk (Patriarch) 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.
| [reply] [d/l] |
Re: Insights sought into floating point division strangeness
by BrowserUk (Patriarch) on Feb 07, 2006 at 18:51 UTC
|
Does your output change if you add use integer; to your test?
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.
| [reply] [d/l] |