Skip Menu |
Report information
Id: 130845
Status: resolved
Priority: 0/
Queue: perl6

Owner: cpan [at] zoffix.com
Requestors: comdog <brian.d.foy [at] gmail.com>
Cc:
AdminCc:

Severity: (no value)
Tag: (no value)
Platform: (no value)
Patch Status: (no value)
VM: (no value)



From: brian d foy <brian.d.foy [...] gmail.com>
Subject: Some things that are less than 5 aren't
To: rakudobug [...] perl.org
Date: Wed, 22 Feb 2017 22:31:27 -0500
Download (untitled) / with headers
text/plain 348b
Here's a curious change over in precision: Show quoted text
> 4.999999999999999 ~~ 0..^5
True Show quoted text
> 4.9999999999999999 ~~ 0..^5
False I figure this is an implementation detail that ties to storage, but one of the selling points of Perl 6 is that this sort of thing isn't a problem anymore. Show quoted text
> $*PERL
Perl 6 (6.c) Show quoted text
> $*VM
moar (2017.01)
Date: Thu, 23 Feb 2017 11:14:07 +0100
Subject: Re: [perl #130845] Some things that are less than 5 aren't
From: Timo Paulssen <timo [...] wakelift.de>
To: perl6-compiler [...] perl.org
Download (untitled) / with headers
text/plain 945b
This is because the implementation of infix:<cmp>(Rat:D, Rat:D) is kind of bad: multi sub infix:<cmp>(Rational:D \a, Rational:D \b) is default { a.Num cmp b.Num } We'll have to do a proper comparison of rats, rather than convert to Num which can give wrong results like this. On 23/02/17 04:32, brian d foy (via RT) wrote: Show quoted text
> # New Ticket Created by "brian d foy" > # Please include the string: [perl #130845] > # in the subject line of all future correspondence about this issue. > # <URL: https://rt.perl.org/Ticket/Display.html?id=130845 > > > > Here's a curious change over in precision: >
> > 4.999999999999999 ~~ 0..^5
> True
> > 4.9999999999999999 ~~ 0..^5
> False > > I figure this is an implementation detail that ties to storage, but > one of the selling points of Perl 6 is that this sort of thing isn't > a problem anymore. >
> > $*PERL
> Perl 6 (6.c)
> > $*VM
> moar (2017.01)
Date: Thu, 23 Feb 2017 13:19:57 +0100
To: perl6-compiler [...] perl.org
From: Timo Paulssen <timo [...] wakelift.de>
Subject: Re: [perl #130845] Some things that are less than 5 aren't
Download (untitled) / with headers
text/plain 742b
Actually, I was mistaken. The code i pointed at there already works for this very case. observe: 131338 timotimo │ m: say 4.999999999999999 cmp 5.0 131338 +camelia │ rakudo-moar 1811b8: OUTPUT: «Less␤» The real problem is this implementation of &infix:<cmp>: multi sub infix:<cmp>(Real:D \a, Real:D \b) { (nqp::istype(a, Rational) && nqp::isfalse(a.denominator)) || (nqp::istype(b, Rational) && nqp::isfalse(b.denominator)) ?? a.Bridge cmp b.Bridge !! a === -Inf || b === Inf ?? Less !! a === Inf || b === -Inf ?? More !! a.Bridge cmp b.Bridge } going via .Bridge will turn the Rat to 5e0 and the comparison goes wrong.
RT-Send-CC: perl6-compiler [...] perl.org
On Wed, 22 Feb 2017 19:32:31 -0800, comdog wrote: Show quoted text
> Here's a curious change over in precision: >
> > 4.999999999999999 ~~ 0..^5
> True
> > 4.9999999999999999 ~~ 0..^5
> False > > I figure this is an implementation detail that ties to storage, but > one of the selling points of Perl 6 is that this sort of thing isn't > a problem anymore. >
> > $*PERL
> Perl 6 (6.c)
> > $*VM
> moar (2017.01)
There are two issues at play here: 1) `cmp` with Rationals with Rational/Ints was busted and was using Num precision. This is now fixed[^1] and tested[^2], so `4.9999999999999999 ~~ 0..^5` does give True 2) My reading of the ticket suggests that you'd expect `4.99999999999999999999999 ~~ 0..^5` to give True as well, however, you'll notice that won't be the case, even after the fix in (1). The reason is such a number is too big to fit into a Rat. If you dump its components, you'll see the loss in precision: <Zoffix> m: dd 4.99999999999999999999999 <camelia> rakudo-moar 9e8ecb: OUTPUT: «<499999999999999999999999/99999999999999991611392>␤» And it's that loss that would give the wrong answer when `cmp` it it or smartmatching with it. That behaviour is LTA and when we'd use proper uint64 for components, things would be even worse. There was a discussion[^3] on the topic today and one of the proposals was to create a RatStr allomorph in such cases which would basically behave as a non-infectious FatRat. I'll give that idea a go in the next few days (though I have a hunch 6.c tests would block this until 6.d). You don't have to wait for resolution of that issue, however, and can make a RatStr yourself, using angled brackets: <Zoffix> m: say <4.99999999999999999999999999999999999999999999> ~~ 0..^5 <camelia> rakudo-moar 9e8ecb: OUTPUT: «True␤» It produces the right result. [1] https://github.com/rakudo/rakudo/commit/9e8ecb7bacedc8845dcf4b290aae431b00ba7e7c [2] https://github.com/perl6/roast/commit/31d9af38842748798a720b0e7b22a305a998c040 [3] https://irclog.perlgeek.de/perl6/2017-02-23#i_14152769
Date: Fri, 24 Feb 2017 07:00:26 +0100
To: perl6-compiler [...] perl.org
From: Joachim Durchholz <jo [...] durchholz.org>
Subject: Re: [perl #130845] Some things that are less than 5 aren't
Download (untitled) / with headers
text/plain 160b
Somewhat offtopic: toolforger: p6: say Inf cmp Inf camelia: rakudo-moar 320c2f: OUTPUT: «Same␤» I.e. Inf compares equal to itself - is this intentional?
Date: Thu, 23 Feb 2017 22:09:16 -0800
From: Darren Duncan <darren [...] darrenduncan.net>
Subject: Re: [perl #130845] Some things that are less than 5 aren't
To: perl6-compiler [...] perl.org
Download (untitled) / with headers
text/plain 628b
On 2017-02-23 10:00 PM, Joachim Durchholz wrote: Show quoted text
> Somewhat offtopic: > > toolforger: p6: say Inf cmp Inf > camelia: rakudo-moar 320c2f: OUTPUT: «Same␤» > > I.e. Inf compares equal to itself - is this intentional?
Even if that isn't valid mathematically, for our purposes in a programming language, Inf is a singleton. I would have a serious problem with any programming language where it isn't the case that asking "is $x the same thing as $x in every possible way" doesn't result in TRUE for all possible values of $x. That's one reason I seriously dislike SQL's NULL that don't equal themselves. -- Darren Duncan
RT-Send-CC: perl6-compiler [...] perl.org
Download (untitled) / with headers
text/plain 1.6k
On Thu, 23 Feb 2017 22:01:25 -0800, jo@durchholz.org wrote: Show quoted text
> Somewhat offtopic:
Feels like a bad place for offtopic discussions. You can ask questions in the same IRC channel you evaled your example. Show quoted text
> toolforger: p6: say Inf cmp Inf > camelia: rakudo-moar 320c2f: OUTPUT: «Same␤» > > I.e. Inf compares equal to itself - is this intentional?
Yes, and we didn't invent this concept. This area is governed by IEEE 2008-753 standard, section 6.1 of which has this to say on infinities: "The behavior of infinity in floating-point arithmetic is derived from the limiting cases of real arithmetic with operands of arbitrarily large magnitude, when such a limit exists. Infinities shall be interpreted in the affine sense, that is: −∞ < {every finite number} < +∞" It's not the only departure from mathematical result for the sake more practically useful results. Division by zero has well-defined behaviour in floating point math, and Rationals in Num view preserve it: <Zoffix> m: say <1/0> == Inf <camelia> rakudo-moar 320c2f: OUTPUT: «True␤» <Zoffix> m: say <-1/0> == -Inf <camelia> rakudo-moar 320c2f: OUTPUT: «True␤» <Zoffix> m: say <0/0>.Num === NaN <camelia> rakudo-moar 320c2f: OUTPUT: «True␤» On Thu, 23 Feb 2017 22:22:22 -0800, darren@DarrenDuncan.net wrote: Show quoted text
> I would have a serious problem with any programming language where it > isn't the > case that asking "is $x the same thing as $x in every possible way" > doesn't > result in TRUE for all possible values of $x.
Then I'd hate to tell you, but such a value exists in most languages: a NaN: <Zoffix> m: say NaN == NaN <camelia> rakudo-moar 320c2f: OUTPUT: «False␤»
Date: Sat, 25 Feb 2017 08:39:08 +0100
To: perl6-compiler [...] perl.org
Subject: Re: [perl #130845] Some things that are less than 5 aren't
From: Joachim Durchholz <jo [...] durchholz.org>
Download (untitled) / with headers
text/plain 1.2k
On 24.02.2017 13:03, Zoffix Znet via RT wrote: Show quoted text
>> toolforger: p6: say Inf cmp Inf >> camelia: rakudo-moar 320c2f: OUTPUT: «Same␤» >> >> I.e. Inf compares equal to itself - is this intentional?
> > Yes, and we didn't invent this concept.
Heh. That's what you'd expect before reading IEEE. After understanding all details of the IEEE standard, it's where you return (but only for infinities, not for NaNs) :-) Show quoted text
> This area is governed by IEEE 2008-753 standard, section 6.1 of which
has this to say on infinities: Show quoted text
> > "The behavior of infinity in floating-point arithmetic is derived from the limiting cases of real arithmetic > with operands of arbitrarily large magnitude, when such a limit exists. Infinities shall be interpreted in > the affine sense, that is: −∞ < {every finite number} < +∞"
While that paragraph does not define how Inf relates to Inf, I see elsewhere that IEEE indeed defines Inf == Inf (and -Inf == -Inf). I thought infinities were just a slightly tamed version of NaNs, but that was a misconception on my side. Show quoted text
> It's not the only departure from mathematical result for the sake more practically useful results.
I know :-) Good thing that we have === so we can compare at the representational level.
RT-Send-CC: perl6-compiler [...] perl.org
Download (untitled) / with headers
text/plain 1020b
On Wed, 22 Feb 2017 19:32:31 -0800, comdog wrote: Show quoted text
> Here's a curious change over in precision: >
> > 4.999999999999999 ~~ 0..^5
> True
> > 4.9999999999999999 ~~ 0..^5
> False > > I figure this is an implementation detail that ties to storage, but > one of the selling points of Perl 6 is that this sort of thing isn't > a problem anymore. >
> > $*PERL
> Perl 6 (6.c)
> > $*VM
> moar (2017.01)
The remaining issues are now fixed, in so far as this now gives True, even when you didn't use the angle brackets: say 4.99999999999999999999999999999999999999999999 ~~ 0..^5 However, what `4.99999999999999999999999999999999999999999999` exactly is will likely change slightly in the future, once we have properly working uint64 and our Rats use it as type for the denominator. One of the proposals is for such literals to end up as RatStr, so you'd basically have the same behaviour as is shown earlier on the ticket where using angle brackets to make a RatStr fixes the noise issue.


This service is sponsored and maintained by Best Practical Solutions and runs on Perl.org infrastructure.

For issues related to this RT instance (aka "perlbug"), please contact perlbug-admin at perl.org