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
Swap meanings of Int / Int and div #1263
Comments
From @colomonThere are two -- well, three issues with this patch that I know of: 1. div merely has the code for the old Int / Int, but S03 suggests the -- |
From @colomon0001-Changes-needed-to-swap-the-meanings-of-div-and-as.patchFrom 5734b0bdf8ebc2da1f9bd6ee1707b709851cd284 Mon Sep 17 00:00:00 2001
From: Solomon Foster <colomon@gmail.com>
Date: Mon, 31 Aug 2009 19:02:28 -0400
Subject: [PATCH] Changes needed to swap the meanings of div and /, as per latest S03. Please
note that the div command in Int.pm almost certainly needs some tweaking to
meet the spec, I have just given it the meaning of the old /, but there are
subtle differences. Also we have one failing test in Temporal.t, leading me to
suspect I messed something up in the Temporal.pm patch.
---
CREDITS | 4 ++++
build/gen_metaop_pir.pl | 2 ++
src/setting/Int.pm | 2 +-
src/setting/Rat.pm | 12 +++++++++---
src/setting/Temporal.pm | 38 +++++++++++++++++++-------------------
5 files changed, 35 insertions(+), 23 deletions(-)
diff --git a/CREDITS b/CREDITS
index 3a4858a..97a5c54 100644
--- a/CREDITS
+++ b/CREDITS
@@ -296,6 +296,10 @@ N: Simon Cozens
U: simon
E: simon@simon-cozens.org
+N: Solomon Foster
+U: colomon
+E: colomon@gmail.com
+
N: St��phane Payrard
D: Various code fixes and improvements
diff --git a/build/gen_metaop_pir.pl b/build/gen_metaop_pir.pl
index dc47087..92bf23d 100644
--- a/build/gen_metaop_pir.pl
+++ b/build/gen_metaop_pir.pl
@@ -9,6 +9,8 @@ my @ops = qw(
** 1 op
* 1 op
/ 'fail' op
+ div 'fail' op
+ mod 'fail' op
% 'fail' op
x 'fail' op
xx 'fail' op
diff --git a/src/setting/Int.pm b/src/setting/Int.pm
index 27bbcd7..864e551 100644
--- a/src/setting/Int.pm
+++ b/src/setting/Int.pm
@@ -49,7 +49,7 @@ multi sub infix:<*>(Int $a, Int $b) {
}
}
-multi sub infix:</>(Int $a, Int $b) {
+multi sub infix:<div>(Int $a, Int $b) {
Q:PIR {
$P0 = find_lex '$a'
$N0 = $P0
diff --git a/src/setting/Rat.pm b/src/setting/Rat.pm
index 9f34f9d..ba73dac 100644
--- a/src/setting/Rat.pm
+++ b/src/setting/Rat.pm
@@ -20,10 +20,12 @@ class Rat {
$denominator = -$denominator;
}
my $gcd = gcd($numerator, $denominator);
- $numerator /= $gcd;
- $denominator /= $gcd;
+ $numerator = $numerator div $gcd;
+ $denominator = $denominator div $gcd;
self.bless(*, :$numerator, :$denominator);
}
+
+ multi method perl() { "Rat.new($!numerator, $!denominator)"; }
multi method Str() { "$!numerator/$!denominator"; }
@@ -52,6 +54,10 @@ multi sub infix:<->(Rat $a, Int $b) {
Rat.new($a.numerator - $b * $a.denominator, $a.denominator);
}
+multi sub infix:<->(Int $a, Rat $b) {
+ Rat.new($a * $b.denominator - $b.numerator, $b.denominator);
+}
+
multi sub infix:<*>(Rat $a, Rat $b) {
Rat.new($a.numerator * $b.numerator, $a.denominator * $b.denominator);
}
@@ -60,7 +66,7 @@ multi sub infix:</>(Rat $a, Rat $b) {
Rat.new($a.numerator * $b.denominator, $a.denominator * $b.numerator);
}
-multi sub infix:<div>(Int $a, Int $b) {
+multi sub infix:</>(Int $a, Int $b) {
Rat.new($a, $b);
}
diff --git a/src/setting/Temporal.pm b/src/setting/Temporal.pm
index c7fd3df..294c194 100644
--- a/src/setting/Temporal.pm
+++ b/src/setting/Temporal.pm
@@ -17,11 +17,11 @@ role Temporal::Date {
method day-of-week { # returns DayOfWeek {
my ( $a, $y, $m, $jd ); # algorithm from Claus T��ndering
- $a = int((14 - $.month) / 12 );
+ $a = int((14 - $.month) div 12 );
$y = $.year + 4800 - $a;
$m = $.month + 12 * $a - 3;
- $jd = $.day + int((153 * $m + 2) / 5) + 365 * $y + int( $y / 4 )
- - int( $y / 100 ) + int( $y / 400 ) - 32045;
+ $jd = $.day + int((153 * $m + 2) div 5) + 365 * $y + int( $y div 4 )
+ - int( $y div 100 ) + int( $y div 400 ) - 32045;
return ($jd + 1) % 7 + 1;
}
@@ -90,8 +90,8 @@ role Temporal::TimeZone::Observance {
# The ISO8601 standard does not allow for offsets with sub-minute
# resolutions. In real-world practice, this is not an issue.
our Str method iso8601 {
- sprintf "%+03d%02d", self.offset / 3600,
- int( abs(self.offset) / 60 ) % 60;
+ sprintf "%+03d%02d", self.offset div 3600,
+ int( abs(self.offset) div 60 ) % 60;
}
method Str { self.iso8601 }
@@ -117,11 +117,11 @@ role Temporal::DateTime {
# This involves a whole bunch of code - see Perl 5's Time::Local
our Num method epoch {
my ( $a, $y, $m, $jd ); # algorithm from Claus T��ndering
- $a = int((14 - $.date.month) / 12 );
+ $a = int((14 - $.date.month) div 12 );
$y = $.date.year + 4800 - $a;
$m = $.date.month + 12 * $a - 3;
- $jd = $.date.day + int((153 * $m + 2) / 5) + 365 * $y
- + int( $y / 4 ) - int( $y / 100 ) + int( $y / 400 ) - 32045;
+ $jd = $.date.day + int((153 * $m + 2) div 5) + 365 * $y
+ + int( $y div 4 ) - int( $y div 100 ) + int( $y div 400 ) - 32045;
return ($jd - 2440588) * 24 * 60 * 60
+ ($.time.hour*60 + $.time.minute)*60 + $.time.second;
}
@@ -136,21 +136,21 @@ class Time {
our method gmtime( Num $epoch = time ) {
my ( $time, $second, $minute, $hour, $day, $month, $year );
$time = int( $epoch );
- $second = $time % 60; $time = int($time/60);
- $minute = $time % 60; $time = int($time/60);
- $hour = $time % 24; $time = int($time/24);
+ $second = $time % 60; $time = int($time div 60);
+ $minute = $time % 60; $time = int($time div 60);
+ $hour = $time % 24; $time = int($time div 24);
# Day month and leap year arithmetic, based on Gregorian day #.
# 2000-01-01 noon UTC == 2451558.0 Julian == 2451545.0 Gregorian
$time += 2440588; # because 2000-01-01 == Unix epoch day 10957
my $a = $time + 32044; # date algorithm from Claus T��ndering
- my $b = int((4 * $a + 3) / 146097); # 146097 = days in 400 years
- my $c = $a - int(( 146097 * $b ) / 4);
- my $d = int((4 * $c + 3) / 1461); # 1461 = days in 4 years
- my $e = $c - int(($d * 1461) / 4);
- my $m = int((5 * $e + 2) / 153); # 153 = days in Mar-Jul Aug-Dec
- $day = $e - int((153 * $m + 2) / 5 ) + 1;
- $month = $m + 3 - 12 * int( $m / 10 );
- $year = $b * 100 + $d - 4800 + int( $m / 10 );
+ my $b = int((4 * $a + 3) div 146097); # 146097 = days in 400 years
+ my $c = $a - int(( 146097 * $b ) div 4);
+ my $d = int((4 * $c + 3) div 1461); # 1461 = days in 4 years
+ my $e = $c - int(($d * 1461) div 4);
+ my $m = int((5 * $e + 2) div 153); # 153 = days in Mar-Jul Aug-Dec
+ $day = $e - int((153 * $m + 2) div 5 ) + 1;
+ $month = $m + 3 - 12 * int( $m div 10 );
+ $year = $b * 100 + $d - 4800 + int( $m div 10 );
Temporal::DateTime.new(
date => Temporal::Date.new(:$year, :$month, :$day),
time => Temporal::Time.new(:$hour, :$minute, :$second),
--
1.6.0.5
|
From @colomonspectest-changes-needed-to-swap-the-meanings-of-div-and-as.patchIndex: t/spec/S02-builtin_data_types/num.t
===================================================================
--- t/spec/S02-builtin_data_types/num.t (revision 28143)
+++ t/spec/S02-builtin_data_types/num.t (working copy)
@@ -19,7 +19,7 @@
#L<S02/Built-In Data Types/Rat supports arbitrary precision rational arithmetic>
{
- my $a = 1 div 1;
+ my $a = 1 / 1;
isa_ok($a, Rat);
is($a, "1/1", '1.0 stringification works');
}
@@ -91,7 +91,7 @@
}
#L<S02/Built-In Data Types/Rat supports arbitrary precision rational arithmetic>
-isa_ok(1 div 1, Rat);
+isa_ok(1 / 1, Rat);
{
my $a = 80000.0000000000000000000000000;
Index: t/spec/S02-builtin_data_types/declare.t
===================================================================
--- t/spec/S02-builtin_data_types/declare.t (revision 28143)
+++ t/spec/S02-builtin_data_types/declare.t (working copy)
@@ -29,7 +29,7 @@
}
{
- my Rat $namcu = 7 div 4;
+ my Rat $namcu = 7 / 4;
isa_ok($namcu,Rat);
}
@@ -148,7 +148,7 @@
#?rakudo skip 'rat not implemented'
{
- my rat $namcu = 7 div 4;
+ my rat $namcu = 7 / 4;
isa_ok($namcu,rat);
}
Index: t/spec/S02-builtin_data_types/type.t
===================================================================
--- t/spec/S02-builtin_data_types/type.t (revision 28143)
+++ t/spec/S02-builtin_data_types/type.t (working copy)
@@ -118,8 +118,8 @@
{
# the following two are the same type of behavior
# S02: "It is possible for the of type to disagree with the as type"
- my Rat sub returntype4 ($pass) as Num {$pass ?? 11 div 10 !! 1}
- my sub returntype5 ($pass --> Rat) as Num {$pass ?? 11 div 5 !! 2}
+ my Rat sub returntype4 ($pass) as Num {$pass ?? 11 / 10 !! 1}
+ my sub returntype5 ($pass --> Rat) as Num {$pass ?? 11 / 5 !! 2}
is(returntype4(True), 1.1, 'good return value works (my Type sub as OtherType)');
eval_dies_ok('returntype4(False)', 'bad return value dies (my Type sub as OtherType)');
Index: t/spec/S32-temporal/Temporal.t
===================================================================
--- t/spec/S32-temporal/Temporal.t (revision 28143)
+++ t/spec/S32-temporal/Temporal.t (working copy)
@@ -31,7 +31,7 @@
'2000-01-01 00:00:00 was on a Saturday in January'; # test 6
$t = time;
-$d = (int($t/86400) + 4) % 7 + 1;
+$d = (int($t / 86400) + 4) % 7 + 1;
$g1 = Time.gmtime($t);
$g2 = Time.gmtime; # $g1 and $g2 might differ very occasionally
ok $g1.date.year ==$g2.date.year && $g1.date.month ==$g2.date.month &&
@@ -85,9 +85,9 @@
# An independent calculation to cross check the Temporal algorithms.
sub test_gmtime( Num $t is copy ) {
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst);
- $sec = int($t) % 60; $t = int($t/60); # $t is now epoch minutes
- $min = $t % 60; $t = int($t/60); # $t is now epoch hours
- $hour = $t % 24; $t = int($t/24); # $t is now epoch days
+ $sec = int($t) % 60; $t = int($t / 60); # $t is now epoch minutes
+ $min = $t % 60; $t = int($t div 60); # $t is now epoch hours
+ $hour = $t % 24; $t = int($t div 24); # $t is now epoch days
# Not a sophisticated or fast algorithm, just an understandable one
# only valid from 1970-01-01 until 2100-02-28
$wday = ($t+4) % 7; # 1970-01-01 was a Thursday
Index: t/spec/S03-operators/assign.t
===================================================================
--- t/spec/S03-operators/assign.t (revision 28143)
+++ t/spec/S03-operators/assign.t (working copy)
@@ -297,10 +297,10 @@
{
my $x = 6;
- @p = $x /= 3, 4;
- is($x, 2, '/= operator');
- is(@p[0],2, "/= operator parses as item assignment 1");
- is(@p[1],4, "/= operator parses as item assignment 2");
+ @p = $x div= 3, 4;
+ is($x, 2, 'div= operator');
+ is(@p[0],2, "div= operator parses as item assignment 1");
+ is(@p[1],4, "div= operator parses as item assignment 2");
}
{
Index: t/spec/S03-operators/ternary.t
===================================================================
--- t/spec/S03-operators/ternary.t (revision 28143)
+++ t/spec/S03-operators/ternary.t (working copy)
@@ -21,7 +21,7 @@
}
is(($str2 eq $str1 ?? 8 * 8 !! 9 * 9), 64, "?? !! in parenthesis");
-is(($str2 eq $str3 ?? 8 + 8 !! 9 / 9), 1, "?? !! in parenthesis");
+is(($str2 eq $str3 ?? 8 + 8 !! 9 div 9), 1, "?? !! in parenthesis");
is(1 ?? 2 ?? 3 !! 4 !! 5 ?? 6 !! 7, 3, "nested ?? !!");
is(1 ?? 0 ?? 3 !! 4 !! 5 ?? 6 !! 7, 4, "nested ?? !!");
Index: t/spec/S03-operators/hyper.t
===================================================================
--- t/spec/S03-operators/hyper.t (revision 28143)
+++ t/spec/S03-operators/hyper.t (working copy)
@@ -35,7 +35,7 @@
@e = ((1,1,1), (2,2), (3));
is(~@r, ~@e, "hyper-xx two arrays");
- @r = (20, 40, 60) ��/�� (2, 5, 10);
+ @r = (20, 40, 60) ��div�� (2, 5, 10);
@e = (10, 8, 6);
is(~@r, ~@e, "hyper-divide two arrays");
@@ -65,7 +65,7 @@
@e = ((1,1,1), (2,2), (3));
is(~@r, ~@e, "hyper-xx two arrays ASCII notation");
- @r = (20, 40, 60) >>/<< (2, 5, 10);
+ @r = (20, 40, 60) >>div<< (2, 5, 10);
@e = (10, 8, 6);
is(~@r, ~@e, "hyper-divide two arrays ASCII notation");
Index: t/spec/S03-operators/basic-types.t
===================================================================
--- t/spec/S03-operators/basic-types.t (revision 28143)
+++ t/spec/S03-operators/basic-types.t (working copy)
@@ -64,7 +64,7 @@
my $float = 0.5;
isa_ok($float, Num, 'it is an Num type');
-isa_ok(1 div 4, Rat, 'infix:<div> produces a Rat');
+isa_ok(1 / 4, Rat, 'infix:</> of integers produces a Rat');
my $string = "Hello World";
isa_ok($string, Str, 'it is a Str type');
Index: t/spec/S03-operators/arith.t
===================================================================
--- t/spec/S03-operators/arith.t (revision 28143)
+++ t/spec/S03-operators/arith.t (working copy)
@@ -263,15 +263,15 @@
# divide
-tryeq 28/14, 2;
-tryeq 28/-7, -4;
-tryeq -28/4, -7;
-tryeq -28/-2, 14;
+tryeq 28 div 14, 2;
+tryeq 28 div -7, -4;
+tryeq -28 div 4, -7;
+tryeq -28 div -2, 14;
-tryeq 0x80000000/1, 0x80000000;
-tryeq 0x80000000/-1, -0x80000000;
-tryeq -0x80000000/1, -0x80000000;
-tryeq -0x80000000/-1, 0x80000000;
+tryeq 0x80000000 div 1, 0x80000000;
+tryeq 0x80000000 div -1, -0x80000000;
+tryeq -0x80000000 div 1, -0x80000000;
+tryeq -0x80000000 div -1, 0x80000000;
# The example for sloppy divide, rigged to avoid the peephole optimiser.
is_approx "20." / "5.", 4;
@@ -283,12 +283,12 @@
# Bluuurg if your floating point can't accurately cope with powers of 2
# [I suspect this is parsing string-to-float problems, not actual arith]
-is 18446744073709551616/1, 18446744073709551616; # Bluuurg
+is 18446744073709551616 div 1, 18446744073709551616; # Bluuurg
{
- tryeq_sloppy 18446744073709551616/2, 9223372036854775808;
- tryeq_sloppy 18446744073709551616/4294967296, 4294967296;
- tryeq_sloppy 18446744073709551616/9223372036854775808, 2;
+ tryeq_sloppy 18446744073709551616 div 2, 9223372036854775808;
+ tryeq_sloppy 18446744073709551616 div 4294967296, 4294967296;
+ tryeq_sloppy 18446744073709551616 div 9223372036854775808, 2;
}
{
@@ -323,7 +323,7 @@
{
is_approx(-1, (0 + 1i)**2, "i^2 == -1");
is_approx(-1, (0.7071067811865476 + -0.7071067811865475i)**4, "sqrt(-i)**4 ==-1" );
- is_approx(1i, (-1+0i)**(1/2), '(-1+0i)**(1/2) == i ');
+ is_approx(1i, (-1+0i)**(1 div 2), '(-1+0i)**(1/2) == i ');
}
{
@@ -335,16 +335,16 @@
is Inf+100, Inf;
is Inf-100, Inf;
is Inf*100, Inf;
- is Inf/100, Inf;
+ is Inf / 100, Inf;
is Inf*-100, -Inf;
- is Inf/-100, -Inf;
- is 100/Inf, 0;
+ is Inf / -100, -Inf;
+ is 100 / Inf, 0;
is Inf**100, Inf;
is Inf*0, NaN;
is Inf - Inf, NaN;
is Inf*Inf, Inf;
- is Inf/Inf, NaN;
- is Inf*Inf/Inf, NaN;
+ is Inf / Inf, NaN;
+ is Inf*Inf / Inf, NaN;
is Inf**0, 1;
is 0**0, 1;
is 0**Inf, 0;
@@ -373,18 +373,18 @@
is NaN+100, NaN;
is NaN-100, NaN;
is NaN*100, NaN;
- is NaN/100, NaN;
+ is NaN / 100, NaN;
is NaN**100, NaN;
is NaN+NaN, NaN;
is NaN - NaN, NaN;
is NaN*NaN, NaN;
- is NaN/NaN, NaN;
+ is NaN / NaN, NaN;
is NaN+Inf, NaN;
is NaN - Inf, NaN;
is NaN*Inf, NaN;
- is NaN/Inf, NaN;
- is Inf/NaN, NaN;
+ is NaN / Inf, NaN;
+ is Inf / NaN, NaN;
my $nan1 = NaN**NaN;
is $nan1, NaN, "NaN**NaN";
@@ -413,9 +413,9 @@
dies_ok( { $x = 0; say 3 % $x; }, 'Modulo zero dies and is catchable with VInt/VRat variables');
dies_ok( { $x := 0; say 3 % $x; }, 'Modulo zero dies and is catchable with VRef variables');
-dies_ok( { say 3 / 0 }, 'Division by zero dies and is catchable');
-dies_ok( { $x = 0; say 3 / $x; }, 'Division by zero dies and is catchable with VInt/VRat variables');
-dies_ok( { $x := 0; say 3 / $x; }, 'Division by zero dies and is catchable with VRef variables');
+dies_ok( { say 3 div 0 }, 'Division by zero dies and is catchable');
+dies_ok( { $x = 0; say 3 div $x; }, 'Division by zero dies and is catchable with VInt div VRat variables');
+dies_ok( { $x := 0; say 3 div $x; }, 'Division by zero dies and is catchable with VRef variables');
# This is a rakudo regression wrt bignum:
#?rakudo skip 'bigint'
Index: t/spec/integration/99problems-31-to-40.t
===================================================================
--- t/spec/integration/99problems-31-to-40.t (revision 28143)
+++ t/spec/integration/99problems-31-to-40.t (working copy)
@@ -147,7 +147,7 @@
while $n > 1 {
if $n % $cond == 0 {
$count++;
- $n /= $cond;
+ $n div= $cond;
}
else {
if $count > 0 {
Index: t/spec/integration/99problems-51-to-60.t
===================================================================
--- t/spec/integration/99problems-51-to-60.t (revision 28143)
+++ t/spec/integration/99problems-51-to-60.t (working copy)
@@ -56,14 +56,14 @@
return undef if $n == 0;
gather {
if $n % 2 == 1 {
- my $k = ($n - 1) / 2;
+ my $k = ($n - 1) div 2;
for cbal-tree($k) -> $a {
for cbal-tree($k) -> $b {
take ['x', $a, $b];
}
}
} else {
- my $k = $n / 2;
+ my $k = $n div 2;
for cbal-tree($k) -> $a {
for cbal-tree($k - 1) -> $b {
take ['x', $a, $b];
Index: t/spec/S32-num/rat.t
===================================================================
--- t/spec/S32-num/rat.t (revision 28143)
+++ t/spec/S32-num/rat.t (working copy)
@@ -18,12 +18,14 @@
is(~(Rat.new(0,33)), "0/1", "Reduce to simplest form in constructor");
# Test basic math
-is(~(1 div 4 + 1 div 4), "1/2", "1/4 + 1/4 = 1/2");
-is(~(1 div 4 + 2 div 7), "15/28", "1/4 + 2/7 = 15/28");
-is(~(1 div 4 + 1), "5/4", "1/4 + 1 = 5/4");
-is(~(1 + 1 div 4), "5/4", "1 + 1/4 = 5/4");
-is(~(1 div 4 - 1 div 4), "0/1", "1/4 - 1/4 = 0/1");
-is(~(1 div 4 - 3 div 4), "-1/2", "1/4 - 3/4 = -1/2");
+is(~(1 / 4 + 1 / 4), "1/2", "1/4 + 1/4 = 1/2");
+is(~(1 / 4 + 2 / 7), "15/28", "1/4 + 2/7 = 15/28");
+is(~(1 / 4 + 1), "5/4", "1/4 + 1 = 5/4");
+is(~(1 + 1 / 4), "5/4", "1 + 1/4 = 5/4");
+is(~(1 / 4 - 1 / 4), "0/1", "1/4 - 1/4 = 0/1");
+is(~(1 / 4 - 3 / 4), "-1/2", "1/4 - 3/4 = -1/2");
+is(~(1 / 4 - 1), "-3/4", "1/4 - 1 = -3/4");
+is(~(1 - 1 / 4), "3/4", "1 - 1/4 = 3/4");
done_testing;
Index: t/spec/S02-literals/radix.t
===================================================================
--- t/spec/S02-literals/radix.t (revision 28143)
+++ t/spec/S02-literals/radix.t (working copy)
@@ -106,7 +106,7 @@
# L<S02/Literals/"Any radix may include a fractional part">
#?rakudo todo 'fractionals base 16'
-is(:16<dead_beef.face>, 0xDEAD_BEEF + 0xFACE / ( 16 ** 4 ), 'Fractional base 16 works' );
+is(:16<dead_beef.face>, 0xDEAD_BEEF + 0xFACE div ( 16 ** 4 ), 'Fractional base 16 works' );
# L<S02/Literals/":8<177777>">
@@ -225,7 +225,7 @@
is +":1_0<14_56>", 1456, "underscore seperators works";
is +":10<123.456>", 123.456, "base 10 decimal notation works";
is +":2<1.111>", 1.875, "base 2 decimal notation works";
- is +":16<dead_beef.face>", 0xDEADBEEF + 0xFACE / (16 ** 4), "fractional base 16 works";
+ is +":16<dead_beef.face>", 0xDEADBEEF + 0xFACE div (16 ** 4), "fractional base 16 works";
for 2..36 {
is +":{$_}<11>", $_ + 1, "stringified form of base $_ works";
Index: t/spec/S02-magicals/dollar_bang.t
===================================================================
--- t/spec/S02-magicals/dollar_bang.t (revision 28143)
+++ t/spec/S02-magicals/dollar_bang.t (working copy)
@@ -34,7 +34,7 @@
ok !$called, 'The subroutine also was not called';
undefine $!;
-try { 1 / 0 };
+try { 1 div 0 };
ok $!, 'Dividing one by zero sets $!';
sub incr ( $a is rw ) { $a++ };
|
From @moritzOn Mon Aug 31 16:11:04 2009, colomon@gmail.com wrote:
I've attached two patches (Rakudo + spectest) that change infix:<div> to
Still the same with my patch.
Well, we can change that later on again, but I don't think it's high Cheers, |
From @moritzspectest-changes-needed-to-swap-the-meanings-of-div-and-as.patchIndex: t/spec/S02-builtin_data_types/num.t
===================================================================
--- t/spec/S02-builtin_data_types/num.t (revision 28143)
+++ t/spec/S02-builtin_data_types/num.t (working copy)
@@ -19,7 +19,7 @@
#L<S02/Built-In Data Types/Rat supports arbitrary precision rational arithmetic>
{
- my $a = 1 div 1;
+ my $a = 1 / 1;
isa_ok($a, Rat);
is($a, "1/1", '1.0 stringification works');
}
@@ -91,7 +91,7 @@
}
#L<S02/Built-In Data Types/Rat supports arbitrary precision rational arithmetic>
-isa_ok(1 div 1, Rat);
+isa_ok(1 / 1, Rat);
{
my $a = 80000.0000000000000000000000000;
Index: t/spec/S02-builtin_data_types/declare.t
===================================================================
--- t/spec/S02-builtin_data_types/declare.t (revision 28143)
+++ t/spec/S02-builtin_data_types/declare.t (working copy)
@@ -29,7 +29,7 @@
}
{
- my Rat $namcu = 7 div 4;
+ my Rat $namcu = 7 / 4;
isa_ok($namcu,Rat);
}
@@ -148,7 +148,7 @@
#?rakudo skip 'rat not implemented'
{
- my rat $namcu = 7 div 4;
+ my rat $namcu = 7 / 4;
isa_ok($namcu,rat);
}
Index: t/spec/S02-builtin_data_types/type.t
===================================================================
--- t/spec/S02-builtin_data_types/type.t (revision 28143)
+++ t/spec/S02-builtin_data_types/type.t (working copy)
@@ -118,8 +118,8 @@
{
# the following two are the same type of behavior
# S02: "It is possible for the of type to disagree with the as type"
- my Rat sub returntype4 ($pass) as Num {$pass ?? 11 div 10 !! 1}
- my sub returntype5 ($pass --> Rat) as Num {$pass ?? 11 div 5 !! 2}
+ my Rat sub returntype4 ($pass) as Num {$pass ?? 11 / 10 !! 1}
+ my sub returntype5 ($pass --> Rat) as Num {$pass ?? 11 / 5 !! 2}
is(returntype4(True), 1.1, 'good return value works (my Type sub as OtherType)');
eval_dies_ok('returntype4(False)', 'bad return value dies (my Type sub as OtherType)');
Index: t/spec/S32-temporal/Temporal.t
===================================================================
--- t/spec/S32-temporal/Temporal.t (revision 28143)
+++ t/spec/S32-temporal/Temporal.t (working copy)
@@ -31,7 +31,7 @@
'2000-01-01 00:00:00 was on a Saturday in January'; # test 6
$t = time;
-$d = (int($t/86400) + 4) % 7 + 1;
+$d = (int($t / 86400) + 4) % 7 + 1;
$g1 = Time.gmtime($t);
$g2 = Time.gmtime; # $g1 and $g2 might differ very occasionally
ok $g1.date.year ==$g2.date.year && $g1.date.month ==$g2.date.month &&
@@ -85,9 +85,9 @@
# An independent calculation to cross check the Temporal algorithms.
sub test_gmtime( Num $t is copy ) {
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst);
- $sec = int($t) % 60; $t = int($t/60); # $t is now epoch minutes
- $min = $t % 60; $t = int($t/60); # $t is now epoch hours
- $hour = $t % 24; $t = int($t/24); # $t is now epoch days
+ $sec = int($t) % 60; $t = int($t / 60); # $t is now epoch minutes
+ $min = $t % 60; $t = int($t div 60); # $t is now epoch hours
+ $hour = $t % 24; $t = int($t div 24); # $t is now epoch days
# Not a sophisticated or fast algorithm, just an understandable one
# only valid from 1970-01-01 until 2100-02-28
$wday = ($t+4) % 7; # 1970-01-01 was a Thursday
Index: t/spec/S03-operators/assign.t
===================================================================
--- t/spec/S03-operators/assign.t (revision 28143)
+++ t/spec/S03-operators/assign.t (working copy)
@@ -297,10 +297,10 @@
{
my $x = 6;
- @p = $x /= 3, 4;
- is($x, 2, '/= operator');
- is(@p[0],2, "/= operator parses as item assignment 1");
- is(@p[1],4, "/= operator parses as item assignment 2");
+ @p = $x div= 3, 4;
+ is($x, 2, 'div= operator');
+ is(@p[0],2, "div= operator parses as item assignment 1");
+ is(@p[1],4, "div= operator parses as item assignment 2");
}
{
Index: t/spec/S03-operators/ternary.t
===================================================================
--- t/spec/S03-operators/ternary.t (revision 28143)
+++ t/spec/S03-operators/ternary.t (working copy)
@@ -21,7 +21,7 @@
}
is(($str2 eq $str1 ?? 8 * 8 !! 9 * 9), 64, "?? !! in parenthesis");
-is(($str2 eq $str3 ?? 8 + 8 !! 9 / 9), 1, "?? !! in parenthesis");
+is(($str2 eq $str3 ?? 8 + 8 !! 9 div 9), 1, "?? !! in parenthesis");
is(1 ?? 2 ?? 3 !! 4 !! 5 ?? 6 !! 7, 3, "nested ?? !!");
is(1 ?? 0 ?? 3 !! 4 !! 5 ?? 6 !! 7, 4, "nested ?? !!");
Index: t/spec/S03-operators/hyper.t
===================================================================
--- t/spec/S03-operators/hyper.t (revision 28143)
+++ t/spec/S03-operators/hyper.t (working copy)
@@ -35,7 +35,7 @@
@e = ((1,1,1), (2,2), (3));
is(~@r, ~@e, "hyper-xx two arrays");
- @r = (20, 40, 60) »/« (2, 5, 10);
+ @r = (20, 40, 60) »div« (2, 5, 10);
@e = (10, 8, 6);
is(~@r, ~@e, "hyper-divide two arrays");
@@ -65,7 +65,7 @@
@e = ((1,1,1), (2,2), (3));
is(~@r, ~@e, "hyper-xx two arrays ASCII notation");
- @r = (20, 40, 60) >>/<< (2, 5, 10);
+ @r = (20, 40, 60) >>div<< (2, 5, 10);
@e = (10, 8, 6);
is(~@r, ~@e, "hyper-divide two arrays ASCII notation");
Index: t/spec/S03-operators/basic-types.t
===================================================================
--- t/spec/S03-operators/basic-types.t (revision 28143)
+++ t/spec/S03-operators/basic-types.t (working copy)
@@ -64,7 +64,7 @@
my $float = 0.5;
isa_ok($float, Num, 'it is an Num type');
-isa_ok(1 div 4, Rat, 'infix:<div> produces a Rat');
+isa_ok(1 / 4, Rat, 'infix:</> of integers produces a Rat');
my $string = "Hello World";
isa_ok($string, Str, 'it is a Str type');
Index: t/spec/S03-operators/arith.t
===================================================================
--- t/spec/S03-operators/arith.t (revision 28143)
+++ t/spec/S03-operators/arith.t (working copy)
@@ -263,15 +263,15 @@
# divide
-tryeq 28/14, 2;
-tryeq 28/-7, -4;
-tryeq -28/4, -7;
-tryeq -28/-2, 14;
+tryeq 28 div 14, 2;
+tryeq 28 div -7, -4;
+tryeq -28 div 4, -7;
+tryeq -28 div -2, 14;
-tryeq 0x80000000/1, 0x80000000;
-tryeq 0x80000000/-1, -0x80000000;
-tryeq -0x80000000/1, -0x80000000;
-tryeq -0x80000000/-1, 0x80000000;
+tryeq 0x80000000 div 1, 0x80000000;
+tryeq 0x80000000 div -1, -0x80000000;
+tryeq -0x80000000 div 1, -0x80000000;
+tryeq -0x80000000 div -1, 0x80000000;
# The example for sloppy divide, rigged to avoid the peephole optimiser.
is_approx "20." / "5.", 4;
@@ -283,12 +283,12 @@
# Bluuurg if your floating point can't accurately cope with powers of 2
# [I suspect this is parsing string-to-float problems, not actual arith]
-is 18446744073709551616/1, 18446744073709551616; # Bluuurg
+is 18446744073709551616 div 1, 18446744073709551616; # Bluuurg
{
- tryeq_sloppy 18446744073709551616/2, 9223372036854775808;
- tryeq_sloppy 18446744073709551616/4294967296, 4294967296;
- tryeq_sloppy 18446744073709551616/9223372036854775808, 2;
+ tryeq_sloppy 18446744073709551616 div 2, 9223372036854775808;
+ tryeq_sloppy 18446744073709551616 div 4294967296, 4294967296;
+ tryeq_sloppy 18446744073709551616 div 9223372036854775808, 2;
}
{
@@ -323,7 +323,7 @@
{
is_approx(-1, (0 + 1i)**2, "i^2 == -1");
is_approx(-1, (0.7071067811865476 + -0.7071067811865475i)**4, "sqrt(-i)**4 ==-1" );
- is_approx(1i, (-1+0i)**(1/2), '(-1+0i)**(1/2) == i ');
+ is_approx(1i, (-1+0i)**(1 div 2), '(-1+0i)**(1/2) == i ');
}
{
@@ -335,16 +335,16 @@
is Inf+100, Inf;
is Inf-100, Inf;
is Inf*100, Inf;
- is Inf/100, Inf;
+ is Inf / 100, Inf;
is Inf*-100, -Inf;
- is Inf/-100, -Inf;
- is 100/Inf, 0;
+ is Inf / -100, -Inf;
+ is 100 / Inf, 0;
is Inf**100, Inf;
is Inf*0, NaN;
is Inf - Inf, NaN;
is Inf*Inf, Inf;
- is Inf/Inf, NaN;
- is Inf*Inf/Inf, NaN;
+ is Inf / Inf, NaN;
+ is Inf*Inf / Inf, NaN;
is Inf**0, 1;
is 0**0, 1;
is 0**Inf, 0;
@@ -373,18 +373,18 @@
is NaN+100, NaN;
is NaN-100, NaN;
is NaN*100, NaN;
- is NaN/100, NaN;
+ is NaN / 100, NaN;
is NaN**100, NaN;
is NaN+NaN, NaN;
is NaN - NaN, NaN;
is NaN*NaN, NaN;
- is NaN/NaN, NaN;
+ is NaN / NaN, NaN;
is NaN+Inf, NaN;
is NaN - Inf, NaN;
is NaN*Inf, NaN;
- is NaN/Inf, NaN;
- is Inf/NaN, NaN;
+ is NaN / Inf, NaN;
+ is Inf / NaN, NaN;
my $nan1 = NaN**NaN;
is $nan1, NaN, "NaN**NaN";
@@ -413,9 +413,9 @@
dies_ok( { $x = 0; say 3 % $x; }, 'Modulo zero dies and is catchable with VInt/VRat variables');
dies_ok( { $x := 0; say 3 % $x; }, 'Modulo zero dies and is catchable with VRef variables');
-dies_ok( { say 3 / 0 }, 'Division by zero dies and is catchable');
-dies_ok( { $x = 0; say 3 / $x; }, 'Division by zero dies and is catchable with VInt/VRat variables');
-dies_ok( { $x := 0; say 3 / $x; }, 'Division by zero dies and is catchable with VRef variables');
+dies_ok( { say 3 div 0 }, 'Division by zero dies and is catchable');
+dies_ok( { $x = 0; say 3 div $x; }, 'Division by zero dies and is catchable with VInt div VRat variables');
+dies_ok( { $x := 0; say 3 div $x; }, 'Division by zero dies and is catchable with VRef variables');
# This is a rakudo regression wrt bignum:
#?rakudo skip 'bigint'
Index: t/spec/integration/99problems-31-to-40.t
===================================================================
--- t/spec/integration/99problems-31-to-40.t (revision 28143)
+++ t/spec/integration/99problems-31-to-40.t (working copy)
@@ -147,7 +147,7 @@
while $n > 1 {
if $n % $cond == 0 {
$count++;
- $n /= $cond;
+ $n div= $cond;
}
else {
if $count > 0 {
Index: t/spec/integration/99problems-51-to-60.t
===================================================================
--- t/spec/integration/99problems-51-to-60.t (revision 28143)
+++ t/spec/integration/99problems-51-to-60.t (working copy)
@@ -56,14 +56,14 @@
return undef if $n == 0;
gather {
if $n % 2 == 1 {
- my $k = ($n - 1) / 2;
+ my $k = ($n - 1) div 2;
for cbal-tree($k) -> $a {
for cbal-tree($k) -> $b {
take ['x', $a, $b];
}
}
} else {
- my $k = $n / 2;
+ my $k = $n div 2;
for cbal-tree($k) -> $a {
for cbal-tree($k - 1) -> $b {
take ['x', $a, $b];
Index: t/spec/S32-num/rat.t
===================================================================
--- t/spec/S32-num/rat.t (revision 28143)
+++ t/spec/S32-num/rat.t (working copy)
@@ -18,12 +18,14 @@
is(~(Rat.new(0,33)), "0/1", "Reduce to simplest form in constructor");
# Test basic math
-is(~(1 div 4 + 1 div 4), "1/2", "1/4 + 1/4 = 1/2");
-is(~(1 div 4 + 2 div 7), "15/28", "1/4 + 2/7 = 15/28");
-is(~(1 div 4 + 1), "5/4", "1/4 + 1 = 5/4");
-is(~(1 + 1 div 4), "5/4", "1 + 1/4 = 5/4");
-is(~(1 div 4 - 1 div 4), "0/1", "1/4 - 1/4 = 0/1");
-is(~(1 div 4 - 3 div 4), "-1/2", "1/4 - 3/4 = -1/2");
+is(~(1 / 4 + 1 / 4), "1/2", "1/4 + 1/4 = 1/2");
+is(~(1 / 4 + 2 / 7), "15/28", "1/4 + 2/7 = 15/28");
+is(~(1 / 4 + 1), "5/4", "1/4 + 1 = 5/4");
+is(~(1 + 1 / 4), "5/4", "1 + 1/4 = 5/4");
+is(~(1 / 4 - 1 / 4), "0/1", "1/4 - 1/4 = 0/1");
+is(~(1 / 4 - 3 / 4), "-1/2", "1/4 - 3/4 = -1/2");
+is(~(1 / 4 - 1), "-3/4", "1/4 - 1 = -3/4");
+is(~(1 - 1 / 4), "3/4", "1 - 1/4 = 3/4");
done_testing;
Index: t/spec/S02-literals/radix.t
===================================================================
--- t/spec/S02-literals/radix.t (revision 28143)
+++ t/spec/S02-literals/radix.t (working copy)
@@ -106,7 +106,7 @@
# L<S02/Literals/"Any radix may include a fractional part">
#?rakudo todo 'fractionals base 16'
-is(:16<dead_beef.face>, 0xDEAD_BEEF + 0xFACE / ( 16 ** 4 ), 'Fractional base 16 works' );
+is(:16<dead_beef.face>, 0xDEAD_BEEF + 0xFACE div ( 16 ** 4 ), 'Fractional base 16 works' );
# L<S02/Literals/":8<177777>">
@@ -225,7 +225,7 @@
is +":1_0<14_56>", 1456, "underscore seperators works";
is +":10<123.456>", 123.456, "base 10 decimal notation works";
is +":2<1.111>", 1.875, "base 2 decimal notation works";
- is +":16<dead_beef.face>", 0xDEADBEEF + 0xFACE / (16 ** 4), "fractional base 16 works";
+ is +":16<dead_beef.face>", 0xDEADBEEF + 0xFACE div (16 ** 4), "fractional base 16 works";
for 2..36 {
is +":{$_}<11>", $_ + 1, "stringified form of base $_ works";
Index: t/spec/S02-magicals/dollar_bang.t
===================================================================
--- t/spec/S02-magicals/dollar_bang.t (revision 28143)
+++ t/spec/S02-magicals/dollar_bang.t (working copy)
@@ -34,7 +34,7 @@
ok !$called, 'The subroutine also was not called';
undefine $!;
-try { 1 / 0 };
+try { 1 div 0 };
ok $!, 'Dividing one by zero sets $!';
sub incr ( $a is rw ) { $a++ };
|
From @moritz0001-Swap-the-meanings-of-div-and-as-per-latest-S03.patchFrom f76db69d1d0b7de691dbc4ba9faf51650475d36c Mon Sep 17 00:00:00 2001
From: Solomon Foster <colomon@gmail.com>
Date: Mon, 31 Aug 2009 19:02:28 -0400
Subject: [PATCH] Swap the meanings of div and /, as per latest S03.
Make infix:<div> use integer semantics.
Contains some cleanups by Moritz
Signed-off-by: Moritz Lenz <moritz@faui2k3.org>
---
CREDITS | 4 ++++
build/gen_metaop_pir.pl | 2 ++
src/setting/Int.pm | 17 +++++------------
src/setting/Rat.pm | 12 +++++++++---
src/setting/Temporal.pm | 38 +++++++++++++++++++-------------------
5 files changed, 39 insertions(+), 34 deletions(-)
diff --git a/CREDITS b/CREDITS
index 3a4858a..97a5c54 100644
--- a/CREDITS
+++ b/CREDITS
@@ -296,6 +296,10 @@ N: Simon Cozens
U: simon
E: simon@simon-cozens.org
+N: Solomon Foster
+U: colomon
+E: colomon@gmail.com
+
N: Stéphane Payrard
D: Various code fixes and improvements
diff --git a/build/gen_metaop_pir.pl b/build/gen_metaop_pir.pl
index dc47087..92bf23d 100644
--- a/build/gen_metaop_pir.pl
+++ b/build/gen_metaop_pir.pl
@@ -9,6 +9,8 @@ my @ops = qw(
** 1 op
* 1 op
/ 'fail' op
+ div 'fail' op
+ mod 'fail' op
% 'fail' op
x 'fail' op
xx 'fail' op
diff --git a/src/setting/Int.pm b/src/setting/Int.pm
index 27bbcd7..2962758 100644
--- a/src/setting/Int.pm
+++ b/src/setting/Int.pm
@@ -49,21 +49,14 @@ multi sub infix:<*>(Int $a, Int $b) {
}
}
-multi sub infix:</>(Int $a, Int $b) {
+multi sub infix:<div>(Int $a, Int $b) {
Q:PIR {
$P0 = find_lex '$a'
- $N0 = $P0
+ $I0 = $P0
$P1 = find_lex '$b'
- $N1 = $P1
- $N2 = $N0 / $N1
- $I2 = floor $N2
- $N3 = $N2 - $I2
- if $N3 != 0 goto notint
- %r = '!upgrade_to_num_if_needed'($N2)
- goto done
- notint:
- %r = box $N2
- done:
+ $I1 = $P1
+ $I2 = $I0 / $I1
+ %r = box $I2
}
}
diff --git a/src/setting/Rat.pm b/src/setting/Rat.pm
index 9f34f9d..ba73dac 100644
--- a/src/setting/Rat.pm
+++ b/src/setting/Rat.pm
@@ -20,10 +20,12 @@ class Rat {
$denominator = -$denominator;
}
my $gcd = gcd($numerator, $denominator);
- $numerator /= $gcd;
- $denominator /= $gcd;
+ $numerator = $numerator div $gcd;
+ $denominator = $denominator div $gcd;
self.bless(*, :$numerator, :$denominator);
}
+
+ multi method perl() { "Rat.new($!numerator, $!denominator)"; }
multi method Str() { "$!numerator/$!denominator"; }
@@ -52,6 +54,10 @@ multi sub infix:<->(Rat $a, Int $b) {
Rat.new($a.numerator - $b * $a.denominator, $a.denominator);
}
+multi sub infix:<->(Int $a, Rat $b) {
+ Rat.new($a * $b.denominator - $b.numerator, $b.denominator);
+}
+
multi sub infix:<*>(Rat $a, Rat $b) {
Rat.new($a.numerator * $b.numerator, $a.denominator * $b.denominator);
}
@@ -60,7 +66,7 @@ multi sub infix:</>(Rat $a, Rat $b) {
Rat.new($a.numerator * $b.denominator, $a.denominator * $b.numerator);
}
-multi sub infix:<div>(Int $a, Int $b) {
+multi sub infix:</>(Int $a, Int $b) {
Rat.new($a, $b);
}
diff --git a/src/setting/Temporal.pm b/src/setting/Temporal.pm
index c7fd3df..149c210 100644
--- a/src/setting/Temporal.pm
+++ b/src/setting/Temporal.pm
@@ -17,11 +17,11 @@ role Temporal::Date {
method day-of-week { # returns DayOfWeek {
my ( $a, $y, $m, $jd ); # algorithm from Claus Tøndering
- $a = int((14 - $.month) / 12 );
+ $a = (14 - $.month) div 12;
$y = $.year + 4800 - $a;
$m = $.month + 12 * $a - 3;
- $jd = $.day + int((153 * $m + 2) / 5) + 365 * $y + int( $y / 4 )
- - int( $y / 100 ) + int( $y / 400 ) - 32045;
+ $jd = $.day + (153 * $m + 2) div 5 + 365 * $y + $y div 4
+ - $y div 100 + $y div 400 - 32045;
return ($jd + 1) % 7 + 1;
}
@@ -90,8 +90,8 @@ role Temporal::TimeZone::Observance {
# The ISO8601 standard does not allow for offsets with sub-minute
# resolutions. In real-world practice, this is not an issue.
our Str method iso8601 {
- sprintf "%+03d%02d", self.offset / 3600,
- int( abs(self.offset) / 60 ) % 60;
+ sprintf "%+03d%02d", self.offset div 3600,
+ int( abs(self.offset) div 60 ) % 60;
}
method Str { self.iso8601 }
@@ -117,11 +117,11 @@ role Temporal::DateTime {
# This involves a whole bunch of code - see Perl 5's Time::Local
our Num method epoch {
my ( $a, $y, $m, $jd ); # algorithm from Claus Tøndering
- $a = int((14 - $.date.month) / 12 );
+ $a = (14 - $.date.month) div 12;
$y = $.date.year + 4800 - $a;
$m = $.date.month + 12 * $a - 3;
- $jd = $.date.day + int((153 * $m + 2) / 5) + 365 * $y
- + int( $y / 4 ) - int( $y / 100 ) + int( $y / 400 ) - 32045;
+ $jd = $.date.day + (153 * $m + 2) div 5 + 365 * $y
+ + $y div 4 - $y div 100 + $y div 400 - 32045;
return ($jd - 2440588) * 24 * 60 * 60
+ ($.time.hour*60 + $.time.minute)*60 + $.time.second;
}
@@ -136,21 +136,21 @@ class Time {
our method gmtime( Num $epoch = time ) {
my ( $time, $second, $minute, $hour, $day, $month, $year );
$time = int( $epoch );
- $second = $time % 60; $time = int($time/60);
- $minute = $time % 60; $time = int($time/60);
- $hour = $time % 24; $time = int($time/24);
+ $second = $time % 60; $time = $time div 60;
+ $minute = $time % 60; $time = $time div 60;
+ $hour = $time % 24; $time = $time div 24;
# Day month and leap year arithmetic, based on Gregorian day #.
# 2000-01-01 noon UTC == 2451558.0 Julian == 2451545.0 Gregorian
$time += 2440588; # because 2000-01-01 == Unix epoch day 10957
my $a = $time + 32044; # date algorithm from Claus Tøndering
- my $b = int((4 * $a + 3) / 146097); # 146097 = days in 400 years
- my $c = $a - int(( 146097 * $b ) / 4);
- my $d = int((4 * $c + 3) / 1461); # 1461 = days in 4 years
- my $e = $c - int(($d * 1461) / 4);
- my $m = int((5 * $e + 2) / 153); # 153 = days in Mar-Jul Aug-Dec
- $day = $e - int((153 * $m + 2) / 5 ) + 1;
- $month = $m + 3 - 12 * int( $m / 10 );
- $year = $b * 100 + $d - 4800 + int( $m / 10 );
+ my $b = (4 * $a + 3) div 146097; # 146097 = days in 400 years
+ my $c = $a - ( 146097 * $b ) div 4;
+ my $d = (4 * $c + 3) div 1461; # 1461 = days in 4 years
+ my $e = $c - int(($d * 1461) div 4);
+ my $m = (5 * $e + 2) div 153; # 153 = days in Mar-Jul Aug-Dec
+ $day = $e - (153 * $m + 2) div 5 + 1;
+ $month = $m + 3 - 12 * $m div 10;
+ $year = $b * 100 + $d - 4800 + $m div 10;
Temporal::DateTime.new(
date => Temporal::Date.new(:$year, :$month, :$day),
time => Temporal::Time.new(:$hour, :$minute, :$second),
--
1.5.6.5
|
The RT System itself - Status changed from 'new' to 'open' |
From @colomonHere is an updated version of the patch, with the Temporal.t bug On Mon, Aug 31, 2009 at 7:11 PM, perl6 via
-- |
From @colomon0001-Fix-Rat.perl-to-return-N-M-rather-than-Rat.new-N.patchFrom 9e343fee7d8da6c7126ca08da9aa64766c7b3835 Mon Sep 17 00:00:00 2001
From: Solomon Foster <colomon@gmail.com>
Date: Mon, 31 Aug 2009 21:39:58 -0400
Subject: [PATCH] Fix Rat.perl to return "N/M" rather than "Rat.new(N,M)".
---
src/setting/Rat.pm | 2 +-
1 files changed, 1 insertions(+), 1 deletions(-)
diff --git a/src/setting/Rat.pm b/src/setting/Rat.pm
index ba73dac..06f08f3 100644
--- a/src/setting/Rat.pm
+++ b/src/setting/Rat.pm
@@ -25,7 +25,7 @@ class Rat {
self.bless(*, :$numerator, :$denominator);
}
- multi method perl() { "Rat.new($!numerator, $!denominator)"; }
+ multi method perl() { "$!numerator/$!denominator"; }
multi method Str() { "$!numerator/$!denominator"; }
--
1.6.0.5
|
From @colomon0001-Changes-needed-to-swap-the-meanings-of-div-and-as.patchFrom 5734b0bdf8ebc2da1f9bd6ee1707b709851cd284 Mon Sep 17 00:00:00 2001
From: Solomon Foster <colomon@gmail.com>
Date: Mon, 31 Aug 2009 19:02:28 -0400
Subject: [PATCH] Changes needed to swap the meanings of div and /, as per latest S03. Please
note that the div command in Int.pm almost certainly needs some tweaking to
meet the spec, I have just given it the meaning of the old /, but there are
subtle differences. Also we have one failing test in Temporal.t, leading me to
suspect I messed something up in the Temporal.pm patch.
---
CREDITS | 4 ++++
build/gen_metaop_pir.pl | 2 ++
src/setting/Int.pm | 2 +-
src/setting/Rat.pm | 12 +++++++++---
src/setting/Temporal.pm | 38 +++++++++++++++++++-------------------
5 files changed, 35 insertions(+), 23 deletions(-)
diff --git a/CREDITS b/CREDITS
index 3a4858a..97a5c54 100644
--- a/CREDITS
+++ b/CREDITS
@@ -296,6 +296,10 @@ N: Simon Cozens
U: simon
E: simon@simon-cozens.org
+N: Solomon Foster
+U: colomon
+E: colomon@gmail.com
+
N: St��phane Payrard
D: Various code fixes and improvements
diff --git a/build/gen_metaop_pir.pl b/build/gen_metaop_pir.pl
index dc47087..92bf23d 100644
--- a/build/gen_metaop_pir.pl
+++ b/build/gen_metaop_pir.pl
@@ -9,6 +9,8 @@ my @ops = qw(
** 1 op
* 1 op
/ 'fail' op
+ div 'fail' op
+ mod 'fail' op
% 'fail' op
x 'fail' op
xx 'fail' op
diff --git a/src/setting/Int.pm b/src/setting/Int.pm
index 27bbcd7..864e551 100644
--- a/src/setting/Int.pm
+++ b/src/setting/Int.pm
@@ -49,7 +49,7 @@ multi sub infix:<*>(Int $a, Int $b) {
}
}
-multi sub infix:</>(Int $a, Int $b) {
+multi sub infix:<div>(Int $a, Int $b) {
Q:PIR {
$P0 = find_lex '$a'
$N0 = $P0
diff --git a/src/setting/Rat.pm b/src/setting/Rat.pm
index 9f34f9d..ba73dac 100644
--- a/src/setting/Rat.pm
+++ b/src/setting/Rat.pm
@@ -20,10 +20,12 @@ class Rat {
$denominator = -$denominator;
}
my $gcd = gcd($numerator, $denominator);
- $numerator /= $gcd;
- $denominator /= $gcd;
+ $numerator = $numerator div $gcd;
+ $denominator = $denominator div $gcd;
self.bless(*, :$numerator, :$denominator);
}
+
+ multi method perl() { "Rat.new($!numerator, $!denominator)"; }
multi method Str() { "$!numerator/$!denominator"; }
@@ -52,6 +54,10 @@ multi sub infix:<->(Rat $a, Int $b) {
Rat.new($a.numerator - $b * $a.denominator, $a.denominator);
}
+multi sub infix:<->(Int $a, Rat $b) {
+ Rat.new($a * $b.denominator - $b.numerator, $b.denominator);
+}
+
multi sub infix:<*>(Rat $a, Rat $b) {
Rat.new($a.numerator * $b.numerator, $a.denominator * $b.denominator);
}
@@ -60,7 +66,7 @@ multi sub infix:</>(Rat $a, Rat $b) {
Rat.new($a.numerator * $b.denominator, $a.denominator * $b.numerator);
}
-multi sub infix:<div>(Int $a, Int $b) {
+multi sub infix:</>(Int $a, Int $b) {
Rat.new($a, $b);
}
diff --git a/src/setting/Temporal.pm b/src/setting/Temporal.pm
index c7fd3df..294c194 100644
--- a/src/setting/Temporal.pm
+++ b/src/setting/Temporal.pm
@@ -17,11 +17,11 @@ role Temporal::Date {
method day-of-week { # returns DayOfWeek {
my ( $a, $y, $m, $jd ); # algorithm from Claus T��ndering
- $a = int((14 - $.month) / 12 );
+ $a = int((14 - $.month) div 12 );
$y = $.year + 4800 - $a;
$m = $.month + 12 * $a - 3;
- $jd = $.day + int((153 * $m + 2) / 5) + 365 * $y + int( $y / 4 )
- - int( $y / 100 ) + int( $y / 400 ) - 32045;
+ $jd = $.day + int((153 * $m + 2) div 5) + 365 * $y + int( $y div 4 )
+ - int( $y div 100 ) + int( $y div 400 ) - 32045;
return ($jd + 1) % 7 + 1;
}
@@ -90,8 +90,8 @@ role Temporal::TimeZone::Observance {
# The ISO8601 standard does not allow for offsets with sub-minute
# resolutions. In real-world practice, this is not an issue.
our Str method iso8601 {
- sprintf "%+03d%02d", self.offset / 3600,
- int( abs(self.offset) / 60 ) % 60;
+ sprintf "%+03d%02d", self.offset div 3600,
+ int( abs(self.offset) div 60 ) % 60;
}
method Str { self.iso8601 }
@@ -117,11 +117,11 @@ role Temporal::DateTime {
# This involves a whole bunch of code - see Perl 5's Time::Local
our Num method epoch {
my ( $a, $y, $m, $jd ); # algorithm from Claus T��ndering
- $a = int((14 - $.date.month) / 12 );
+ $a = int((14 - $.date.month) div 12 );
$y = $.date.year + 4800 - $a;
$m = $.date.month + 12 * $a - 3;
- $jd = $.date.day + int((153 * $m + 2) / 5) + 365 * $y
- + int( $y / 4 ) - int( $y / 100 ) + int( $y / 400 ) - 32045;
+ $jd = $.date.day + int((153 * $m + 2) div 5) + 365 * $y
+ + int( $y div 4 ) - int( $y div 100 ) + int( $y div 400 ) - 32045;
return ($jd - 2440588) * 24 * 60 * 60
+ ($.time.hour*60 + $.time.minute)*60 + $.time.second;
}
@@ -136,21 +136,21 @@ class Time {
our method gmtime( Num $epoch = time ) {
my ( $time, $second, $minute, $hour, $day, $month, $year );
$time = int( $epoch );
- $second = $time % 60; $time = int($time/60);
- $minute = $time % 60; $time = int($time/60);
- $hour = $time % 24; $time = int($time/24);
+ $second = $time % 60; $time = int($time div 60);
+ $minute = $time % 60; $time = int($time div 60);
+ $hour = $time % 24; $time = int($time div 24);
# Day month and leap year arithmetic, based on Gregorian day #.
# 2000-01-01 noon UTC == 2451558.0 Julian == 2451545.0 Gregorian
$time += 2440588; # because 2000-01-01 == Unix epoch day 10957
my $a = $time + 32044; # date algorithm from Claus T��ndering
- my $b = int((4 * $a + 3) / 146097); # 146097 = days in 400 years
- my $c = $a - int(( 146097 * $b ) / 4);
- my $d = int((4 * $c + 3) / 1461); # 1461 = days in 4 years
- my $e = $c - int(($d * 1461) / 4);
- my $m = int((5 * $e + 2) / 153); # 153 = days in Mar-Jul Aug-Dec
- $day = $e - int((153 * $m + 2) / 5 ) + 1;
- $month = $m + 3 - 12 * int( $m / 10 );
- $year = $b * 100 + $d - 4800 + int( $m / 10 );
+ my $b = int((4 * $a + 3) div 146097); # 146097 = days in 400 years
+ my $c = $a - int(( 146097 * $b ) div 4);
+ my $d = int((4 * $c + 3) div 1461); # 1461 = days in 4 years
+ my $e = $c - int(($d * 1461) div 4);
+ my $m = int((5 * $e + 2) div 153); # 153 = days in Mar-Jul Aug-Dec
+ $day = $e - int((153 * $m + 2) div 5 ) + 1;
+ $month = $m + 3 - 12 * int( $m div 10 );
+ $year = $b * 100 + $d - 4800 + int( $m div 10 );
Temporal::DateTime.new(
date => Temporal::Date.new(:$year, :$month, :$day),
time => Temporal::Time.new(:$hour, :$minute, :$second),
--
1.6.0.5
|
From @colomonspectest-changes-needed-to-swap-the-meanings-of-div-and-as.patchIndex: t/spec/S02-builtin_data_types/num.t
===================================================================
--- t/spec/S02-builtin_data_types/num.t (revision 28151)
+++ t/spec/S02-builtin_data_types/num.t (working copy)
@@ -19,7 +19,7 @@
#L<S02/Built-In Data Types/Rat supports arbitrary precision rational arithmetic>
{
- my $a = 1 div 1;
+ my $a = 1 / 1;
isa_ok($a, Rat);
is($a, "1/1", '1.0 stringification works');
}
@@ -91,7 +91,7 @@
}
#L<S02/Built-In Data Types/Rat supports arbitrary precision rational arithmetic>
-isa_ok(1 div 1, Rat);
+isa_ok(1 / 1, Rat);
{
my $a = 80000.0000000000000000000000000;
Index: t/spec/S02-builtin_data_types/declare.t
===================================================================
--- t/spec/S02-builtin_data_types/declare.t (revision 28151)
+++ t/spec/S02-builtin_data_types/declare.t (working copy)
@@ -29,7 +29,7 @@
}
{
- my Rat $namcu = 7 div 4;
+ my Rat $namcu = 7 / 4;
isa_ok($namcu,Rat);
}
@@ -148,7 +148,7 @@
#?rakudo skip 'rat not implemented'
{
- my rat $namcu = 7 div 4;
+ my rat $namcu = 7 / 4;
isa_ok($namcu,rat);
}
Index: t/spec/S02-builtin_data_types/type.t
===================================================================
--- t/spec/S02-builtin_data_types/type.t (revision 28151)
+++ t/spec/S02-builtin_data_types/type.t (working copy)
@@ -118,8 +118,8 @@
{
# the following two are the same type of behavior
# S02: "It is possible for the of type to disagree with the as type"
- my Rat sub returntype4 ($pass) as Num {$pass ?? 11 div 10 !! 1}
- my sub returntype5 ($pass --> Rat) as Num {$pass ?? 11 div 5 !! 2}
+ my Rat sub returntype4 ($pass) as Num {$pass ?? 11 / 10 !! 1}
+ my sub returntype5 ($pass --> Rat) as Num {$pass ?? 11 / 5 !! 2}
is(returntype4(True), 1.1, 'good return value works (my Type sub as OtherType)');
eval_dies_ok('returntype4(False)', 'bad return value dies (my Type sub as OtherType)');
Index: t/spec/S32-temporal/Temporal.t
===================================================================
--- t/spec/S32-temporal/Temporal.t (revision 28151)
+++ t/spec/S32-temporal/Temporal.t (working copy)
@@ -31,7 +31,7 @@
'2000-01-01 00:00:00 was on a Saturday in January'; # test 6
$t = time;
-$d = (int($t/86400) + 4) % 7 + 1;
+$d = (int($t / 86400) + 4) % 7 + 1;
$g1 = Time.gmtime($t);
$g2 = Time.gmtime; # $g1 and $g2 might differ very occasionally
ok $g1.date.year ==$g2.date.year && $g1.date.month ==$g2.date.month &&
@@ -80,14 +80,18 @@
time => Temporal::Time.new( :hour(@t[2]), :minute(@t[1]), :second(@t[0]) ),
timezone => Temporal::TimeZone::Observance.new(
offset=>-8*3600, isdst=>Bool::False, abbreviation=>'PDT' ) );
+
ok $g1.epoch == $t, "at $g1, epoch is {$g1.epoch}"; # test 17
+# All these "int()"s should be considered grotesque, but that make it
+# work in Rakudo for now.
+
# An independent calculation to cross check the Temporal algorithms.
sub test_gmtime( Num $t is copy ) {
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst);
- $sec = int($t) % 60; $t = int($t/60); # $t is now epoch minutes
- $min = $t % 60; $t = int($t/60); # $t is now epoch hours
- $hour = $t % 24; $t = int($t/24); # $t is now epoch days
+ $sec = int($t) % 60; $t = int(int($t) div 60); # $t is now epoch minutes
+ $min = $t % 60; $t = int($t div 60); # $t is now epoch hours
+ $hour = $t % 24; $t = int($t div 24); # $t is now epoch days
# Not a sophisticated or fast algorithm, just an understandable one
# only valid from 1970-01-01 until 2100-02-28
$wday = ($t+4) % 7; # 1970-01-01 was a Thursday
Index: t/spec/S03-operators/assign.t
===================================================================
--- t/spec/S03-operators/assign.t (revision 28151)
+++ t/spec/S03-operators/assign.t (working copy)
@@ -297,10 +297,10 @@
{
my $x = 6;
- @p = $x /= 3, 4;
- is($x, 2, '/= operator');
- is(@p[0],2, "/= operator parses as item assignment 1");
- is(@p[1],4, "/= operator parses as item assignment 2");
+ @p = $x div= 3, 4;
+ is($x, 2, 'div= operator');
+ is(@p[0],2, "div= operator parses as item assignment 1");
+ is(@p[1],4, "div= operator parses as item assignment 2");
}
{
Index: t/spec/S03-operators/ternary.t
===================================================================
--- t/spec/S03-operators/ternary.t (revision 28151)
+++ t/spec/S03-operators/ternary.t (working copy)
@@ -21,7 +21,7 @@
}
is(($str2 eq $str1 ?? 8 * 8 !! 9 * 9), 64, "?? !! in parenthesis");
-is(($str2 eq $str3 ?? 8 + 8 !! 9 / 9), 1, "?? !! in parenthesis");
+is(($str2 eq $str3 ?? 8 + 8 !! 9 div 9), 1, "?? !! in parenthesis");
is(1 ?? 2 ?? 3 !! 4 !! 5 ?? 6 !! 7, 3, "nested ?? !!");
is(1 ?? 0 ?? 3 !! 4 !! 5 ?? 6 !! 7, 4, "nested ?? !!");
Index: t/spec/S03-operators/hyper.t
===================================================================
--- t/spec/S03-operators/hyper.t (revision 28151)
+++ t/spec/S03-operators/hyper.t (working copy)
@@ -35,7 +35,7 @@
@e = ((1,1,1), (2,2), (3));
is(~@r, ~@e, "hyper-xx two arrays");
- @r = (20, 40, 60) ��/�� (2, 5, 10);
+ @r = (20, 40, 60) ��div�� (2, 5, 10);
@e = (10, 8, 6);
is(~@r, ~@e, "hyper-divide two arrays");
@@ -65,7 +65,7 @@
@e = ((1,1,1), (2,2), (3));
is(~@r, ~@e, "hyper-xx two arrays ASCII notation");
- @r = (20, 40, 60) >>/<< (2, 5, 10);
+ @r = (20, 40, 60) >>div<< (2, 5, 10);
@e = (10, 8, 6);
is(~@r, ~@e, "hyper-divide two arrays ASCII notation");
Index: t/spec/S03-operators/basic-types.t
===================================================================
--- t/spec/S03-operators/basic-types.t (revision 28151)
+++ t/spec/S03-operators/basic-types.t (working copy)
@@ -64,7 +64,7 @@
my $float = 0.5;
isa_ok($float, Num, 'it is an Num type');
-isa_ok(1 div 4, Rat, 'infix:<div> produces a Rat');
+isa_ok(1 / 4, Rat, 'infix:</> of integers produces a Rat');
my $string = "Hello World";
isa_ok($string, Str, 'it is a Str type');
Index: t/spec/S03-operators/precedence.t
===================================================================
--- t/spec/S03-operators/precedence.t (revision 28151)
+++ t/spec/S03-operators/precedence.t (working copy)
@@ -13,7 +13,7 @@
=end pod
-plan 54;
+plan 55;
# terms
@@ -46,7 +46,8 @@
# multiplicative
is(4 + 3 * 2, 10, "* binds tighter than binary +");
-is(2 - 2 / 2, 1, "/ binds tighter than binary -");
+is(2 - 2 div 2, 1, "div binds tighter than binary -");
+is(2 - 2 / 2, 1 / 1, "/ binds tighter than binary -");
# additive
Index: t/spec/S03-operators/arith.t
===================================================================
--- t/spec/S03-operators/arith.t (revision 28151)
+++ t/spec/S03-operators/arith.t (working copy)
@@ -2,7 +2,7 @@
use Test;
-plan 199;
+plan 200;
my $five = abs(-5);
@@ -263,16 +263,19 @@
# divide
-tryeq 28/14, 2;
-tryeq 28/-7, -4;
-tryeq -28/4, -7;
-tryeq -28/-2, 14;
+tryeq 28 div 14, 2;
+tryeq 28 div -7, -4;
+tryeq -28 div 4, -7;
+tryeq -28 div -2, 14;
-tryeq 0x80000000/1, 0x80000000;
-tryeq 0x80000000/-1, -0x80000000;
-tryeq -0x80000000/1, -0x80000000;
-tryeq -0x80000000/-1, 0x80000000;
+tryeq 0x80000000 div 1, 0x80000000;
+tryeq 0x80000000 div -1, -0x80000000;
+tryeq -0x80000000 div 1, -0x80000000;
+tryeq -0x80000000 div -1, 0x80000000;
+#?rakudo todo '$x div $y == floor($x/$y)'
+is(9 div 4, 2, "9 div 4 == 2");
+
# The example for sloppy divide, rigged to avoid the peephole optimiser.
is_approx "20." / "5.", 4;
@@ -283,12 +286,12 @@
# Bluuurg if your floating point can't accurately cope with powers of 2
# [I suspect this is parsing string-to-float problems, not actual arith]
-is 18446744073709551616/1, 18446744073709551616; # Bluuurg
+is 18446744073709551616 div 1, 18446744073709551616; # Bluuurg
{
- tryeq_sloppy 18446744073709551616/2, 9223372036854775808;
- tryeq_sloppy 18446744073709551616/4294967296, 4294967296;
- tryeq_sloppy 18446744073709551616/9223372036854775808, 2;
+ tryeq_sloppy 18446744073709551616 div 2, 9223372036854775808;
+ tryeq_sloppy 18446744073709551616 div 4294967296, 4294967296;
+ tryeq_sloppy 18446744073709551616 div 9223372036854775808, 2;
}
{
@@ -323,7 +326,7 @@
{
is_approx(-1, (0 + 1i)**2, "i^2 == -1");
is_approx(-1, (0.7071067811865476 + -0.7071067811865475i)**4, "sqrt(-i)**4 ==-1" );
- is_approx(1i, (-1+0i)**(1/2), '(-1+0i)**(1/2) == i ');
+ is_approx(1i, (-1+0i)**(1 div 2), '(-1+0i)**(1/2) == i ');
}
{
@@ -335,16 +338,16 @@
is Inf+100, Inf;
is Inf-100, Inf;
is Inf*100, Inf;
- is Inf/100, Inf;
+ is Inf / 100, Inf;
is Inf*-100, -Inf;
- is Inf/-100, -Inf;
- is 100/Inf, 0;
+ is Inf / -100, -Inf;
+ is 100 / Inf, 0;
is Inf**100, Inf;
is Inf*0, NaN;
is Inf - Inf, NaN;
is Inf*Inf, Inf;
- is Inf/Inf, NaN;
- is Inf*Inf/Inf, NaN;
+ is Inf / Inf, NaN;
+ is Inf*Inf / Inf, NaN;
is Inf**0, 1;
is 0**0, 1;
is 0**Inf, 0;
@@ -373,18 +376,18 @@
is NaN+100, NaN;
is NaN-100, NaN;
is NaN*100, NaN;
- is NaN/100, NaN;
+ is NaN / 100, NaN;
is NaN**100, NaN;
is NaN+NaN, NaN;
is NaN - NaN, NaN;
is NaN*NaN, NaN;
- is NaN/NaN, NaN;
+ is NaN / NaN, NaN;
is NaN+Inf, NaN;
is NaN - Inf, NaN;
is NaN*Inf, NaN;
- is NaN/Inf, NaN;
- is Inf/NaN, NaN;
+ is NaN / Inf, NaN;
+ is Inf / NaN, NaN;
my $nan1 = NaN**NaN;
is $nan1, NaN, "NaN**NaN";
@@ -413,9 +416,9 @@
dies_ok( { $x = 0; say 3 % $x; }, 'Modulo zero dies and is catchable with VInt/VRat variables');
dies_ok( { $x := 0; say 3 % $x; }, 'Modulo zero dies and is catchable with VRef variables');
-dies_ok( { say 3 / 0 }, 'Division by zero dies and is catchable');
-dies_ok( { $x = 0; say 3 / $x; }, 'Division by zero dies and is catchable with VInt/VRat variables');
-dies_ok( { $x := 0; say 3 / $x; }, 'Division by zero dies and is catchable with VRef variables');
+dies_ok( { say 3 div 0 }, 'Division by zero dies and is catchable');
+dies_ok( { $x = 0; say 3 div $x; }, 'Division by zero dies and is catchable with VInt div VRat variables');
+dies_ok( { $x := 0; say 3 div $x; }, 'Division by zero dies and is catchable with VRef variables');
# This is a rakudo regression wrt bignum:
#?rakudo skip 'bigint'
Index: t/spec/integration/99problems-31-to-40.t
===================================================================
--- t/spec/integration/99problems-31-to-40.t (revision 28151)
+++ t/spec/integration/99problems-31-to-40.t (working copy)
@@ -147,7 +147,7 @@
while $n > 1 {
if $n % $cond == 0 {
$count++;
- $n /= $cond;
+ $n div= $cond;
}
else {
if $count > 0 {
Index: t/spec/integration/99problems-51-to-60.t
===================================================================
--- t/spec/integration/99problems-51-to-60.t (revision 28151)
+++ t/spec/integration/99problems-51-to-60.t (working copy)
@@ -56,14 +56,14 @@
return undef if $n == 0;
gather {
if $n % 2 == 1 {
- my $k = ($n - 1) / 2;
+ my $k = ($n - 1) div 2;
for cbal-tree($k) -> $a {
for cbal-tree($k) -> $b {
take ['x', $a, $b];
}
}
} else {
- my $k = $n / 2;
+ my $k = $n div 2;
for cbal-tree($k) -> $a {
for cbal-tree($k - 1) -> $b {
take ['x', $a, $b];
Index: t/spec/S32-num/rat.t
===================================================================
--- t/spec/S32-num/rat.t (revision 28151)
+++ t/spec/S32-num/rat.t (working copy)
@@ -18,12 +18,14 @@
is(~(Rat.new(0,33)), "0/1", "Reduce to simplest form in constructor");
# Test basic math
-is(~(1 div 4 + 1 div 4), "1/2", "1/4 + 1/4 = 1/2");
-is(~(1 div 4 + 2 div 7), "15/28", "1/4 + 2/7 = 15/28");
-is(~(1 div 4 + 1), "5/4", "1/4 + 1 = 5/4");
-is(~(1 + 1 div 4), "5/4", "1 + 1/4 = 5/4");
-is(~(1 div 4 - 1 div 4), "0/1", "1/4 - 1/4 = 0/1");
-is(~(1 div 4 - 3 div 4), "-1/2", "1/4 - 3/4 = -1/2");
+is(~(1 / 4 + 1 / 4), "1/2", "1/4 + 1/4 = 1/2");
+is(~(1 / 4 + 2 / 7), "15/28", "1/4 + 2/7 = 15/28");
+is(~(1 / 4 + 1), "5/4", "1/4 + 1 = 5/4");
+is(~(1 + 1 / 4), "5/4", "1 + 1/4 = 5/4");
+is(~(1 / 4 - 1 / 4), "0/1", "1/4 - 1/4 = 0/1");
+is(~(1 / 4 - 3 / 4), "-1/2", "1/4 - 3/4 = -1/2");
+is(~(1 / 4 - 1), "-3/4", "1/4 - 1 = -3/4");
+is(~(1 - 1 / 4), "3/4", "1 - 1/4 = 3/4");
done_testing;
Index: t/spec/S02-literals/radix.t
===================================================================
--- t/spec/S02-literals/radix.t (revision 28151)
+++ t/spec/S02-literals/radix.t (working copy)
@@ -106,7 +106,7 @@
# L<S02/Literals/"Any radix may include a fractional part">
#?rakudo todo 'fractionals base 16'
-is(:16<dead_beef.face>, 0xDEAD_BEEF + 0xFACE / ( 16 ** 4 ), 'Fractional base 16 works' );
+is(:16<dead_beef.face>, 0xDEAD_BEEF + 0xFACE div ( 16 ** 4 ), 'Fractional base 16 works' );
# L<S02/Literals/":8<177777>">
@@ -225,7 +225,7 @@
is +":1_0<14_56>", 1456, "underscore seperators works";
is +":10<123.456>", 123.456, "base 10 decimal notation works";
is +":2<1.111>", 1.875, "base 2 decimal notation works";
- is +":16<dead_beef.face>", 0xDEADBEEF + 0xFACE / (16 ** 4), "fractional base 16 works";
+ is +":16<dead_beef.face>", 0xDEADBEEF + 0xFACE div (16 ** 4), "fractional base 16 works";
for 2..36 {
is +":{$_}<11>", $_ + 1, "stringified form of base $_ works";
Index: t/spec/S02-magicals/dollar_bang.t
===================================================================
--- t/spec/S02-magicals/dollar_bang.t (revision 28151)
+++ t/spec/S02-magicals/dollar_bang.t (working copy)
@@ -34,7 +34,7 @@
ok !$called, 'The subroutine also was not called';
undefine $!;
-try { 1 / 0 };
+try { 1 div 0 };
ok $!, 'Dividing one by zero sets $!';
sub incr ( $a is rw ) { $a++ };
|
From @colomonHi all, This is an updated version of the patch to swap / and div. Thanks to -- |
From @colomon0001-Fix-Rat.perl-to-return-N-M-rather-than-Rat.new-N.patchFrom 9e343fee7d8da6c7126ca08da9aa64766c7b3835 Mon Sep 17 00:00:00 2001
From: Solomon Foster <colomon@gmail.com>
Date: Mon, 31 Aug 2009 21:39:58 -0400
Subject: [PATCH] Fix Rat.perl to return "N/M" rather than "Rat.new(N,M)".
---
src/setting/Rat.pm | 2 +-
1 files changed, 1 insertions(+), 1 deletions(-)
diff --git a/src/setting/Rat.pm b/src/setting/Rat.pm
index ba73dac..06f08f3 100644
--- a/src/setting/Rat.pm
+++ b/src/setting/Rat.pm
@@ -25,7 +25,7 @@ class Rat {
self.bless(*, :$numerator, :$denominator);
}
- multi method perl() { "Rat.new($!numerator, $!denominator)"; }
+ multi method perl() { "$!numerator/$!denominator"; }
multi method Str() { "$!numerator/$!denominator"; }
--
1.6.0.5
|
From @colomon0002-Switch-GCD-routine-to-use-instead-of-for-a-vast.patchFrom b63ce509840430ff48896ae7795ad1d393edd5b4 Mon Sep 17 00:00:00 2001
From: Solomon Foster <colomon@gmail.com>
Date: Tue, 1 Sep 2009 07:14:26 -0400
Subject: [PATCH] Switch GCD routine to use % instead of -, for a vast performance increase on widely mismatched numbers. Add Rat * Int, Int * Rat, Rat / Int, and Int / Rat overloads.
---
src/setting/Rat.pm | 18 +++++++++++++++++-
1 files changed, 17 insertions(+), 1 deletions(-)
diff --git a/src/setting/Rat.pm b/src/setting/Rat.pm
index 06f08f3..f2355da 100644
--- a/src/setting/Rat.pm
+++ b/src/setting/Rat.pm
@@ -9,7 +9,7 @@ class Rat {
while $a > 0 && $b > 0
{
($a, $b) = ($b, $a) if ($b > $a);
- $a -= $b;
+ $a %= $b;
}
return $a + $b;
}
@@ -62,10 +62,26 @@ multi sub infix:<*>(Rat $a, Rat $b) {
Rat.new($a.numerator * $b.numerator, $a.denominator * $b.denominator);
}
+multi sub infix:<*>(Rat $a, Int $b) {
+ Rat.new($a.numerator * $b, $a.denominator);
+}
+
+multi sub infix:<*>(Int $a, Rat $b) {
+ Rat.new($a * $b.numerator, $b.denominator);
+}
+
multi sub infix:</>(Rat $a, Rat $b) {
Rat.new($a.numerator * $b.denominator, $a.denominator * $b.numerator);
}
+multi sub infix:</>(Rat $a, Int $b) {
+ Rat.new($a.numerator, $a.denominator * $b);
+}
+
+multi sub infix:</>(Int $a, Rat $b) {
+ Rat.new($b.denominator, $a * $b.numerator);
+}
+
multi sub infix:</>(Int $a, Int $b) {
Rat.new($a, $b);
}
--
1.6.0.5
|
From @colomon0004-Apply-moritz-s-patch-to-infix-div-for-Ints.-Patch.patchFrom de1dbcaa3acc1fb2d29c2fe048843ca5f014a2db Mon Sep 17 00:00:00 2001
From: Solomon Foster <colomon@gmail.com>
Date: Tue, 1 Sep 2009 10:36:03 -0400
Subject: [PATCH] Apply moritz's patch to infix:<div> for Ints. Patch makes it behave correctly
with "floor" behavior, but fails tests on very large Ints.
---
src/setting/Int.pm | 15 ++++-----------
1 files changed, 4 insertions(+), 11 deletions(-)
diff --git a/src/setting/Int.pm b/src/setting/Int.pm
index 864e551..2962758 100644
--- a/src/setting/Int.pm
+++ b/src/setting/Int.pm
@@ -52,18 +52,11 @@ multi sub infix:<*>(Int $a, Int $b) {
multi sub infix:<div>(Int $a, Int $b) {
Q:PIR {
$P0 = find_lex '$a'
- $N0 = $P0
+ $I0 = $P0
$P1 = find_lex '$b'
- $N1 = $P1
- $N2 = $N0 / $N1
- $I2 = floor $N2
- $N3 = $N2 - $I2
- if $N3 != 0 goto notint
- %r = '!upgrade_to_num_if_needed'($N2)
- goto done
- notint:
- %r = box $N2
- done:
+ $I1 = $P1
+ $I2 = $I0 / $I1
+ %r = box $I2
}
}
--
1.6.0.5
|
From @colomonspectest-Rat.patchIndex: t/spec/S02-builtin_data_types/num.t
===================================================================
--- t/spec/S02-builtin_data_types/num.t (revision 28156)
+++ t/spec/S02-builtin_data_types/num.t (working copy)
@@ -19,7 +19,7 @@
#L<S02/Built-In Data Types/Rat supports arbitrary precision rational arithmetic>
{
- my $a = 1 div 1;
+ my $a = 1 / 1;
isa_ok($a, Rat);
is($a, "1/1", '1.0 stringification works');
}
@@ -91,7 +91,7 @@
}
#L<S02/Built-In Data Types/Rat supports arbitrary precision rational arithmetic>
-isa_ok(1 div 1, Rat);
+isa_ok(1 / 1, Rat);
{
my $a = 80000.0000000000000000000000000;
Index: t/spec/S02-builtin_data_types/declare.t
===================================================================
--- t/spec/S02-builtin_data_types/declare.t (revision 28156)
+++ t/spec/S02-builtin_data_types/declare.t (working copy)
@@ -29,7 +29,7 @@
}
{
- my Rat $namcu = 7 div 4;
+ my Rat $namcu = 7 / 4;
isa_ok($namcu,Rat);
}
@@ -148,7 +148,7 @@
#?rakudo skip 'rat not implemented'
{
- my rat $namcu = 7 div 4;
+ my rat $namcu = 7 / 4;
isa_ok($namcu,rat);
}
Index: t/spec/S02-builtin_data_types/type.t
===================================================================
--- t/spec/S02-builtin_data_types/type.t (revision 28156)
+++ t/spec/S02-builtin_data_types/type.t (working copy)
@@ -118,8 +118,8 @@
{
# the following two are the same type of behavior
# S02: "It is possible for the of type to disagree with the as type"
- my Rat sub returntype4 ($pass) as Num {$pass ?? 11 div 10 !! 1}
- my sub returntype5 ($pass --> Rat) as Num {$pass ?? 11 div 5 !! 2}
+ my Rat sub returntype4 ($pass) as Num {$pass ?? 11 / 10 !! 1}
+ my sub returntype5 ($pass --> Rat) as Num {$pass ?? 11 / 5 !! 2}
is(returntype4(True), 1.1, 'good return value works (my Type sub as OtherType)');
eval_dies_ok('returntype4(False)', 'bad return value dies (my Type sub as OtherType)');
Index: t/spec/S03-operators/assign.t
===================================================================
--- t/spec/S03-operators/assign.t (revision 28156)
+++ t/spec/S03-operators/assign.t (working copy)
@@ -297,10 +297,10 @@
{
my $x = 6;
- @p = $x /= 3, 4;
- is($x, 2, '/= operator');
- is(@p[0],2, "/= operator parses as item assignment 1");
- is(@p[1],4, "/= operator parses as item assignment 2");
+ @p = $x div= 3, 4;
+ is($x, 2, 'div= operator');
+ is(@p[0],2, "div= operator parses as item assignment 1");
+ is(@p[1],4, "div= operator parses as item assignment 2");
}
{
Index: t/spec/S03-operators/ternary.t
===================================================================
--- t/spec/S03-operators/ternary.t (revision 28156)
+++ t/spec/S03-operators/ternary.t (working copy)
@@ -21,7 +21,7 @@
}
is(($str2 eq $str1 ?? 8 * 8 !! 9 * 9), 64, "?? !! in parenthesis");
-is(($str2 eq $str3 ?? 8 + 8 !! 9 / 9), 1, "?? !! in parenthesis");
+is(($str2 eq $str3 ?? 8 + 8 !! 9 div 9), 1, "?? !! in parenthesis");
is(1 ?? 2 ?? 3 !! 4 !! 5 ?? 6 !! 7, 3, "nested ?? !!");
is(1 ?? 0 ?? 3 !! 4 !! 5 ?? 6 !! 7, 4, "nested ?? !!");
Index: t/spec/S03-operators/hyper.t
===================================================================
--- t/spec/S03-operators/hyper.t (revision 28156)
+++ t/spec/S03-operators/hyper.t (working copy)
@@ -35,7 +35,7 @@
@e = ((1,1,1), (2,2), (3));
is(~@r, ~@e, "hyper-xx two arrays");
- @r = (20, 40, 60) ��/�� (2, 5, 10);
+ @r = (20, 40, 60) ��div�� (2, 5, 10);
@e = (10, 8, 6);
is(~@r, ~@e, "hyper-divide two arrays");
@@ -65,7 +65,7 @@
@e = ((1,1,1), (2,2), (3));
is(~@r, ~@e, "hyper-xx two arrays ASCII notation");
- @r = (20, 40, 60) >>/<< (2, 5, 10);
+ @r = (20, 40, 60) >>div<< (2, 5, 10);
@e = (10, 8, 6);
is(~@r, ~@e, "hyper-divide two arrays ASCII notation");
Index: t/spec/S03-operators/basic-types.t
===================================================================
--- t/spec/S03-operators/basic-types.t (revision 28156)
+++ t/spec/S03-operators/basic-types.t (working copy)
@@ -64,7 +64,7 @@
my $float = 0.5;
isa_ok($float, Num, 'it is an Num type');
-isa_ok(1 div 4, Rat, 'infix:<div> produces a Rat');
+isa_ok(1 / 4, Rat, 'infix:</> of integers produces a Rat');
my $string = "Hello World";
isa_ok($string, Str, 'it is a Str type');
Index: t/spec/S03-operators/precedence.t
===================================================================
--- t/spec/S03-operators/precedence.t (revision 28156)
+++ t/spec/S03-operators/precedence.t (working copy)
@@ -13,7 +13,7 @@
=end pod
-plan 54;
+plan 55;
# terms
@@ -46,7 +46,8 @@
# multiplicative
is(4 + 3 * 2, 10, "* binds tighter than binary +");
-is(2 - 2 / 2, 1, "/ binds tighter than binary -");
+is(2 - 2 div 2, 1, "div binds tighter than binary -");
+is(2 - 2 / 2, 1 / 1, "/ binds tighter than binary -");
# additive
Index: t/spec/S03-operators/arith.t
===================================================================
--- t/spec/S03-operators/arith.t (revision 28156)
+++ t/spec/S03-operators/arith.t (working copy)
@@ -2,7 +2,7 @@
use Test;
-plan 199;
+plan 200;
my $five = abs(-5);
@@ -263,16 +263,19 @@
# divide
-tryeq 28/14, 2;
-tryeq 28/-7, -4;
-tryeq -28/4, -7;
-tryeq -28/-2, 14;
+tryeq 28 div 14, 2;
+tryeq 28 div -7, -4;
+tryeq -28 div 4, -7;
+tryeq -28 div -2, 14;
-tryeq 0x80000000/1, 0x80000000;
-tryeq 0x80000000/-1, -0x80000000;
-tryeq -0x80000000/1, -0x80000000;
-tryeq -0x80000000/-1, 0x80000000;
+tryeq 0x80000000 div 1, 0x80000000;
+tryeq 0x80000000 div -1, -0x80000000;
+tryeq -0x80000000 div 1, -0x80000000;
+tryeq -0x80000000 div -1, 0x80000000;
+#?rakudo todo '$x div $y == floor($x/$y)'
+is(9 div 4, 2, "9 div 4 == 2");
+
# The example for sloppy divide, rigged to avoid the peephole optimiser.
is_approx "20." / "5.", 4;
@@ -283,12 +286,12 @@
# Bluuurg if your floating point can't accurately cope with powers of 2
# [I suspect this is parsing string-to-float problems, not actual arith]
-is 18446744073709551616/1, 18446744073709551616; # Bluuurg
+is 18446744073709551616 div 1, 18446744073709551616; # Bluuurg
{
- tryeq_sloppy 18446744073709551616/2, 9223372036854775808;
- tryeq_sloppy 18446744073709551616/4294967296, 4294967296;
- tryeq_sloppy 18446744073709551616/9223372036854775808, 2;
+ tryeq_sloppy 18446744073709551616 div 2, 9223372036854775808;
+ tryeq_sloppy 18446744073709551616 div 4294967296, 4294967296;
+ tryeq_sloppy 18446744073709551616 div 9223372036854775808, 2;
}
{
@@ -323,7 +326,7 @@
{
is_approx(-1, (0 + 1i)**2, "i^2 == -1");
is_approx(-1, (0.7071067811865476 + -0.7071067811865475i)**4, "sqrt(-i)**4 ==-1" );
- is_approx(1i, (-1+0i)**(1/2), '(-1+0i)**(1/2) == i ');
+ is_approx(1i, (-1+0i)**(1 div 2), '(-1+0i)**(1/2) == i ');
}
{
@@ -335,16 +338,16 @@
is Inf+100, Inf;
is Inf-100, Inf;
is Inf*100, Inf;
- is Inf/100, Inf;
+ is Inf / 100, Inf;
is Inf*-100, -Inf;
- is Inf/-100, -Inf;
- is 100/Inf, 0;
+ is Inf / -100, -Inf;
+ is 100 / Inf, 0;
is Inf**100, Inf;
is Inf*0, NaN;
is Inf - Inf, NaN;
is Inf*Inf, Inf;
- is Inf/Inf, NaN;
- is Inf*Inf/Inf, NaN;
+ is Inf / Inf, NaN;
+ is Inf*Inf / Inf, NaN;
is Inf**0, 1;
is 0**0, 1;
is 0**Inf, 0;
@@ -373,18 +376,18 @@
is NaN+100, NaN;
is NaN-100, NaN;
is NaN*100, NaN;
- is NaN/100, NaN;
+ is NaN / 100, NaN;
is NaN**100, NaN;
is NaN+NaN, NaN;
is NaN - NaN, NaN;
is NaN*NaN, NaN;
- is NaN/NaN, NaN;
+ is NaN / NaN, NaN;
is NaN+Inf, NaN;
is NaN - Inf, NaN;
is NaN*Inf, NaN;
- is NaN/Inf, NaN;
- is Inf/NaN, NaN;
+ is NaN / Inf, NaN;
+ is Inf / NaN, NaN;
my $nan1 = NaN**NaN;
is $nan1, NaN, "NaN**NaN";
@@ -413,9 +416,9 @@
dies_ok( { $x = 0; say 3 % $x; }, 'Modulo zero dies and is catchable with VInt/VRat variables');
dies_ok( { $x := 0; say 3 % $x; }, 'Modulo zero dies and is catchable with VRef variables');
-dies_ok( { say 3 / 0 }, 'Division by zero dies and is catchable');
-dies_ok( { $x = 0; say 3 / $x; }, 'Division by zero dies and is catchable with VInt/VRat variables');
-dies_ok( { $x := 0; say 3 / $x; }, 'Division by zero dies and is catchable with VRef variables');
+dies_ok( { say 3 div 0 }, 'Division by zero dies and is catchable');
+dies_ok( { $x = 0; say 3 div $x; }, 'Division by zero dies and is catchable with VInt div VRat variables');
+dies_ok( { $x := 0; say 3 div $x; }, 'Division by zero dies and is catchable with VRef variables');
# This is a rakudo regression wrt bignum:
#?rakudo skip 'bigint'
Index: t/spec/integration/99problems-31-to-40.t
===================================================================
--- t/spec/integration/99problems-31-to-40.t (revision 28156)
+++ t/spec/integration/99problems-31-to-40.t (working copy)
@@ -147,7 +147,7 @@
while $n > 1 {
if $n % $cond == 0 {
$count++;
- $n /= $cond;
+ $n div= $cond;
}
else {
if $count > 0 {
Index: t/spec/integration/99problems-51-to-60.t
===================================================================
--- t/spec/integration/99problems-51-to-60.t (revision 28156)
+++ t/spec/integration/99problems-51-to-60.t (working copy)
@@ -56,14 +56,14 @@
return undef if $n == 0;
gather {
if $n % 2 == 1 {
- my $k = ($n - 1) / 2;
+ my $k = ($n - 1) div 2;
for cbal-tree($k) -> $a {
for cbal-tree($k) -> $b {
take ['x', $a, $b];
}
}
} else {
- my $k = $n / 2;
+ my $k = $n div 2;
for cbal-tree($k) -> $a {
for cbal-tree($k - 1) -> $b {
take ['x', $a, $b];
Index: t/spec/S32-num/rat.t
===================================================================
--- t/spec/S32-num/rat.t (revision 28156)
+++ t/spec/S32-num/rat.t (working copy)
@@ -16,15 +16,30 @@
is(~(Rat.new(2,4)), "1/2", "Reduce to simplest form in constructor");
is(~(Rat.new(39,33)), "13/11", "Reduce to simplest form in constructor");
is(~(Rat.new(0,33)), "0/1", "Reduce to simplest form in constructor");
+is(~(Rat.new(1451234131,60)), "1451234131/60", "Reduce huge number to simplest form in constructor");
# Test basic math
-is(~(1 div 4 + 1 div 4), "1/2", "1/4 + 1/4 = 1/2");
-is(~(1 div 4 + 2 div 7), "15/28", "1/4 + 2/7 = 15/28");
-is(~(1 div 4 + 1), "5/4", "1/4 + 1 = 5/4");
-is(~(1 + 1 div 4), "5/4", "1 + 1/4 = 5/4");
-is(~(1 div 4 - 1 div 4), "0/1", "1/4 - 1/4 = 0/1");
-is(~(1 div 4 - 3 div 4), "-1/2", "1/4 - 3/4 = -1/2");
+is(~(1 / 4 + 1 / 4), "1/2", "1/4 + 1/4 = 1/2");
+is(~(1 / 4 + 2 / 7), "15/28", "1/4 + 2/7 = 15/28");
+is(~(1 / 4 + 1), "5/4", "1/4 + 1 = 5/4");
+is(~(1 + 1 / 4), "5/4", "1 + 1/4 = 5/4");
+is(~(1 / 4 - 1 / 4), "0/1", "1/4 - 1/4 = 0/1");
+is(~(1 / 4 - 3 / 4), "-1/2", "1/4 - 3/4 = -1/2");
+is(~(1 / 4 - 1), "-3/4", "1/4 - 1 = -3/4");
+is(~(1 - 1 / 4), "3/4", "1 - 1/4 = 3/4");
+
+is(~((2 / 3) * (5 / 4)), "5/6", "2/3 * 5/4 = 5/6");
+is(~((2 / 3) * 2), "4/3", "2/3 * 2 = 4/3");
+is(~(2 * (2 / 3)), "4/3", "2 * 2/3 = 4/3");
+
+is(~((2 / 3) / (5 / 4)), "8/15", "2/3 / 5/4 = 8/15");
+is(~((2 / 3) / 2), "1/3", "2/3 / 2 = 1/3");
+is(~(2 / (1 / 3)), "3/2", "2 / 1/3 = 3/2");
+
+
+
+
done_testing;
# vim: ft=perl6
Index: t/spec/S02-literals/radix.t
===================================================================
--- t/spec/S02-literals/radix.t (revision 28156)
+++ t/spec/S02-literals/radix.t (working copy)
@@ -106,7 +106,7 @@
# L<S02/Literals/"Any radix may include a fractional part">
#?rakudo todo 'fractionals base 16'
-is(:16<dead_beef.face>, 0xDEAD_BEEF + 0xFACE / ( 16 ** 4 ), 'Fractional base 16 works' );
+is(:16<dead_beef.face>, 0xDEAD_BEEF + 0xFACE div ( 16 ** 4 ), 'Fractional base 16 works' );
# L<S02/Literals/":8<177777>">
@@ -225,7 +225,7 @@
is +":1_0<14_56>", 1456, "underscore seperators works";
is +":10<123.456>", 123.456, "base 10 decimal notation works";
is +":2<1.111>", 1.875, "base 2 decimal notation works";
- is +":16<dead_beef.face>", 0xDEADBEEF + 0xFACE / (16 ** 4), "fractional base 16 works";
+ is +":16<dead_beef.face>", 0xDEADBEEF + 0xFACE / 65536.0, "fractional base 16 works";
for 2..36 {
is +":{$_}<11>", $_ + 1, "stringified form of base $_ works";
Index: t/spec/S02-magicals/dollar_bang.t
===================================================================
--- t/spec/S02-magicals/dollar_bang.t (revision 28156)
+++ t/spec/S02-magicals/dollar_bang.t (working copy)
@@ -34,7 +34,7 @@
ok !$called, 'The subroutine also was not called';
undefine $!;
-try { 1 / 0 };
+try { 1 div 0 };
ok $!, 'Dividing one by zero sets $!';
sub incr ( $a is rw ) { $a++ };
|
From @moritzApplied + squashed + pushed as 39c3f4b6d5f2a702f336d33e79d23391adf36980. Cheers, and thanks for the patches. Moritz |
@moritz - Status changed from 'open' to 'resolved' |
Migrated from rt.perl.org#68898 (status was 'resolved')
Searchable as RT68898$
The text was updated successfully, but these errors were encountered: