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

duration ctor or addition with duration sensitive to rat/num rand (?) #2221

Open
robertlemmen opened this issue Aug 16, 2018 · 2 comments

Comments

Projects
None yet
4 participants
@robertlemmen
Copy link

commented Aug 16, 2018

don't quite understand what is going on, but:

srand(42);

my $time = DateTime.new(1534447684).utc + Duration.new(0.002);
for ^100 -> $i {
    $time = $time + Duration.new(2.rand);
    say "$i $time";
}

gives me:

0 2018-08-16T19:28:04.551037Z
1 2018-08-16T19:28:05.684497Z
2 2018-08-16T19:28:07.604283Z
3 2018-08-16T19:28:07.911050Z
4 2018-08-16T19:28:09.078071Z
5 2018-08-16T19:28:09.513809Z
Type check failed in assignment to $!tai; expected Rat but got Num (1534447727.7935638e0)
  in block <unit> at ./golf line 7

my perl is 2018.04.1-33-ga957b712c built on MoarVM version 2018.04-39-gc702f4c66

@MasterDuke17

This comment has been minimized.

Copy link
Contributor

commented Aug 16, 2018

@zoffixznet

This comment has been minimized.

Copy link
Contributor

commented Aug 29, 2018

Golfs to:

dd DateTime.new(<14324937243230497380352387879/9335565490898839500>) + Duration.new(<1043/815>)

Addition of two Rats degrades (by design) to a Num if the resultant denominator would be over 64 bits (after reduction):

    dd <14324937243230497380352387879/9335565490898839500> + <1043/815>;
    # 1534447727.7935638e0

And what's happening here is the addition actually produces such a Rat, and we end up with an attempt to assign a Num into a Rat attribute (the $.tai).

One possible fix would be to use Rat.new with the summed up numerators denominators, because Rat.new allows "overloaded" denominators that are larger than 64 bits and it won't degrade to a Num. However, we still end up with .tai that contains a Rat that'd degrade to a Num in math operators, so perhaps that's a bad idea.

my $a := <14324937243230497380352387879/9335565490898839500>;
my $b := <1043/815>;
dd Rat.new: $a.numerator + $b.numerator, $a.denominator + $b.denominator;
# <14324937243230497380352388922/9335565490898840315>

The other fix would be to have all the .tai operations coerce to a .Rat, so we force the Num back to a Rat, but that's losing some precision due to this roundtrip.

Haven't thought about this much, so there might be much better solutions available.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.