This repository has been archived by the owner on Jun 1, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Promote on overflow to bigint, and not to NV. This is a new lexical user-pragma to use exact arithmetic without loosing precision on all builtin arithmetic ops. As in perl6, just much faster. (only in the rare 1% overflow case) It is of course a bit slower than without, but I could not measure any difference. See #21.
- Loading branch information
Showing
18 changed files
with
458 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package exact_arith; | ||
our $VERSION = '0.01'; | ||
my $HINT_EXACT_ARITH = 0x0000010; # see perl.h | ||
|
||
sub import { | ||
use Math::BigInt try => 'GMP'; | ||
#$^H{exact_arith} = 1; | ||
$^H |= $HINT_EXACT_ARITH; | ||
} | ||
sub unimport { | ||
#delete $^H{exact_arith}; | ||
$^H &= ~$HINT_EXACT_ARITH; | ||
} | ||
|
||
1; | ||
__END__ | ||
=head1 NAME | ||
exact_arith - promote on overflow to bigint/num | ||
=head1 SYNOPSIS | ||
use exact_arith; | ||
print 18446744073709551614 * 2; # => 36893488147419103228, a Math::BigInt object | ||
{ no exact_arith; | ||
print 18446744073709551614 * 2; # => 3.68934881474191e+19 | ||
} | ||
=head1 DESCRIPTION | ||
This is a new lexical user-pragma since cperl 5.32 to use exact | ||
arithmetic, without loosing precision on all builtin arithmetic ops. | ||
As in perl6. | ||
It is of course a bit slower than without, but it's much faster than | ||
perl6, since it only does use bigint on IV/UV overflows which do | ||
happen very seldom. | ||
=cut |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#!./perl -- -*- mode: cperl; cperl-indent-level: 4 -*- | ||
|
||
BEGIN { | ||
chdir 't' if -d 't'; | ||
@INC = ( '.', '../lib' ); | ||
} | ||
|
||
use strict; | ||
require '../t/test.pl'; | ||
plan(8); | ||
|
||
$|=1; | ||
my $a = 18446744073709551614; | ||
|
||
# test it at compile-time in constant folding | ||
use exact_arith; | ||
my $n = 18446744073709551614 * 2; # => 36893488147419103228, Math::BigInt or *::GMP | ||
like(ref $n, qr/^Math::BigInt/, '* type (c)'); | ||
ok($n eq '36893488147419103228', '* val (c)') or | ||
is($n, '36893488147419103228'); | ||
|
||
{ | ||
no exact_arith; | ||
my $m = 18446744073709551614 * 2; | ||
is(ref $m, '', '* no type (c)'); | ||
is($m, 3.68934881474191e+19, '* no val (c)'); | ||
} | ||
|
||
my $two = 2; | ||
$n = 18446744073709551614 * $two; # run-time | ||
like(ref $n, qr/^Math::BigInt/, '* type (r)'); | ||
ok($n eq '36893488147419103228', '* val (r)') or | ||
is($n, '36893488147419103228'); | ||
|
||
{ | ||
no exact_arith; | ||
my $m = 18446744073709551614 * $two; | ||
is(ref $m, '', '* no type (r)'); | ||
is($m, 3.68934881474191e+19, '* no val (r)'); | ||
} | ||
|
||
my $c = 18446744073709551614 + 10000; | ||
like(ref $c, qr/^Math::BigInt/, '+ type (c)'); | ||
my $r = $a + 10000; | ||
like(ref $r, qr/^Math::BigInt/, '+ type (r)'); | ||
|
||
$c = 18446744073709551624 - 2; | ||
like(ref $c, qr/^Math::BigInt/, '- type (c)'); | ||
$r = $c - 1; | ||
like(ref $r, qr/^Math::BigInt/, '- type (r)'); | ||
|
||
$c = 1844674407370955162400 / 0.3; | ||
like(ref $c, qr/^Math::BigInt/, '/ type (c)'); | ||
$r = 1844674407370955162400 / 0.3; | ||
like(ref $r, qr/^Math::BigInt/, '/ type (r)'); | ||
|
||
$c = 18446744073709551614 ** 2; | ||
like(ref $c, qr/^Math::BigInt/, '** type (c)'); | ||
$r = $a ** 2; | ||
like(ref $r, qr/^Math::BigInt/, '** type (r)'); | ||
|
||
$r = $a++; | ||
like(ref $r, qr/^Math::BigInt/, '++ type (r)'); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.