File: anomaly.t

package info (click to toggle)
libmath-mpfr-perl 4.45-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,716 kB
  • sloc: perl: 1,508; ansic: 517; makefile: 9
file content (64 lines) | stat: -rwxr-xr-x 2,099 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64

# Here we check that a (somewhat anomalous) behaviour that can
# arise when strings are involved in overloaded comparisons with
# Math::MPFR objects ('==', '!=', '<', '>', '<=', '>=' and '<=>')
# is as expected.
# Some of these tests require Math::GMPz.

use strict;
use warnings;
use Math::MPFR;

use Config;
use Test::More;

my $have_mpz = 0;
eval {require Math::GMPz;};
$have_mpz++ unless $@;

if($Config{ivsize} == 8) {
   my $f = Math::MPFR->new(2 ** 64); # 0x1p+63
   my $i =  ~0; # == 18446744073709551615, 1 less than 2 ** 64

   cmp_ok($f, '>',   $i,  '2**64 >   18446744073709551615  (IV)'); # $i is evaluated to its full (64-bit) precision.
   cmp_ok($f, '==', "$i", '2**64 == "18446744073709551615" (PV)'); # "$i" is rounded to 53-bit precision.

   if($have_mpz) {
     # In order to have the string "$i" evaluated to its full 64-bit precision:
     cmp_ok($f, '>', Math::GMPz->new("$i"), '2**64 >  "18446744073709551615" (mpz)');
   }
}

if($Config{nvsize} > 8) {

  my $f = Math::MPFR->new(2 ** 63);       # 9.223372036854775808e18
  my $s = sprintf "%.20e", (2 ** 63) + 1; # 9.223372036854775809e18

  cmp_ok(  $f, '==', $s,     '2**63 == (2**63)+1 - RHS is PV');       # $s, (POK):             treated as PV

  if($Config{ivsize} < 8) {
    cmp_ok($f, '<',  $s + 0, '2**63 <  (2**63)+1 - RHS is NV');       # '$s + 0', (NOK):       treated as NV
    cmp_ok($f, '==', $s,     '2**63 == (2**63)+1 - RHS is again PV'); # $s, (now POK and NOK): treated as PV
  }
  else {
    cmp_ok($f, '<',  $s + 0, '2**63 <  (2**63)+1 - RHS is IV');       # '$s + 0', (IOK):       treated as IV
    cmp_ok($f, '<', $s,      '2**63 <  (2**63)+1 - RHS is still IV'); # $s, (now POK and IOK): treated as IV
  }

}

my $f = Math::MPFR->new(2 ** 70); # 1180591620717411303424
my $s = '1180591620717411303423'; # 1 less than 2 ** 70;


cmp_ok($f, '==', $s, '2**70 == (2**70)-1 - RHS is PV'); # Value of $s is rounded to 53-bit precision.

if($have_mpz) {
  # $s is evaluated to its full (70-bit) precision:
  cmp_ok($f, '>',  Math::GMPz->new($s), '2**70 >  (2**70)-1 - RHS is mpz');
}

done_testing();