This repository has been archived by the owner on Aug 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
t_ccalc.pas
286 lines (257 loc) · 8.2 KB
/
t_ccalc.pas
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
{Test program for mp_ccalc unit, (c) W.Ehrhardt 2018}
program t_ccalc;
{$i STD.INC}
{$i mp_conf.inc}
{$x+} {pchar I/O}
{$i+} {RTE on I/O error}
{$ifdef BIT16}
{$N+}
{$endif}
{$ifdef Delphi}
{$J+}
{$endif}
{$ifdef APPCONS}
{$apptype console}
{$endif}
{$ifndef MPC_NOHALT}
{$message error 't_ccalc should be compiled with -dMPC_NOHALT'}
{$endif}
uses
BTypes, HRTimer,
{$ifdef MPC_Diagnostic}
mp_supp,
{$endif}
mp_types, mp_base, mp_real, mp_cmplx, mp_ccalc;
{---------------------------------------------------------------------------}
function HexLong(L: longint): mp_string;
{-longint as hex string, LSB first}
var
i: integer;
s: string[8];
begin
s := '';
for i:=0 to 7 do begin
s := mp_ucrmap[L and $F] + s;
L := L shr 4;
end;
HexLong := s;
end;
{---------------------------------------------------------------------------}
procedure ShowInfo;
begin
writeln;
{$ifdef MPC_E1Ln10Tab}
writeln('Operators: + - * / ^ Constants: i, pi, e, ln2, ln10');
{$else}
writeln('Operators: + - * / ^ Constants: i, pi, ln2');
{$endif}
writeln;
writeln('Functions: abs(a) agm(a,b) arccos(a) arccosh(a) arccot(a)');
writeln(' arccotc(a) arccoth(a) arccsc(a) arccsch(a) arcsec(a)');
writeln(' arcsech(a) arcsin(a) arcsinh(a) arctan(a) arctanh(a)');
writeln(' arg(a) conj(a) cos(a) cosh(a) cot(a)');
writeln(' coth(a) csc(a) csch(a) exp(a) expm1(a)');
writeln(' im(a) ln(a) ln1p(a) log10(a) nroot(a,n)');
writeln(' re(a) sec(a) sech(a) sin(a) sinh(a)');
writeln(' sqr(a) sqrt(a) tan(a) tanh(a)');
writeln;
writeln('Variables: x y z, Syntax: Var=expression[;] or Var=_[;]');
writeln;
writeln('Other : line terminator ";" shows only chksum/time of result');
writeln(' sci, alt: display results using scientific/alternative format');
writeln(' prec [n]: display/set bit precision');
writeln(' nfd [n]: display/set number of digits in fractional part');
writeln(' "_<enter>" re-displays last result');
{$ifdef CPUARM}
writeln(' ".<enter>" displays time for last calculation; the ARM version');
writeln(' does not use TSC and is not high-precision.');
{$else}
writeln(' ".<enter>" displays time for last calculation');
{$endif}
writeln;
end;
{---------------------------------------------------------------------------}
procedure HelpLine;
begin
writeln('Type "?<enter>" to get some info about commands, "\q" or "quit" to end.');
end;
const
nfd0: word = 50;
maxnfd = 200;
var
evr: TFEval;
{$ifdef VirtualPascal}
ir: longint;
{$else}
ir: integer;
{$endif}
EP,i: integer;
HR: THRTimer;
dp,ctime: double;
ac: char8;
use_sci,print: boolean;
s: string[255];
resprefix: string[8];
cmd: string[20];
nfd: word;
prec,n: longint;
begin
{mp_verbose := 3;}
StartTimer(HR);
mpc_init_eval(evr);
mp_uppercase := true;
use_sci := false;
ctime := 0.0;
nfd := nfd0;
writeln('Test of MPArith V', MP_VERSION, ' [mp_ccalc] (c) W.Ehrhardt 2008-2018');
writeln('Karatsuba cutoffs: mul/sqr = ',mp_mul_cutoff,'/',mp_sqr_cutoff);
writeln('Toom-3, BZ cutoffs: mul/sqr = ',mp_t3m_cutoff,'/',mp_t3s_cutoff, ', div = ',mp_bz_cutoff);
HelpLine;
prec := mpf_get_default_prec;
dp :=prec*ln(2)/ln(10);
writeln('Current bit precision = ', prec, ' (max:',MPF_MAX_PREC, '), decimal precision = ', dp:1:1);
writeln;
repeat
ac := #0;
write('=> ');
readln(s);
while (s<>'') and (s[1]=' ') do delete(s,1,1);
while (s<>'') and (s[length(s)]=' ') do delete(s,length(s),1);
if s='' then continue;
if s='?' then begin
ShowInfo;
continue;
end;
if s='.' then begin
writeln('Time = ', ctime:1:3, ' ms');
continue;
end;
if s[1]='"' then begin
delete(s,1,1);
writeln(s);
continue;
end;
cmd := s;
for i:=1 to length(cmd) do cmd[i] := upcase(cmd[i]);
if (cmd='\Q') or (cmd='Q') or (cmd='QUIT') or (cmd='\@') then break;
if (cmd='ALT') or (cmd='SCI') then begin
use_sci := cmd='SCI';
continue;
end;
if copy(cmd,1,4)='PREC' then begin
if length(cmd)=4 then begin
prec := mpf_get_default_prec;
dp := prec*ln(2)/ln(10);
writeln('Current bit precision = ', prec, ', decimal precision = ', dp:1:1);
continue;
end;
delete(cmd,1,4);
while (cmd<>'') and (cmd[1]=' ') do delete(cmd,1,1);
{$ifdef D12Plus}
val(string(cmd),prec,ir);
{$else}
val(cmd,prec,ir);
{$endif}
if (ir=0) and (prec>=MPF_MIN_PREC) then begin
if prec>MPF_MAX_PREC then begin
prec := MPF_MAX_PREC;
write('** prec > ',MPF_MAX_PREC, ' - ');
end;
mpf_set_default_prec(prec);
prec := mpf_get_default_prec;
dp :=prec*ln(2)/ln(10);
if dp<nfd0 then nfd := trunc(dp) else nfd := nfd0;
mpf_chg_prec(evr.Res.re, prec);
mpf_chg_prec(evr.Res.im, prec);
writeln('New bit precision = ', prec, ', decimal precision = ', dp:1:1);
continue;
end;
end;
if copy(cmd,1,3)='NFD' then begin
if length(cmd)=3 then begin
writeln('Number of digits in fractional part = ', nfd0);
continue;
end;
delete(cmd,1,3);
while (cmd<>'') and (cmd[1]=' ') do delete(cmd,1,1);
{$ifdef D12Plus}
val(string(cmd),n,ir);
{$else}
val(cmd,n,ir);
{$endif}
if (ir=0) and (n>=10) and (n<=maxnfd) then begin
nfd := n;
nfd0 := n;
writeln('Number of digits in fractional part = ', nfd0);
end
else writeln('Invalid number [10 <= nfd <= ',maxnfd);
continue;
end;
{Check for trailing ";", i.e. dont print the result}
if (s<>'') and (s[length(s)]=';') then begin
delete(s,length(s),1);
while (s<>'') and (s[length(s)]=' ') do delete(s,length(s),1);
print := false;
end
else print := true;
if s='' then continue;
if s<>'_' then begin
s := s + #0;
ac := #0;
if (length(s)>1) and (upcase(s[1]) in ['X','Y','Z']) then begin
{Check if we have an assignment to x,y,z. Analyse first non-blank}
{char; next while/if statements are OK because s has trailing #0}
while (s[2]=' ') and (s[3]=' ') do delete(s,2,1);
if (s[2]=' ') and (s[3]='=') then delete(s,2,1);
if s[2]='=' then begin
ac := upcase(s[1]);
delete(s,1,2);
while (s<>'') and (s[1]=' ') do delete(s,1,1);
end;
end;
if s<>'_'#0 then begin
RestartTimer(HR);
mpc_calculate(@s[1],evr,EP);
ctime := 1000.0*ReadSeconds(HR);
if evr.Err=Err_MPERR_Eval then set_mp_error(MP_OKAY);
end;
end
else if evr.Err>0 then continue;
if evr.Err>0 then begin
writeln('Error ', evr.Err,', ',mpc_calc_errorstr(evr.Err),': <',copy(s,EP+1,length(s)-EP-1), '>');
if evr.Err=Err_Unknown_Function then HelpLine;
end
else if evr.Err<0 then writeln(#7'Error: ', evr.Err, ' [',mpc_calc_errorstr(evr.Err),']')
else begin
resprefix := 'Result';
if ac in ['X', 'Y', 'Z'] then begin
case ac of
'X': mpc_copy(evr.Res,evr.X);
'Y': mpc_copy(evr.Res,evr.Y);
'Z': mpc_copy(evr.Res,evr.Z);
end;
resprefix := ac;
end;
if print then begin
if use_sci then begin
write(resprefix,'.re = '); mpf_output_decimal(evr.Res.re, nfd); writeln;
write(resprefix,'.re = '); mpf_output_decimal(evr.Res.im, nfd); writeln;
end
else begin
write(resprefix,'.re = '); mpf_output_decimal_alt(evr.Res.re, nfd); writeln;
write(resprefix,'.im = '); mpf_output_decimal_alt(evr.Res.im, nfd); writeln;
end;
end
else begin
write('[ chksum=$', HexLong(mpc_checksum(evr.Res)),', time=', ctime:1:3, ' ms]');
end;
end;
evr.Err := 0;
writeln;
until false;
mpc_clear_eval(evr);
{$ifdef MPC_Diagnostic}
mp_dump_meminfo;
mp_dump_diagctr;
{$endif}
end.