Skip to content

Commit

Permalink
Merge pull request #475 from perl6/normalized-ZDRs
Browse files Browse the repository at this point in the history
Normalize ZDRs
  • Loading branch information
zoffixznet committed Sep 24, 2018
2 parents 84974bf + 17761c2 commit 30ad4de
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 42 deletions.
3 changes: 0 additions & 3 deletions S03-operators/arith.t
Original file line number Diff line number Diff line change
Expand Up @@ -337,16 +337,13 @@ All uses of a zero modulus or divisor should 'die', and the
'Division by zero with infix:<div> dies and is catchable with VRef variables';

throws-like { say 0 / 0 }, X::Numeric::DivideByZero,
numerator => 0,
'Division by zero with infix:</> dies and is catchable (1)';
throws-like { say 3 / 0 }, X::Numeric::DivideByZero,
numerator => 3,
'Division by zero with infix:</> dies and is catchable (2)';
throws-like { my $x = 0; say 3.5 / $x }, X::Numeric::DivideByZero,
# numerator => 3.5, # numerator is always an Int, so we get 7
'Division by zero with infix:</> dies and is catchable with VInt/VRat variables';
throws-like { my $x = 0; say 4 / $x }, X::Numeric::DivideByZero,
numerator => 4,
'Division by zero with infix:</> dies and is catchable with VRef variables';
}

Expand Down
29 changes: 13 additions & 16 deletions S32-num/fatrat.t
Original file line number Diff line number Diff line change
Expand Up @@ -278,26 +278,23 @@ subtest 'Rational.isNaN' => {
}

subtest '=== with 0-denominator FatRats' => {
plan 15;
plan 11;
sub postfix:<F> (Rat $_ --> FatRat) { FatRat.new: .numerator, .denominator }

# We normalize these Rationals, so only numerator's sign matters
is-deeply <0/0>F === <0/0>F, True, ' 0/0 === 0/0';
is-deeply <2/0>F === <2/0>F, True, ' 2/0 === 2/0';
is-deeply <-2/0>F === <-2/0>F, True, '-2/0 === -2/0';

is-deeply <0/0>F === <-2/0>F, False, ' 0/0 === -2/0';
is-deeply <0/0>F === <2/0>F, False, ' 0/0 === 2/0';
is-deeply <2/0>F === <0/0>F, False, ' 2/0 === 0/0';
is-deeply <5/0>F === <2/0>F, False, ' 5/0 === 2/0';
is-deeply <2/0>F === <5/0>F, False, ' 2/0 === 5/0';
is-deeply <-5/0>F === <-2/0>F, False, '-5/0 === -2/0';
is-deeply <-2/0>F === <-5/0>F, False, '-2/0 === -5/0';

is-deeply <0/0>F === <2/2>F, False, ' 0/0 === 2/2';
is-deeply <2/2>F === <0/0>F, False, ' 2/2 === 0/0';
is-deeply <5/2>F === <2/0>F, False, ' 5/2 === 2/0';
is-deeply <2/0>F === <5/2>F, False, ' 2/0 === 5/2';
is-deeply <-5/2>F === <-2/0>F, False, '-5/2 === -2/0';
is-deeply <-2/0>F === <-5/2>F, False, '-2/0 === -5/2';

is-deeply <-2/0>F === <-2/0>F, True, '-2/0 === -2/0';
is-deeply <-2/0>F === <-4/0>F, True, '-2/0 === -4/0';
is-deeply <-2/0>F === <2/0>F, False, '-2/0 === -2/0';
is-deeply <-2/0>F === <0/0>F, False, '-2/0 === 0/0';

is-deeply <2/0>F === <-2/0>F, False, ' 2/0 === -2/0';
is-deeply <2/0>F === <4/0>F, True, ' 2/0 === 4/0';
is-deeply <2/0>F === <2/0>F, True, ' 2/0 === 2/0';
is-deeply <2/0>F === <0/0>F, False, ' 2/0 === 0/0';
}

# RT#130427
Expand Down
45 changes: 22 additions & 23 deletions S32-num/rat.t
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use lib $?FILE.IO.parent(2).add("packages");
use Test;
use Test::Util;

plan 853;
plan 855;

# Basic test functions specific to rational numbers.

Expand Down Expand Up @@ -40,8 +40,8 @@ is(Rat.new(2, 4).nude, (1, 2), "Reduce to simplest form in constructor");
is(Rat.new(39, 33).nude, (13, 11), "Reduce to simplest form in constructor");
is(Rat.new(0, 33).nude, (0, 1), "Reduce to simplest form in constructor");
is(Rat.new(1451234131, 60).nude, (1451234131, 60), "Reduce huge number to simplest form in constructor");
is(Rat.new(1141234123, 0).nude, (1141234123, 0), "Huge over zero stays huge over zero");
is(Rat.new(-7, 0).nude, (-7, 0), "Negative seven over zero stays negative seven over zero");
is(Rat.new(1141234123, 0).nude, (1, 0), "Huge over zero is normalized");
is(Rat.new(-7, 0).nude, (-1, 0), "Negative seven over zero is normalized");
is(Rat.new(0, 0).nude, (0,0), "Zero over zero stays zero over zero");

# Test basic math
Expand Down Expand Up @@ -331,8 +331,7 @@ is 241025348275725.3352.Str, "241025348275725.3352", 'stringification of bigish
}

#RT #126391
try {say 42/(.1+.2-.3)};
isa-ok( $!.numerator, 42, "got the answer rather than 420");
try {say 42/(.1+.2-.3)}; isnt $!.numerator, 420, "no bogus errors";

# RT#126016
subtest '0.9999999999999999999999 to string conversions' => {
Expand Down Expand Up @@ -396,25 +395,21 @@ subtest 'Rational.isNaN' => {
}

subtest '=== with 0-denominator Rats' => {
plan 15;
plan 11;

is-deeply <0/0> === <0/0>, True, ' 0/0 === 0/0';
is-deeply <2/0> === <2/0>, True, ' 2/0 === 2/0';
is-deeply <-2/0> === <-2/0>, True, '-2/0 === -2/0';

is-deeply <0/0> === <-2/0>, False, ' 0/0 === -2/0';
is-deeply <0/0> === <2/0>, False, ' 0/0 === 2/0';
is-deeply <2/0> === <0/0>, False, ' 2/0 === 0/0';
is-deeply <5/0> === <2/0>, False, ' 5/0 === 2/0';
is-deeply <2/0> === <5/0>, False, ' 2/0 === 5/0';
is-deeply <-5/0> === <-2/0>, False, '-5/0 === -2/0';
is-deeply <-2/0> === <-5/0>, False, '-2/0 === -5/0';

is-deeply <0/0> === <2/2>, False, ' 0/0 === 2/2';
is-deeply <2/2> === <0/0>, False, ' 2/2 === 0/0';
is-deeply <5/2> === <2/0>, False, ' 5/2 === 2/0';
is-deeply <2/0> === <5/2>, False, ' 2/0 === 5/2';
is-deeply <-5/2> === <-2/0>, False, '-5/2 === -2/0';
is-deeply <-2/0> === <-5/2>, False, '-2/0 === -5/2';

is-deeply <-2/0> === <-2/0>, True, '-2/0 === -2/0';
is-deeply <-2/0> === <-4/0>, True, '-2/0 === -4/0';
is-deeply <-2/0> === <2/0>, False, '-2/0 === -2/0';
is-deeply <-2/0> === <0/0>, False, '-2/0 === 0/0';

is-deeply <2/0> === <-2/0>, False, ' 2/0 === -2/0';
is-deeply <2/0> === <4/0>, True, ' 2/0 === 4/0';
is-deeply <2/0> === <2/0>, True, ' 2/0 === 2/0';
is-deeply <2/0> === <0/0>, False, ' 2/0 === 0/0';
}

# RT #130606
Expand Down Expand Up @@ -634,12 +629,16 @@ subtest 'Rational keeps nu/de in proper types' => {
is-deeply .denominator, Foo.new(2), 'denominator';
}

# 6.d TODO XXX: are we normalizing ZDRs or not normalizing them?
with Bar.new: Foo.new(42), Foo.new: 0 {
# numerator is meant to be normalized, so it'll end up as 1
is-deeply .numerator, Foo.new(42), 'numerator (zero-denom rational)';
is-deeply .numerator, Foo.new(1), 'numerator (zero-denom rational)';
is-deeply .denominator, Foo.new(0), 'denominator (zero-denom rational)';
}
}

fails-like { <42/0>.floor }, X::Numeric::DivideByZero,
'Rational.floor fails for zero-denominator-rationals';
fails-like { <42/0>.ceiling }, X::Numeric::DivideByZero,
'Rational.ceiling fails for zero-denominator-rationals';

# vim: ft=perl6

0 comments on commit 30ad4de

Please sign in to comment.