Ask a Question related to PERL Miscellaneous, Design and Development.
-
Thens #1
comparing floating point numbers
Hi,
I have a problem comparing floating point numbers. After a lot of googling and docs I found the issue to be because of the internal floating point representation.
Here is a sample program that explains that
#!/usr/local/bin/perl
use strict;
use warnings;
my ( $num, $num1, $num2 );
$num = 1.8;
$num1 = ( $num * (1 + (10/100) ) ); # $num + 10% $num => 1.98
print "Number :", sprintf "%2.20f \n" , $num;
print "Number +10% :", sprintf "%2.20f \n", $num1;
$num2 = 1.98;
print "Number +10% :", sprintf "%30.20f \n", $num2;
if ( $num1 != $num2 ) {
print "FIRST PASS >> $num1 is not equal to $num2 !! \n";
}
if ( ($num1+0.1) != ($num2+.1) ) {
print "SECOND PASS >> $num1 is not eqaul to $num2 !!\n";
}
This prints
FIRST PASS >> 1.98 is not equal to 1.98 !!
I understand this is because of the floating point representation. Then I tried to solve it by adding 0.1 at both the ends and then comparing the numbers. THis seems to have solved the problem. My question is, Is this a reliable solution.
While searching the group archive, I found this can be achieved by checking for a small delta like
$delta = 0.0001
if ( abs ( $num1 - $num2 ) < $delta ) {
# Now they are eual
}
...
I wanted to know under what circumstances may adding 0.1 at both ends can fail.
Thanks and Regards,
Thens.
Thens Guest
-
Comparing structures, ignoring floating point roundoff differences
I've got a problem where I need to compare structures that have floating point numbers stashed in them. I want to test the structures for identity... -
Random Floating point numbers
I am trying to write a function that will gerenerate a random number that is of float value. Now I need it to generate between two values like so: ... -
calculator, digits behind floating point
Dear forum users, i got an calculator doing all kinds of things with some values users past in text fields. In an other textfield i get the... -
Floating Point precision madness
Hi All, I have the rather fun task of converting IEEE 32 bit floating Hex into decimal. From what I can tell Director has limited base handling... -
From floating point to fraction
Hi I'd like to, from inside my Perl script, go from a floating point number, like 0.33333333333333333, to a fraction, in this case 1/3. Is that... -
Mothra #2
Re: comparing floating point numbers
"Thens" <thens@nospam.com> wrote in message
news:20030710101239.5a111e91.thens@nospam.com...googling and docs I found the issue to be because of the internal floating> Hi,
>
> I have a problem comparing floating point numbers. After a lot of
point representation.[snipped]>
Here is what I use to compare two floating numbers.
until ( equal( $tmp_rise_2, $tmp_rise_3, 8 ) ) {
do stuff....
}
sub equal {
#
#
# FUNCTIONAL SEQUENCE for equal
#
# _GIVEN
#
# Two floating point numbers and Accuracy
#
# _THEN
#
# Use sprintf to format the numbers to Accuracy
# number of decimal places
#
# _RETURN
#
# True if the numbers are equal
#
my ( $A, $B, $dp ) = @_;
return sprintf( "%.${dp}g", $A ) eq sprintf( "%.${dp}g", $B );
}
hope this helps
Mothra
Mothra Guest
-
Greg Bacon #3
Re: comparing floating point numbers
In article <20030710101239.5a111e91.thens@nospam.com>,
Thens <thens@nospam.com> wrote:
: [...]
:
: While searching the group archive, I found this can be achieved by
: checking for a small delta like
:
: $delta = 0.0001
:
: if ( abs ( $num1 - $num2 ) < $delta ) {
: # Now they are eual
: }
:
: I wanted to know under what circumstances may adding 0.1 at both
: ends can fail.
That depends on the nature of your computation. Remember that computers
use finite-precision arithmetic, the way you structure computations can
hurt you, e.g., dividing by a very small number or subtracting numbers
close values can produce serious rounding errors.
See "What Every Computer Scientist Should Know About Floating-Point
Arithmetic" at
[url]http://docs.sun.com/source/806-3568/ncg_goldberg.html[/url]
Even checking for membership in some epsilon-neighborhood isn't
always sufficient:
Incidentally, some people think that the solution to such
anomalies is never to compare floating-point numbers for
equality, but instead to consider them equal if they are within
some error bound E. This is hardly a cure-all because it raises
as many questions as it answers. What should the value of E be?
If x < 0 and y > 0 are within E, should they really be
considered to be equal, even though they have different signs?
Furthermore, the relation defined by this rule,
a ~ b <=> |a - b| < E
is not an equivalence relation because a ~ b and b ~ c does
not imply that a ~ c.
ibid.
Greg
--
While the federal government is frustrating when it treats economic
problems with nonchalance, it is terrifying when it gets involved.
-- Bob Novak
Greg Bacon Guest
-
Michael P. Broida #4
Re: comparing floating point numbers
Thens wrote:
One of the things you need to learn about floating point>
> Hi,
>
> I have a problem comparing floating point numbers.
numbers is:
NEVER compare them for equality (or inequality).
Mike
Michael P. Broida Guest
-
Unregistered #5
Re: comparing floating point numbers
Adding this worked for me.
$num1 = "$num1";
$num2 = "$num2";
$num1 = $num1 * 1;
$num2 = $num2 * 1;
This seems to "reset" Perl's floating point value and lets you compare correctly.
webmaster
taketheheavenquiz.comUnregistered Guest



Reply With Quote

