Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

method rand (Range) ignores $!excludes-max flag #5620

Closed
p6rt opened this issue Aug 27, 2016 · 6 comments
Closed

method rand (Range) ignores $!excludes-max flag #5620

p6rt opened this issue Aug 27, 2016 · 6 comments
Labels

Comments

@p6rt
Copy link

p6rt commented Aug 27, 2016

Migrated from rt.perl.org#129104 (status was 'resolved')

Searchable as RT129104$

@p6rt
Copy link
Author

p6rt commented Aug 27, 2016

From @titsuki

See the following result

$ perl6 -e '(1..^(1+10e-15)).rand.say'
1.00000000000001

$ perl6 -e 'say 1.00000000000001 - (1+10e-15)'
0

$ perl6 -e '(1..^(1+10e-15)).excludes-max.say'
True

I think that the 1st example it shouldn't return 1+10e-15, since Range.excludes-max is True.

My Perl 6 version is
$ perl6 --version
This is Rakudo version 2016.07.1-199-gdd9b760 built on MoarVM version 2016.07-17-g40948f6
implementing Perl 6.c.

@p6rt
Copy link
Author

p6rt commented Aug 27, 2016

From @titsuki

*1st example shouldn't

On 2016-8月-26 金 22​:31​:11, cookbook_000@​yahoo.co.jp wrote​:

See the following result

$ perl6 -e '(1..^(1+10e-15)).rand.say'
1.00000000000001

$ perl6 -e 'say 1.00000000000001 - (1+10e-15)'
0

$ perl6 -e '(1..^(1+10e-15)).excludes-max.say'
True

I think that the 1st example it shouldn't return 1+10e-15, since
Range.excludes-max is True.

My Perl 6 version is
$ perl6 --version
This is Rakudo version 2016.07.1-199-gdd9b760 built on MoarVM version
2016.07-17-g40948f6
implementing Perl 6.c.

@p6rt
Copy link
Author

p6rt commented Aug 27, 2016

From @lizmat

Welcome to the world of floating point accuracy.

This little program typically ends within 100 iterations for me​:

$ 6 'my int $a = 0; $a++ while (1..^(1+10e-15)).rand < 1+10e-15; say $a’
96

Either we dismiss this bug as being caused by lack of floating point accuracy, or we build in something special in .rand for Num Ranges that will make sure that the returned value actually is less then upper range as indicated by “<“.

I would mark this RT as a @​LARRY case.

On 27 Aug 2016, at 08​:31, Itsuki Toyota (via RT) <perl6-bugs-followup@​perl.org> wrote​:

# New Ticket Created by Itsuki Toyota
# Please include the string​: [perl #​129104]
# in the subject line of all future correspondence about this issue.
# <URL​: https://rt-archive.perl.org/perl6/Ticket/Display.html?id=129104 >

See the following result

$ perl6 -e '(1..^(1+10e-15)).rand.say'
1.00000000000001

$ perl6 -e 'say 1.00000000000001 - (1+10e-15)'
0

$ perl6 -e '(1..^(1+10e-15)).excludes-max.say'
True

I think that the 1st example it shouldn't return 1+10e-15, since Range.excludes-max is True.

My Perl 6 version is
$ perl6 --version
This is Rakudo version 2016.07.1-199-gdd9b760 built on MoarVM version 2016.07-17-g40948f6
implementing Perl 6.c.

@p6rt
Copy link
Author

p6rt commented Aug 27, 2016

The RT System itself - Status changed from 'new' to 'open'

@p6rt
Copy link
Author

p6rt commented Dec 16, 2016

From @zoffixznet

On Fri, 26 Aug 2016 22​:31​:11 -0700, cookbook_000@​yahoo.co.jp wrote​:

See the following result

$ perl6 -e '(1..^(1+10e-15)).rand.say'
1.00000000000001

$ perl6 -e 'say 1.00000000000001 - (1+10e-15)'
0

$ perl6 -e '(1..^(1+10e-15)).excludes-max.say'
True

I think that the 1st example it shouldn't return 1+10e-15, since
Range.excludes-max is True.

My Perl 6 version is
$ perl6 --version
This is Rakudo version 2016.07.1-199-gdd9b760 built on MoarVM version
2016.07-17-g40948f6
implementing Perl 6.c.

Thank you for the report. This is now fixed (to clarify, what's fixed is the
.rand occasionally producing a value equal to the excluded end point, so the
one-liner lizmat produced no longer exits. When the generated numbers are
printed, they still show only the "1.00000000000001" and "1", despite actual
values being noisier).

Fix rakudo/rakudo@334d134
Test​: Raku/roast@55febd6

I'm also reproducing the fix commit message since it answers some of the
questions asked on the ticket​:

  Fix Range.rand generating value equal to excluded endpoints

  Fixes RT#​129104​: https://rt-archive.perl.org/perl6/Ticket/Display.html?id=129104

  The discussion in the ticket speculates this only affects Num ranges,
  however from the code I see other types are affected as well. For example
  using a Rat 1.00000000000001 has the same issue in lizmat's one liner.
  Reproducing with an Int is a bit harder due to much larger pool of
  possible random numbers, but sometime... somewhere... in a nuclear plant
  running on Perl 6, a .rand on Int range with endpoint exclusions will
  produce a value equal to the excluded endpoint.

  The problem is due to us checking generated value against zero and
  $!min-$!max, fix by adding the $!min before doing exclusion point
  equality checks. This way, the floating point noise does not introduce
  a difference between what we checked against and what we return.

  I measured performance impact of this on 100,000 iterations and found
  it to be only 1.1% speed loss.

@p6rt
Copy link
Author

p6rt commented Dec 16, 2016

@zoffixznet - Status changed from 'open' to 'resolved'

@p6rt p6rt closed this as completed Dec 16, 2016
@p6rt p6rt added the at_larry label Jan 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant