File: arithmetic.mpt

package info (click to toggle)
mathpiper 0.81f%2Bsvn4469%2Bdfsg3-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 36,572 kB
  • sloc: java: 57,479; lisp: 13,721; objc: 1,300; xml: 988; makefile: 114; awk: 95; sh: 38
file content (166 lines) | stat: -rw-r--r-- 3,873 bytes parent folder | download | duplicates (4)
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166

NextTest("Test arithmetic");

NextTest("Basic calculations");
Verify(3 + 2 , 5);
Verify(3-7, -4);
Verify(1 = 2 , 0 = -1);
Verify(5 ^ 2 , 25);

Verify(IsZero(0.000),True);

Verify(2/5,Hold(2/5));
Verify(IsZero(N(2/5)-0.4));
Verify(IsRational(2),True);
Verify(IsRational(2/5),True);
Verify(IsRational(-2/5),True);
Verify(IsRational(2.0/5),False);
Verify(IsRational(Pi/2),False);
Verify(Numerator(2/5),2);
Verify(Denominator(2/5),5);

VerifyArithmetic(10,5,8);
VerifyArithmetic(10000000000,5,8);
VerifyArithmetic(10,50,80);
VerifyArithmetic(10000,50,88);

Verify(4!,24);
Verify(BinomialCoefficient(2,1),2);

NextTest("Testing math stuff");
Verify(1*a,a);
Verify(a*1,a);
Verify(0*a,0);
Verify(a*0,0);
Verify(aa-aa,0);

Verify(2+3,5);
Verify(2*3,6);

Verify(2+3*4,14);
Verify(3*4+2,14);
Verify(3*(4+2),18);
Verify((4+2)*3,18);

Verify(15/5,3);

Verify(-2+3,1);
Verify(-2.01+3.01,1.);

Verify(0+a,a);
Verify(a+0,a);
Verify(aa-aa,0);

Testing("IntegerOperations");
Verify(1<<10,1024);
Verify(1024>>10,1);
Verify(Modulo(10,3),1);
Verify(Quotient(10,3),3);
Verify(GcdN(55,10),5);

Verify(Modulo(2,Infinity),2);
Verify(Modulo({0,1,2,3,4,5,6},2),{0,1,0,1,0,1,0});
Verify(Modulo({0,1,2,3,4,5,6},{2,2,2,2,2,2,2}),{0,1,0,1,0,1,0});

Testing("PowerN");
// was broken in the gmp version
Verify(PowerN(19, 0), 1);
Verify(PowerN(1, -1), 1);
Verify(PowerN(1, -2), 1);
Verify(IsZero(PowerN(10, -2)- 0.01));
Verify(PowerN(2, 3), 8);
NumericEqual(PowerN(2, -3), 0.125,BuiltinPrecisionGet()); 

Testing("Rounding");
Verify(Floor(1.2),1);
Verify(Floor(-1.2),-2);
Verify(Ceil(1.2),2);
Verify(Ceil(-1.2),-1);
Verify(Round(1.49),1);
Verify(Round(1.51),2);
Verify(Round(-1.49),-1);
Verify(Round(-1.51),-2);

Testing("Bases");
Verify(ToBase(16,255),"ff");
Verify(FromBase(2,"100"),4);

// conversion between decimal and binary digits
Verify(BitsToDigits(2000, 10), 602);
Verify(DigitsToBits(602, 10), 2000);

LocalSymbols(f,ft)
[
  f(x,y):=(Quotient(x,y)*y+Rem(x,y)-x);
  ft(x,y):=
  [
    Verify(f(x,y),0);
    Verify(f(-x,y),0);
    Verify(f(x,-y),0);
    Verify(f(-x,-y),0);
  ];
  ft(10,4);
  ft(2.5,1.2);
];

Testing("Factorization");
Verify(
Eval(Factors(447738843))
, {{3,1},{17,1},{2729,1},{3217,1}}
);


//Exponential notation is now supported in the native arithmetic library too...
Verify(2e3+1,2001.);
Verify(2.0e3+1,2001.);
Verify(2.00e3+1,2001.);
Verify(2.000e3+1,2001.);
Verify(2.0000e3+1,2001.);

Verify(1+2e3,2001.);
Verify(1+2.0e3,2001.);
Verify(1+2.00e3,2001.);
Verify(1+2.000e3,2001.);
Verify(1+2.0000e3,2001.);

NumericEqual(N(Sqrt(1e4))-100,0,BuiltinPrecisionGet());
NumericEqual(N(Sqrt(1.0e4))-100,0,BuiltinPrecisionGet());

Verify(2.0000e3-1,1999.);
[
  Local(p);
  p:=BuiltinPrecisionGet();
  BuiltinPrecisionSet(12);//TODO this will fail if you drop precision to below 12, for some reason.
  NumericEqual(RoundToPrecision(10e3*1.2e-3,BuiltinPrecisionGet()),12.,BuiltinPrecisionGet());
  BuiltinPrecisionSet(p);
];
Verify((10e3*1.2e-4)-1.2,0);

Verify(IsZero(N(Sin(0.1e1)-Sin(1),30)),True);
[
  /* In Dutch they have a saying "dit verdient geen schoonheidsprijs" ;-) We need to sort this out.
   * But a passable result, for now.
   */
  Local(diff);
  diff := N(Sin(10e-1)-Sin(1),30);
//BuiltinPrecisionSet(20);
//Echo("diff = ",diff);
//Echo("diff > -0.00001 = ",diff > -0.00001);
//Echo("diff < 0.00001 = ",diff < 0.00001);
  Verify(diff > -0.00001 And diff < 0.00001,True);
];


/* Jonathan reported a problem with Simplify(-Sqrt(8)/2), which returned some
 * complex expression containing greatest common divisors of square roots.
 * This was fixed by adding some rules dealing with taking the gcd of two objects
 * where at least one is a square root.
 */
Verify(-Sqrt(8)/2,-Sqrt(2));
Verify(Sqrt(8)/2,Sqrt(2));
Verify(Gcd(Sqrt(2),Sqrt(2)),Sqrt(2));
Verify(Gcd(-Sqrt(2),-Sqrt(2)),Sqrt(2));
Verify(Gcd(Sqrt(2),-Sqrt(2)),Sqrt(2));
Verify(Gcd(-Sqrt(2),Sqrt(2)),Sqrt(2));