File: _e_math.t

package info (click to toggle)
perl 5.8.4-8sarge6
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 58,128 kB
  • ctags: 31,422
  • sloc: perl: 224,262; ansic: 155,398; sh: 32,253; pascal: 7,747; lisp: 6,121; makefile: 2,341; cpp: 2,035; yacc: 1,019; java: 23
file content (111 lines) | stat: -rwxr-xr-x 2,703 bytes parent folder | download
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/perl -w

# test the helper math routines in Math::BigFloat

use Test::More;
use strict;

BEGIN
  {
  $| = 1;
  # to locate the testing files
  my $location = $0; $location =~ s/_e_math.t//i;
  if ($ENV{PERL_CORE})
    {
    # testing with the core distribution
    @INC = qw(../lib);
    }
  unshift @INC, '../lib';
  if (-d 't')
    {
    chdir 't';
    require File::Spec;
    unshift @INC, File::Spec->catdir(File::Spec->updir, $location);
    }
  else
    {
    unshift @INC, $location;
    }
  print "# INC = @INC\n";

  plan tests => 26;
  }

use Math::BigFloat;

#############################################################################
# add

my $a = Math::BigInt::Calc->_new("123");
my $b = Math::BigInt::Calc->_new("321");

my ($x, $xs) = Math::BigFloat::_e_add($a,$b,'+','+');
is (_str($x,$xs), '+444', 'add two positive numbers');
is (_str($a,''), '444', 'a modified');

($x,$xs) = _add (123,321,'+','+');
is (_str($x,$xs), '+444', 'add two positive numbers');

($x,$xs) = _add (123,321,'+','-');
is (_str($x,$xs), '-198', 'add +x + -y');
($x,$xs) = _add (123,321,'-','+');
is (_str($x,$xs), '+198', 'add -x + +y');

($x,$xs) = _add (321,123,'-','+');
is (_str($x,$xs), '-198', 'add -x + +y');
($x,$xs) = _add (321,123,'+','-');
is (_str($x,$xs), '+198', 'add +x + -y');

($x,$xs) = _add (10,1,'+','-');
is (_str($x,$xs), '+9', 'add 10 + -1');
($x,$xs) = _add (10,1,'-','+');
is (_str($x,$xs), '-9', 'add -10 + +1');
($x,$xs) = _add (1,10,'-','+');
is (_str($x,$xs), '+9', 'add -1 + 10');
($x,$xs) = _add (1,10,'+','-');
is (_str($x,$xs), '-9', 'add 1 + -10');

#############################################################################
# sub

$a = Math::BigInt::Calc->_new("123");
$b = Math::BigInt::Calc->_new("321");
($x, $xs) = Math::BigFloat::_e_sub($b,$a,'+','+');
is (_str($x,$xs), '+198', 'sub two positive numbers');
is (_str($b,''), '198', 'a modified');

($x,$xs) = _sub (123,321,'+','-');
is (_str($x,$xs), '+444', 'sub +x + -y');
($x,$xs) = _sub (123,321,'-','+');
is (_str($x,$xs), '-444', 'sub -x + +y');

sub _add
  {
  my ($a,$b,$as,$bs) = @_;

  my $aa = Math::BigInt::Calc->_new($a);
  my $bb = Math::BigInt::Calc->_new($b);
  my ($x, $xs) = Math::BigFloat::_e_add($aa,$bb,$as,$bs);
  is (Math::BigInt::Calc->_str($x), Math::BigInt::Calc->_str($aa),
    'param0 modified');
  ($x,$xs);
  }

sub _sub
  {
  my ($a,$b,$as,$bs) = @_;

  my $aa = Math::BigInt::Calc->_new($a);
  my $bb = Math::BigInt::Calc->_new($b);
  my ($x, $xs) = Math::BigFloat::_e_sub($aa,$bb,$as,$bs);
  is (Math::BigInt::Calc->_str($x), Math::BigInt::Calc->_str($aa),
    'param0 modified');
  ($x,$xs);
  }

sub _str
  {
  my ($x,$s) = @_;

  $s . Math::BigInt::Calc->_str($x);
  }