Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > wish: a comparison operator that is no more sensitive than printf

Reply
Thread Tools

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

 
 
Dan Jacobson
Guest
Posts: n/a
 
      03-02-2004
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".
 
Reply With Quote
 
 
 
 
gnari
Guest
Posts: n/a
 
      03-02-2004
"Dan Jacobson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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




 
Reply With Quote
 
 
 
 
Thomas Kratz
Guest
Posts: n/a
 
      03-02-2004
Dan Jacobson wrote:
> 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


--
open STDIN,"<&DATA";$=+=14;$%=50;while($_=(seek( #J~.> a>n~>>e~.......>r.
STDIN,$:*$=+$,+$%,0),getc)){/\./&&last;/\w| /&&( #.u.t.^..oP..r.>h>a~.e..
print,$_=$~);/~/&&++$:;/\^/&&--$:;/>/&&++$,;/</ #.>s^~h<t< ..~. ...c.^..
&&--$,;$:%=4;$,%=23;$~=$_;++$i==1?++$,:_;}__END__#.... >>e>r^..>l^...>k^..
 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      03-02-2004

Dan Jacobson <(E-Mail Removed)> wrote:
> 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

--
"If a book is worth reading when you are six, * http://www.velocityreviews.com/forums/(E-Mail Removed)
it is worth reading when you are sixty." - C.S.Lewis
 
Reply With Quote
 
Ted Zlatanov
Guest
Posts: n/a
 
      03-02-2004
On Tue, 02 Mar 2004, (E-Mail Removed) wrote:

> 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
 
Reply With Quote
 
Ted Zlatanov
Guest
Posts: n/a
 
      03-02-2004
On Tue, 2 Mar 2004, (E-Mail Removed) wrote:

> 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
 
Reply With Quote
 
David K. Wall
Guest
Posts: n/a
 
      03-02-2004
Ben Morrow <(E-Mail Removed)> wrote:

> Dan Jacobson <(E-Mail Removed)> wrote:
>> 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)
>
> ?


I think you meant sprintf(), not printf().

 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      03-03-2004
Dan Jacobson wrote:
> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
Like all great travelers, I have seen more than I remember andremember more than I have seen. shenrilaa@gmail.com C++ 0 03-05-2008 08:41 AM
Like all great travelers, I have seen more than I remember andremember more than I have seen. shenrilaa@gmail.com C Programming 0 03-05-2008 03:26 AM
Comparison - Language sensitive collation and searching using UTF-8 Soso C++ 1 09-26-2007 01:33 PM
Is WinXP more driver sensitive than 98SE or Me? Jimmy Dean Computer Support 2 12-08-2003 03:12 AM



Advertisments