wish: a comparison operator that is no more sensitive than printf

D

Dan Jacobson

perldoc perlop says Testing for exact equality of floating-point equality or
inequality is not a good idea.

OK, perhaps add a new "squishy comparison operator" one could use so that even
this wouldn't overrun the last test at least on my computer:
$i=0; while($i<1){printf "$i\n"; $i+=.1};
$i=0; while($i<1){printf "%.19f\n",$i; $i+=.1}; #for details

The main frustration is that the < operator is much more sensitive to
tiny floating trailing digits than any of the printf defaults. I wish
there was a comparison operator "that was no more sensitive than printf".
 
G

gnari

Dan Jacobson said:
perldoc perlop says Testing for exact equality of floating-point equality or
inequality is not a good idea.

OK, perhaps add a new "squishy comparison operator" one could use so that even
this wouldn't overrun the last test at least on my computer:
$i=0; while($i<1){printf "$i\n"; $i+=.1};

if you need exactness try to use integers

$i=0; while($i<10){printf ($i/10)."\n"; $i+=1};

no amount of wishing for some magic will change the
basic reality of how floating point numbers are stored.

gnari
 
T

Thomas Kratz

Dan said:
perldoc perlop says Testing for exact equality of floating-point equality or
inequality is not a good idea.

OK, perhaps add a new "squishy comparison operator" one could use so that even
this wouldn't overrun the last test at least on my computer:
$i=0; while($i<1){printf "$i\n"; $i+=.1};
^^^^
I think you mean:
$i=0; while($i<1){printf "%f\n", $i; $i+=.1}

or else you leave the formatting to the stringification with "".
$i=0; while($i<1){printf "%.19f\n",$i; $i+=.1}; #for details

The main frustration is that the < operator is much more sensitive to
tiny floating trailing digits than any of the printf defaults. I wish
there was a comparison operator "that was no more sensitive than printf".

Thomas
 
B

Ben Morrow

Dan Jacobson said:
perldoc perlop says Testing for exact equality of floating-point equality or
inequality is not a good idea.

OK, perhaps add a new "squishy comparison operator" one could use so that even
this wouldn't overrun the last test at least on my computer:
$i=0; while($i<1){printf "$i\n"; $i+=.1};
$i=0; while($i<1){printf "%.19f\n",$i; $i+=.1}; #for details

The main frustration is that the < operator is much more sensitive to
tiny floating trailing digits than any of the printf defaults. I wish
there was a comparison operator "that was no more sensitive than printf".

Ummm.... what's wrong with

printf('%.19f', $a) < printf('%.19f', $b)

?

Ben
 
T

Ted Zlatanov

perldoc perlop says Testing for exact equality of floating-point
equality or inequality is not a good idea.

OK, perhaps add a new "squishy comparison operator" one could use so
that even this wouldn't overrun the last test at least on my
computer: [...]
The main frustration is that the < operator is much more sensitive
to tiny floating trailing digits than any of the printf defaults. I
wish there was a comparison operator "that was no more sensitive
than printf".

Right below the advice you mention is this code:

sub fp_equal {
my ($X, $Y, $POINTS) = @_;
my ($tX, $tY);
$tX = sprintf("%.${POINTS}g", $X);
$tY = sprintf("%.${POINTS}g", $Y);
return $tX eq $tY;
}

(at least since the Perl 5.6.1 docs)

Did you not see that code? Or are you asking just because you want a
new operator in Perl 5 (a wish very unlikely to be granted)?

Ted
 
T

Ted Zlatanov

if you need exactness try to use integers

$i=0; while($i<10){printf ($i/10)."\n"; $i+=1};

no amount of wishing for some magic will change the
basic reality of how floating point numbers are stored.

I think the OP doesn't want exactness, but the illusion of it :)

Ted
 
J

Jürgen Exner

Dan said:
perldoc perlop says Testing for exact equality of floating-point
equality or inequality is not a good idea.

Yeah, that is "Introduction into Computer Numerics: lesson 1"
OK, perhaps add a new "squishy comparison operator" one could use so
that even this wouldn't overrun the last test at least on my computer:
$i=0; while($i<1){printf "$i\n"; $i+=.1};
$i=0; while($i<1){printf "%.19f\n",$i; $i+=.1}; #for details

The main frustration is that the < operator is much more sensitive to
tiny floating trailing digits than any of the printf defaults. I wish
there was a comparison operator "that was no more sensitive than
printf".

I have no idea what you mean, but have you heard about epsilon environments
or ranges?
At least that is what people are talking about in the second half of
"Introduction into Computer Numerics: lesson 1"

jue
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top