-
Notifications
You must be signed in to change notification settings - Fork 0
/
mpmod90.f90
7561 lines (6715 loc) · 191 KB
/
mpmod90.f90
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
module mpdefmod
! MPFUN-90 translation modules.
! IEEE Fortran-90 version
! David H Bailey 2010-07-16
! Author:
! David H. Bailey
! NERSC, Lawrence Berkeley Lab
! Mail Stop 50B-2239
! Berkeley, CA 94720
! Email: dhbailey@lbl.gov
! This work was supported by the Director, Office of Science, Division
! of Mathematical, Information, and Computational Sciences of the
! U.S. Department of Energy under contract number DE-AC03-76SF00098.
! See README file accompanying this software for other legal details.
! A detailed description of this package, and instructions for compiling
! and testing this program on various specific systems are included in the
! README file that accompanies this file.
! The following notational scheme is used to designate datatypes below:
! A Alphabetic [i.e. ASCII]
! D Double precision or real*8 [i.e. REAL (KIND (0.D0))]
! I Integer
! J MP integer
! Q MP real
! X Double complex or real*16 [i.e. COMPLEX (KIND (0.D0))]
! Z MP complex
! Note that ordinary real*4 and complex*8 types are not included -- if
! you have code with these datatypes, convert them to real*8 and complex*16.
! The following parameters are all that need to be changed in normal usage:
! MPIPL Initial precision level, in digits.
! MPIOU Initial output precision level, in digits.
! MPIEP Log_10 of initial MP epsilon level.
use mpfunmod
implicit none
integer mpipl, mpiou, mpiep, mpwds
parameter (mpipl = 2005, mpiou = 56, mpiep = 10 - mpipl)
!----------------------------------------------------------------------------
! *** No code below this point needs to be altered in normal usage.
parameter (mpwds = mpipl / 7.224719896d0 + 2.d0)
integer, private:: kdb, mp4, mp24, mp41
parameter (kdb = kind (0.d0), mp4 = mpwds + 4, mp24 = 2 * mp4, mp41 = mp4 + 1)
type mp_integer
sequence
real mpi(mp4)
end type
type mp_real
sequence
real mpr(mp4)
end type
type mp_complex
sequence
real mpc(mp24)
end type
type (mp_real), public:: mpl02, mpl10, mppic, mpeps, mplrg, mpsml
integer, public:: mpnwx, mpoud, new_mpipl, new_mpwds
contains
subroutine mpinit (n_mpipl)
! MPINIT must be called at the start of execution in the user's main program.
! It sets the numeric precision level, the MP epsilon level, the output
! precision level, and computes the constants Pi, Log(2) and Log(10) and
! trigonometric factors for the cos/sin routine. Note that the numeric
! precision level MPNW is temporarily set to MPNWX + 1, in order to permit
! extra-accurate calculations of the constants, and then is reset to MPNWX.
integer, optional:: n_mpipl
integer i, mpnw
real*4 t0(mp4+1), t1(mp4+1), t2(mp4+1), t3(mp4+1), t4(mp4+1)
if (present (n_mpipl)) then
if (n_mpipl > mpipl) then
write (mpldb, *) 'mpinit: argument too large'
stop
endif
new_mpipl = n_mpipl
new_mpwds = n_mpipl / 7.224719896d0 + 2.d0
mpnwx = new_mpwds
else
new_mpipl = mpipl
new_mpwds = mpwds
mpnwx = new_mpwds
endif
! Compute log2, log10 and pi.
mpier = 0
mpnw = mpnwx + 1
call mppi (t1, mpnw)
call mpdmc (2.d0, 0, t0)
call mplog (t0, t2, t2, mpnw)
call mpdmc (10.d0, 0, t0)
call mplog (t0, t2, t3, mpnw)
call mpnpwr (t0, mpiep, t4, mpnw)
mpnw = mpnwx
call mpeq (t1, mppic%mpr, mpnw)
call mpeq (t2, mpl02%mpr, mpnw)
call mpeq (t3, mpl10%mpr, mpnw)
call mpeq (t4, mpeps%mpr, mpnw)
! Allocate and compute the mpcosq and mpsinq arrays, needed for cos and sin.
call mpiniq (mpwds, mpnw)
! Set mpoud, mplrg and mpsml.
mpoud = mpiou
mplrg%mpr(1) = 1.
mplrg%mpr(2) = 2.e6
mplrg%mpr(3) = 1.
mplrg%mpr(4) = 0.
mpsml%mpr(1) = 1.
mpsml%mpr(2) = -2.e6
mpsml%mpr(3) = 1.
mpsml%mpr(4) = 0.
return
end subroutine
subroutine mpsetprec (num_digits)
integer num_digits
if (num_digits > new_mpipl) then
write (mpldb, *) 'mpsetprec: invalid argument; precision set to ', &
new_mpipl, ' digits'
mpnwx = new_mpwds
else
mpnwx = num_digits / 7.224719896d0 + 2.d0
endif
end subroutine
subroutine mpgetprec (num_digits)
integer num_digits
num_digits = (mpnwx - 2) * 7.224719896d0
end subroutine
subroutine mpsetprecwords (num_words)
integer num_words
if (num_words > new_mpwds) then
write (mpldb, *) 'mpsetprecwords: invalid argument; precision set to ', &
new_mpwds, ' words'
mpnwx = new_mpwds
else
mpnwx = num_words
endif
end subroutine
subroutine mpgetprecwords (num_words)
integer num_words
num_words = mpnwx
end subroutine
subroutine mpsetoutputprec (num_digits)
integer num_digits
if (num_digits > new_mpipl) then
write (mpldb, *) &
'mpsetoutputprec: invalid argument; output precision set to ', &
new_mpipl, ' digits'
mpoud = new_mpipl
else
mpoud = num_digits
endif
end subroutine
subroutine mpgetoutputprec (num_digits)
integer num_digits
num_digits = mpoud
end subroutine
subroutine mpgetpar (s, n, k)
! MPGETPAR retrieves some ARPREC C++ integer parameters.
character*(*), intent(in) :: s
integer, intent(out) :: n
integer, intent(in), optional :: k
if (s == 'mpnw') then
n = mpnwx
elseif (s == 'mpidb') then
n = mpidb
elseif (s == 'mpndb') then
n = mpndb
elseif (s == 'mpmcr') then
n = mpmcr
elseif (s == 'mpird') then
n = mpird
elseif (s == 'mpier') then
n = mpier
elseif (s == 'mpker') then
n = mpker(k)
else
write (mpldb, 1) s
1 format ('mpgetpar: invalid parameter name: ',a)
n = 0
endif
end subroutine
subroutine mpsetpar (s, n, k)
! MPSETPAR sets some ARPREC C++ integer parameters.
character*(*), intent(in) :: s
integer, intent(in) :: n
integer, intent(in), optional :: k
if (s == 'mpnw') then
mpnwx = n
elseif (s == 'mpidb') then
mpidb = n
elseif (s == 'mpndb') then
mpndb = n
elseif (s == 'mpmcr') then
mpmcr = n
elseif (s == 'mpird') then
mpird = n
elseif (s == 'mpier') then
mpier = n
elseif (s == 'mpker') then
mpker(k) = n
else
write (mpldb, 1) s
1 format ('mpsetpar: invalid parameter name: ',a)
endif
end subroutine
subroutine mpeform (a, n1, n2, b)
type (mp_real) a
integer n1, n2
character*1 b(n1)
integer mpnw
mpnw = mpnwx
call mpeformx (a%mpr, n1, n2, b, mpnw)
return
end subroutine
subroutine mpfform (a, n1, n2, b)
type (mp_real) a
integer n1, n2
character*1 b(n1)
integer mpnw
mpnw = mpnwx
call mpfformx (a%mpr, n1, n2, b, mpnw)
return
end subroutine
subroutine mpdexc (a, l, b, mpnw)
! This routine converts the character*1 string A, which
! represents a multiprecision number in Fortran style, i.e.
! '1234567890' or '1.23456789D-21', into standard MP binary format.
! This routine is not intended to be called directly by the user.
integer i, i1, l, l1, l2, mpnw
character*1 a(l), c(mpipl+100)
real b(mpnw+4)
do i = 1, l
if (a(i) .eq. 'D' .or. a(i) .eq. 'E' .or. a(i) .eq. 'd' &
.or. a(i) .eq. 'e') goto 100
enddo
call mpinpc (a, l, b, mpnw)
goto 110
100 i1 = i
l1 = i - 1
l2 = l - i
c(1) = '1'
c(2) = '0'
c(3) = '^'
do i = 1, l2
c(i+3) = a(i+i1)
enddo
c(l2+4) = 'x'
do i = 1, l1
c(i+l2+4) = a(i)
enddo
call mpinpc (c, l1 + l2 + 4, b, mpnw)
110 return
end subroutine
subroutine mpinp (iu, a, cs, mpnw)
! This routine reads the MP number A from logical unit IU. CS is a scratch
! array of type CHARACTER*1. CS must be dimensioned at least 7.225*MPNW
! + 100. The digits of A may span more than one line. A comma at the end
! of the last line denotes the end of the MP number. The input lines may not
! exceed 200 characters in length. Embedded blanks are allowed anywhere.
! However, if the input number contains more than 80 embedded blanks, then
! the dimension of CS must be increased by a corresponding amount. The
! exponent is optional in the input number, but if present it must appear
! first. Two examples:
! 1073741824.,
! 10 ^ -4 x 3.14159 26535 89793 23846 26433 83279
! 50288 41971 69399 37510,
! Max SP space for A: MPNW + 4 cells.
integer i, iu, l, l1, mpnw, nn
character*200 line
character*1 cs(*)
real a(mpnw+2)
if (mpier .ne. 0) then
if (mpier .eq. 99) call mpabrt
a(1) = 0.
a(2) = 0.
return
endif
l = 0
nn = mpipl + 100
100 continue
read (iu, '(A)', end = 200) line
do i = 200, 1, -1
if (line(i:i) .ne. ' ') goto 110
enddo
i = 0
goto 100
110 continue
l1 = i
do i = 1, l1
if (line(i:i) == ',') goto 150
if (l + 1 <= nn) then
l = l + 1
cs(l) = line(i:i)
endif
enddo
goto 100
150 continue
call mpinpc (cs, l, a, mpnw)
goto 300
200 continue
if (mpker(72) .ne. 0) then
write (mpldb, 1)
1 format ('*** MPINP: End-of-file encountered.')
mpier = 72
if (mpker(mpier) .eq. 2) call mpabrt
endif
300 return
end subroutine
subroutine mpinpc (a, n, b, mpnw)
! Converts the CHARACTER*1 array A of length N into the MP number B. The
! string A must be in the format '10^s a x tb.c' where a, b and c are digit
! strings; s and t are '-', '+' or blank; x is either 'x' or '*'. Blanks may
! be embedded anywhere. The digit string a is limited to nine digits and
! 80 total characters, including blanks. The exponent portion (i.e. the
! portion up to and including x) and the period may optionally be omitted.
! Debug output starts with MPIDB = 7.
! Max SP space for B: MPNW + 4 cells.
! The following example shows how this routine may be used to input a MP
! number:
! CHARACTER*1 CX(800)
! READ (1, '(80A1)') (CX(I), I = 1, ND)
! CALL MPINPC (CX, ND, B)
integer i, ib, id, ier, ip, is, it, i1, i2, k0, k1, k2, l1, mpnw, n, nb, &
nn, no, nws, n5
double precision bi
character*1 a(n), ai
character*10 dig
character*80 ca
parameter (dig = '0123456789')
real b(mpnw+4), f(8), s(3*mpnw+15)
! real*8 mpdigin
if (mpier .ne. 0) then
if (mpier .eq. 99) call mpabrt
b(1) = 0.
b(2) = 0.
return
endif
if (mpidb .ge. 7) then
no = min (n, int (7.225 * mpndb) + 20)
write (mpldb, 1) (a(i), i = 1, no)
1 format ('MPINPC I'/(78a1))
endif
n5 = mpnw + 5
k0 = 1
k1 = k0 + n5
k2 = k1 + n5
nws = mpnw
mpnw = mpnw + 1
i1 = 1
nn = 0
! Find the carat, period, plus or minus sign, whichever comes first.
do i = 1, n
ai = a(i)
if (ai .eq. '^') goto 110
if (ai .eq. '.' .or. ai .eq. '+' .or. ai .eq. '-') goto 160
enddo
goto 160
! Make sure number preceding the carat is 10.
110 continue
i2 = i - 1
if (i2 .gt. 80) then
ier = 1
goto 210
endif
ca = ' '
do i = 1, i2
ai = a(i)
if (ai .eq. ' ') then
goto 120
elseif (index (dig, ai) .eq. 0) then
ier = 2
goto 210
endif
ca(i:i) = ai
120 continue
enddo
nn = mpdigin (ca, 80)
if (nn .ne. 10) then
ier = 3
goto 210
endif
i1 = i2 + 2
! Find the x or *.
do i = i1, n
ai = a(i)
if (ai .eq. 'x' .or. ai .eq. '*') goto 140
enddo
ier = 4
goto 210
! Convert the exponent.
140 i2 = i - 1
l1 = i2 - i1 + 1
if (l1 .gt. 80) then
ier = 5
goto 210
endif
ca = ' '
id = 0
is = 1
do i = 1, l1
ai = a(i+i1-1)
if (ai .eq. ' ' .or. ai .eq. '+') then
goto 150
elseif (ai .eq. '-' .and. id .eq. 0) then
id = 1
is = -1
ca(i:i) = ' '
else
if (index (dig, ai) .eq. 0) then
ier = 6
goto 210
endif
id = 1
ca(i:i) = ai
endif
150 continue
enddo
nn = is * mpdigin (ca, 80)
i1 = i2 + 2
! Find the next nonblank character.
160 do i = i1, n
if (a(i) .ne. ' ') goto 180
enddo
ier = 7
goto 210
! Check if the nonblank character is a plus or minus sign.
180 continue
i1 = i
if (a(i1) .eq. '+') then
i1 = i1 + 1
is = 1
elseif (a(i1) .eq. '-') then
i1 = i1 + 1
is = -1
else
is = 1
endif
nb = 0
ib = 0
id = 0
ip = 0
s(k2) = 0.
s(k2+1) = 0.
f(1) = 1.
f(2) = 0.
it = 0
190 continue
ip = 0
ca(1:6) = '000000'
! Scan for digits, looking for the period also. On the first pass we just
! count, so that on the second pass it will come out right.
do i = i1, n
ai = a(i)
if (ai .eq. ' ') then
elseif (ai .eq. '.') then
if (ip .ne. 0) then
ier = 8
goto 210
endif
ip = id
elseif (index (dig, ai) .eq. 0) then
ier = 9
goto 210
else
ib = ib + 1
id = id + 1
ca(ib:ib) = ai
endif
if (ib .eq. 6 .or. i .eq. n .and. ib .ne. 0) then
if (it .ne. 0) then
nb = nb + 1
bi = mpdigin (ca(1:6), 6)
call mpmuld (s(k2), 1.d6, 0, s(k0), mpnw)
if (bi .ne. 0) then
f(1) = 1.
f(3) = bi
else
f(1) = 0.
endif
call mpadd (s(k0), f, s(k2), mpnw)
ca(1:6) = '000000'
endif
if (i .ne. n) ib = 0
endif
enddo
if (it .eq. 0) then
ib = 6 - ib
if (ib .eq. 6) ib = 0
it = 1
goto 190
endif
if (is .eq. -1) s(k2) = - s(k2)
if (ip .eq. 0) ip = id
nn = nn + ip - id
f(1) = 1.
f(3) = 10.
call mpnpwr (f, nn, s(k0), mpnw)
call mpmul (s(k2), s(k0), s(k1), mpnw)
call mpeq (s(k1), b, mpnw)
mpnw = nws
call mproun (b, mpnw)
if (mpidb .ge. 7) then
no = min (int (abs (b(1))), mpndb) + 2
write (mpldb, 2) (b(i), i = 1, no)
2 format ('MPINPC O'/(6f12.0))
endif
goto 220
210 continue
if (mpker(41) .ne. 0) then
write (mpldb, 3)
3 format ('*** MPINPC: Syntax error in literal string.')
mpier = 41
if (mpker(mpier) .eq. 2) call mpabrt
mpnw = nws
endif
220 return
end subroutine
subroutine mpout (iu, a, la, cs, mpnw)
! This routine writes the exponent plus LA mantissa digits of the MP number
! A to logical unit IU. CS is a scratch array of type CHARACTER*1. CS must
! be dimensioned at least LA + 25. The digits of A may span more than one
! line. A comma is placed at the end of the last line to denote the end of
! the MP number. Here is an example of the output:
! 10 ^ -4 x 3.14159265358979323846264338327950288419716939937510,
integer i, iu, l, la, ll, mpnw, nws
character*1 cs(la+25)
real a(mpnw+2)
if (mpier .ne. 0) return
nws = mpnw
ll = la / log10 (mpbdx) + 2.d0
mpnw = min (mpnw, ll)
call mpoutc (a, cs, l, mpnw)
mpnw = nws
l = min (l, la + 20) + 1
cs(l) = ','
write (iu, '(78A1)') (cs(i), i = 1, l)
return
end subroutine
subroutine mpoutc (a, b, n, mpnw)
! Converts the MP number A into character form in the CHARACTER*1 array B.
! N (an output parameter) is the length of the output. In other words, B is
! contained in B(1), ..., B(N). The format is analogous to the Fortran
! exponential format (E format), except that the exponent is placed first.
! Debug output starts with MPIDB = 7.
! Max CHARACTER*1 space for B: 7.225 * MPNW + 30 cells.
! This routine is called by MPOUT, but it may be directly called by the user
! if desired for custom output. Example:
! CHARACTER*1 CX(800)
! CALL MPOUTC (A, CX, ND)
! WRITE (1, '(20A1/(72A1))') (CX(I), I = 1, ND)
integer i, ia, ix, j, k0, k1, l, mpnw, na, nl, n5, nws, n, no, nx
character*1 b(n)
character*16 ca
real*8 aa, al2, con, t1
parameter (al2 = 0.301029995663981195d0, con = 0.8304820235d0)
real a(mpnw+2), f(8), s(2*mpnw+10)
real*8 an
! character*16 mpdigout
if (mpier .ne. 0) then
if (mpier .eq. 99) call mpabrt
b(1) = ' '
n = 0
return
endif
if (mpidb .ge. 7) then
no = min (int (abs (a(1))), mpndb) + 2
write (mpldb, 1) (a(i), i = 1, no)
1 format ('MPOUTC I'/(6f12.0))
endif
ia = sign (1., a(1))
na = min (int (abs (a(1))), mpnw)
n5 = mpnw + 5
k0 = 1
k1 = k0 + n5
nws = mpnw
mpnw = mpnw + 1
f(1) = 1.
f(2) = 0.
f(3) = 10.
! Determine exact power of ten for exponent.
if (na .ne. 0) then
aa = a(3)
if (na .ge. 2) aa = aa + mprdx * a(4)
if (na .ge. 3) aa = aa + mprx2 * a(5)
if (na .ge. 4) aa = aa + mprdx * mprx2 * a(6)
t1 = al2 * mpnbt * a(2) + log10 (aa)
if (t1 .ge. 0.d0) then
nx = t1
else
nx = t1 - 1.d0
endif
call mpnpwr (f, nx, s(k0), mpnw)
call mpdiv (a, s(k0), s(k1), mpnw)
! If we didn't quite get it exactly right, multiply or divide by 10 to fix.
100 continue
if (s(k1+1) .lt. 0.) then
nx = nx - 1
call mpmuld (s(k1), 10.d0, 0, s(k0), mpnw)
call mpeq (s(k0), s(k1), mpnw)
goto 100
elseif (s(k1+2) .ge. 10.) then
nx = nx + 1
call mpdivd (s(k1), 10.d0, 0, s(k0), mpnw)
call mpeq (s(k0), s(k1), mpnw)
goto 100
endif
s(k1) = abs (s(k1))
else
nx = 0
endif
! Place exponent first instead of at the very end as in Fortran.
b(1) = '1'
b(2) = '0'
b(3) = ' '
b(4) = '^'
ca = mpdigout (dble (nx), 10)
do i = 1, 10
b(i+4) = ca(i:i)
enddo
b(15) = ' '
b(16) = 'x'
b(17) = ' '
! Insert sign and first digit.
if (ia .eq. -1) then
b(18) = '-'
else
b(18) = ' '
endif
if (na .ne. 0) then
an = s(k1+2)
else
an = 0
endif
ca = mpdigout (an, 1)
b(19) = ca(1:1)
b(20) = '.'
ix = 20
if (na .eq. 0) goto 190
f(3) = an
call mpsub (s(k1), f, s(k0), mpnw)
if (s(k0) .eq. 0) goto 190
call mpmuld (s(k0), 1.d6, 0, s(k1), mpnw)
nl = max (mpnw * log10 (mpbdx) / 6.d0 - 1.d0, 1.d0)
nl = min (nl, mpoud/6 + 1)
! Insert the digits of the remaining words.
do j = 1, nl
if (s(k1+1) .eq. 0.) then
an = s(k1+2)
f(1) = 1.
f(3) = an
else
f(1) = 0.
an = 0.
endif
ca = mpdigout (an, 6)
do i = 1, 6
if (ca(i:i) == ' ') ca(i:i) = '0'
b(i+ix) = ca(i:i)
enddo
ix = ix + 6
call mpsub (s(k1), f, s(k0), mpnw)
call mpmuld (s(k0), 1.d6, 0, s(k1), mpnw)
if (s(k1) .eq. 0.) goto 140
enddo
! Check if trailing zeroes should be trimmed.
j = nl + 1
140 l = ix
if (b(l) .eq. '0' .and. b(l-1) .eq. '0' .or. (j .gt. nl .and. b(l-2) .eq. '0' &
.and. b(l-3) .eq. '0')) then
b(l) = ' '
b(l-1) = ' '
do i = l - 2, 21, -1
if (b(i) .ne. '0') then
ix = i
goto 190
endif
b(i) = ' '
enddo
ix = 20
! Check if trailing nines should be rounded up.
elseif (j .gt. nl .and. b(l-2) .eq. '9' .and. b(l-3) .eq. '9') then
b(l) = ' '
b(l-1) = ' '
do i = l - 2, 21, -1
if (b(i) .ne. '9') goto 180
b(i) = ' '
enddo
! We have rounded away all digits to the right of the decimal point, and the
! digit to the left of the digit is a 9. Set the digit to 1 and increase
! the exponent by one.
ix = 20
if (b(19) .eq. '9') then
b(19) = '1'
ca = mpdigout (dble (nx+1), 10)
do i = 1, 10
b(i+4) = ca(i:i)
enddo
else
ca = b(19)
an = mpdigin (ca, 1)
ca = mpdigout (an + 1.d0, 1)
b(19) = ca(1:1)
endif
goto 190
180 continue
ca = b(i)
an = mpdigin (ca, 1)
ca = mpdigout (an + 1.d0, 1)
b(i) = ca(1:1)
ix = i
endif
190 continue
n = min (ix, mpoud + 20)
mpnw = nws
if (mpidb .ge. 7) then
no = min (n, 6 * mpndb + 20)
write (mpldb, 2) (b(i), i = 1, no)
2 format ('MPOUTC O'/(78a1))
endif
return
end subroutine
recursive subroutine mpoutcx (a, b, n, mpnw)
! Converts the MP number A into character form in the CHARACTER*1 array B.
! N (an output parameter) is the length of the output. In other words, B is
! contained in B(1), ..., B(N). The format is analogous to the Fortran
! exponential format (E format), except that the exponent is placed first.
! Before calling MPOUTX, the arrays UU1 and UU2 must be initialized by
! calling MPINIX. For modest levels of precision, use MPOUTC. Debug output
! starts with MPIDB = 7.
! Max CHARACTER*1 space for B: 7.225 * MPNW + 30 cells.
implicit none
integer i, ia, ie, ie1, ie2, i1, i2, k0, k1, k2, k3, k4, m1, m2, &
mpnw, mpnws, n, na, nb1, nb2, ncr, no, n4
double precision al2, t1, t2, t3
character*1 b(*), b1(8*mpnw+30), b2(8*mpnw+30)
character*10 dig
character*16 c1, c2
parameter (al2 = 0.301029995663981195d0, dig = '0123456789')
real a(mpnw+2), s(5*mpnw+20)
if (mpier .ne. 0) then
if (mpier .eq. 99) call mpabrt
b(1) = ' '
n = 0
return
endif
if (mpidb .ge. 7) then
no = min (int (abs (a(1))), mpndb) + 2
write (mpldb, 1) (a(i), i = 1, no)
1 format ('MPOUTCX I'/(6f12.0))
endif
ia = sign (1., a(1))
na = min (int (abs (a(1))), mpnw)
n4 = mpnw + 4
k0 = 1
k1 = k0 + n4
k2 = k1 + n4
k3 = k2 + n4
k4 = k3 + n4
ncr = 2 ** mpmcr
! Check if actual precision level of argument is too low to justify the
! advanced routine.
if (na .le. ncr) then
call mpoutc (a, b, n, mpnw)
goto 110
endif
! Normalize input to an integer by multiplying by a suitable power of 10.
t1 = a(3) + mprdx * a(4) + mprx2 * a(5)
t2 = log10 (t1)
m1 = max (al2 * mpnbt * (abs (a(1)) - a(2)) - t2, 0.d0)
call mpdmc (10.d0, 0, s(k0))
call mpnpwx (s(k0), m1, s(k2), mpnw)
call mpmulx (a, s(k2), s(k1), mpnw)
s(k1) = abs (s(k1))
! Split large integer into two approximately equal decimal sections.
call mpmdc (s(k1), t1, i1)
call dpdec (t1, i1, t2, i2)
m2 = i2 / 2
call mpnpwx (s(k0), m2, s(k3), mpnw)
call mpdivx (s(k1), s(k3), s(k0), mpnw)
call mpinfr (s(k0), s(k2), s(k4), mpnw)
call mpmulx (s(k2), s(k3), s(k0), mpnw)
call mpsub (s(k1), s(k0), s(k3), mpnw)
! Recursively convert each section.
mpnws = mpnw
mpnw = s(k2) + 1
call mpoutcx (s(k2), b1, nb1, mpnw)
mpnw = s(k3) + 1
call mpoutcx (s(k3), b2, nb2, mpnw)
mpnw = mpnws
! Obtain decimal exponents from each section.
c1 = ' '
c2 = ' '
do i = 1, 10
c1(i:i) = b1(i+4)
c2(i:i) = b2(i+4)
enddo
read (c1, '(I10)') ie1
read (c2, '(I10)') ie2
! Set exponent of result.
ie = ie1 + m2 - m1
write (c1, '(I14)') ie
do i = 1, 4
b(i) = b1(i)
enddo
do i = 5, 14
b(i) = c1(i:i)
enddo
! Copy mantissa of first section.
do i = 15, nb1
b(i) = b1(i)
enddo
i1 = ie1 + m2 - ie2 + 19
! If first section is too long, then round trailing digits (probably 9s).
if (nb1 .gt. i1) then
i2 = index (dig, b(i1+1)) - 1
if (i2 .ge. 5) then
do i = i1, 21, -1
if (b(i) .ne. '9') goto 100
b(i) = '0'
enddo
write (mpldb, 2)
2 format ('*** MPOUTCX: Exceptional case -- contact DHB.')
stop
100 i2 = index (dig, b(i)) - 1
write (c1, '(I1)') i2 + 1
b(i) = c1(1:1)
endif
elseif (nb1 .lt. i1) then
! If first section is too short, then insert zeroes in gap.