File: maths.adb

package info (click to toggle)
spark 2012.0.deb-9
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 29,260 kB
  • ctags: 3,098
  • sloc: ada: 186,243; cpp: 13,497; makefile: 685; yacc: 440; lex: 176; ansic: 119; sh: 16
file content (1894 lines) | stat: -rw-r--r-- 67,641 bytes parent folder | download | duplicates (2)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
-------------------------------------------------------------------------------
-- (C) Altran Praxis Limited
-------------------------------------------------------------------------------
--
-- The SPARK toolset is free software; you can redistribute it and/or modify it
-- under terms of the GNU General Public License as published by the Free
-- Software Foundation; either version 3, or (at your option) any later
-- version. The SPARK toolset is distributed in the hope that it will be
-- useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
-- Public License for more details. You should have received a copy of the GNU
-- General Public License distributed with the SPARK toolset; see file
-- COPYING3. If not, go to http://www.gnu.org/licenses for a complete copy of
-- the license.
--
--=============================================================================

package body Maths is

   type Division_Result_Type is record
      Quotient, Remnant : Part;
   end record;

   --------------------------------------------------------------------------
   --                        Local Procedures
   --------------------------------------------------------------------------

   --------------------------------------------------------------------------
   -- Low-level conversions and utilities
   --------------------------------------------------------------------------

   --------------------------------------------------------------------------
   -- Converts extended Character to digit
   -- only works for '0'..'9' and 'A'..'F'.  ParseString will ensure that
   -- no other values will be passed to it.
   function CharToDigit (Ch : Character) return Digit
   -- # pre ((Ch >= '0') and (Ch <= '9')) or ((Ch >= 'A') and (Ch <= 'F'));
   is
      Valu : Digit;
   begin
      if Ch in '0' .. '9' then
         Valu := (Character'Pos (Ch) - Character'Pos ('0'));
      else
         Valu := ((Character'Pos (Ch) - Character'Pos ('A')) + 10);
      end if;
      return Valu;
   end CharToDigit;

   --------------------------------------------------------------------------
   -- Converts digit (Integer subtype 0..15) to extended Character
   function DigitToChar (Dig : Digit) return Character is
      Ch : Character;
   begin
      if Dig <= 9 then
         Ch := Character'Val (Dig + Character'Pos ('0'));
      else
         Ch := Character'Val ((Dig + Character'Pos ('A')) - 10);
      end if;
      return Ch;
   end DigitToChar;

   --------------------------------------------------------------------------
   -- Returns Larger of 2 Naturals
   function Max (X, Y : Natural) return Natural is
      Larger : Natural;
   begin
      if X > Y then
         Larger := X;
      else
         Larger := Y;
      end if;
      return Larger;
   end Max;

   --------------------------------------------------------------------------
   -- final check of value overflow or underflow before returning Value
   -- to caller from one of the exported procedures below.  Any existing
   -- error is preserved ie. overflow will only be reported if an earlier
   -- error is not detected.
   function OverflowCheck (ExistingError : ErrorCode;
                           Num           : Value) return ErrorCode is
      Result : ErrorCode;
   begin
      if ExistingError = NoError and then (Num.Numerator.Overflowed or Num.Denominator.Overflowed) then
         Result := OverFlow;
      else
         Result := ExistingError;
      end if;
      return Result;
   end OverflowCheck;

   --------------------------------------------------------------------------
   -- Low-level manipulation of part-Value arrays
   function StripLeadingZeros (P : Part) return Part is
      PLocal : Part;
   begin
      PLocal := P;
      while (PLocal.Length > 1) and then (PLocal.Numerals (PLocal.Length) = 0) loop
         PLocal.Length := PLocal.Length - 1;
      end loop;
      return PLocal;
   end StripLeadingZeros;

   --------------------------------------------------------------------------
   -- Multiplies Value part by 10
   function ShiftUpPart (P : Part) return Part is
      PLocal : Part;
   begin
      PLocal := P;
      if PLocal.Length = MaxLength then ----------can't shift it without overflow
         PLocal.Overflowed := True;
      else                              ----------room to shift
         for i in reverse PosRange range 1 .. PLocal.Length loop
            PLocal.Numerals (i + 1) := PLocal.Numerals (i);
         end loop;

         PLocal.Numerals (1) := 0;
         PLocal.Length       := P.Length + 1;
         PLocal              := StripLeadingZeros (PLocal); --in case we have just turned 0 into 00
      end if;
      return PLocal;
   end ShiftUpPart;

   --------------------------------------------------------------------------
   -- Divides Value Part by 10
   function ShiftDownPart (P : Part) return Part is
      PLocal : Part;
   begin
      PLocal := P;
      if PLocal.Length = 1 then
         PLocal.Numerals (1) := 0;
      else
         for i in PosRange range 2 .. PLocal.Length loop
            PLocal.Numerals (i - 1) := PLocal.Numerals (i);
         end loop;

         PLocal.Numerals (PLocal.Length) := 0;
         PLocal.Length                   := PLocal.Length - 1;
      end if;
      return PLocal;
   end ShiftDownPart;

   --------------------------------------------------------------------------
   -- Basic arithmetic on Part Values
   --------------------------------------------------------------------------
   -- Symbolically adds two Part Values together
   function AddPart (FirstPart, SecondPart : Part) return Part is
      Length                   : PosRange;
      Carry, IntermediateValue : Natural;
      Result                   : Part;
   begin
      Carry := 0;

      Result            := ZeroPart;
      Result.Overflowed := FirstPart.Overflowed or SecondPart.Overflowed;        -- propagate error

      Length := Max (FirstPart.Length, SecondPart.Length);
      for i in PosRange range 1 .. Length loop
         IntermediateValue := (Natural (FirstPart.Numerals (i)) + Natural (SecondPart.Numerals (i))) + Carry;

         if IntermediateValue >= 10 then
            IntermediateValue := IntermediateValue - 10;
            Carry             := 1;
         else
            Carry := 0;
         end if;
         Result.Numerals (i) := Digit (IntermediateValue);
      end loop;

      if Carry /= 0 then
         if Length = MaxLength then              -- can't carry without overflow
            Result.Overflowed := True;
         else                                    -- ok to extend Value
            Length                   := Length + 1;
            Result.Numerals (Length) := 1;
         end if;
      end if;
      Result.Length := Length;
      return Result;
   end AddPart;

   --------------------------------------------------------------------------
   -- Symbolically subtracts Part Values
   -- WARNING second parameter must be <= first before the call
   function SubtractPart (Larger, Smaller : Part) return Part is
      Length            : PosRange;
      Borrow            : Natural;
      IntermediateValue : Integer;
      Result            : Part;
   begin
      Borrow            := 0;
      Result            := ZeroPart;
      Result.Overflowed := Larger.Overflowed or Smaller.Overflowed;           -- propagate error

      Length        := Max (Larger.Length, Smaller.Length);
      Result.Length := Length;

      for i in PosRange range 1 .. Length loop
         IntermediateValue := (Natural (Larger.Numerals (i)) - Natural (Smaller.Numerals (i))) - Borrow;

         if IntermediateValue < 0 then
            IntermediateValue := IntermediateValue + 10;
            Borrow            := 1;
         else
            Borrow := 0;
         end if;
         Result.Numerals (i) := Digit (IntermediateValue);
      end loop;

      return StripLeadingZeros (Result);
   end SubtractPart;

   --------------------------------------------------------------------------
   -- NB.  These Parts are considered unsigned
   function GreaterPart (FirstPart, SecondPart : Part) return Boolean is
      IsGreater : Boolean;
      i         : LengthRange;
   begin
      if FirstPart.Length = SecondPart.Length then
         IsGreater := False;
         i         := FirstPart.Length;
         loop
            if FirstPart.Numerals (i) /= SecondPart.Numerals (i) then
               IsGreater := FirstPart.Numerals (i) > SecondPart.Numerals (i);
               exit;
            end if;

            exit when i = 1;
            i := i - 1;
         end loop;

      else
         IsGreater := FirstPart.Length > SecondPart.Length;
      end if;
      return IsGreater;
   end GreaterPart;

   --------------------------------------------------------------------------
   -- NB.  These Parts are considered unsigned
   function LesserPart (FirstPart, SecondPart : Part) return Boolean is
      IsLesser : Boolean;
      i        : LengthRange;
   begin
      if FirstPart.Length = SecondPart.Length then
         IsLesser := False;
         i        := FirstPart.Length;

         loop
            if FirstPart.Numerals (i) /= SecondPart.Numerals (i) then
               IsLesser := FirstPart.Numerals (i) < SecondPart.Numerals (i);
               exit;
            end if;

            exit when i = 1;
            i := i - 1;
         end loop;

      else
         IsLesser := FirstPart.Length < SecondPart.Length;
      end if;
      return IsLesser;
   end LesserPart;

   --------------------------------------------------------------------------
   -- Multiplies a Part Value by a single digit (range 0..15).
   -- Used in conversion of based literal to a Value and as Part of the
   -- Value multiply routines
   function SingleDigitMult (P : Part;
                             D : Digit) return Part is
      Carry, IntermediateValue : Natural;
      Result                   : Part;
   begin
      Carry             := 0;
      Result            := ZeroPart;
      Result.Overflowed := P.Overflowed;           --propagate error
      Result.Length     := P.Length;

      for i in PosRange range 1 .. P.Length loop
         IntermediateValue := Natural (P.Numerals (i)) * Natural (D) + Carry;

         Result.Numerals (i) := Digit (IntermediateValue mod 10);
         Carry               := IntermediateValue / 10;
      end loop;

      while Carry /= 0 loop
         if Result.Length = MaxLength then       -- can't carry without overflow
            Result.Overflowed := True;
            exit;
         end if;

         Result.Length                   := Result.Length + 1;
         Result.Numerals (Result.Length) := Digit (Carry mod 10);

         Carry := Carry / 10;
      end loop;

      return Result;
   end SingleDigitMult;

   --------------------------------------------------------------------------
   -- Symbolically multiples 2 Part Values together
   function MultPart (FirstPart, SecondPart : Part) return Part is
      FirstPartLocal, Result : Part;
   begin
      Result            := ZeroPart;
      Result.Overflowed := FirstPart.Overflowed or SecondPart.Overflowed;      -- propagate error
      FirstPartLocal    := FirstPart;

      for i in PosRange range 1 .. SecondPart.Length loop
         Result         := AddPart (Result, SingleDigitMult (FirstPartLocal, SecondPart.Numerals (i)));
         FirstPartLocal := ShiftUpPart (FirstPartLocal);
      end loop;
      return Result;
   end MultPart;

   --------------------------------------------------------------------------
   --Digit by digit long div of one Value Part by another
   --Do not call with bot=0

   function DivPart (Top, Bot : Part) return Division_Result_Type is
      subtype GoesIndexRange is Integer range 0 .. 9;
      type GoesArray is array (GoesIndexRange) of Part;
      GoesDigit               : GoesIndexRange;
      Goes                    : GoesArray;
      ResultLocal, CurrentTry : Part;
      Column                  : Natural;

      ---------------------------

      -- builds array of 1*divisor..9*divisor
      procedure BuildGoesTable
      --# global in     Bot;
      --#           out Goes;
      --# derives Goes from Bot;
      is
      begin
         Goes     := GoesArray'(GoesIndexRange => ZeroPart);
         Goes (1) := Bot;

         for i in GoesIndexRange range 2 .. 9 loop
            Goes (i) := AddPart (Goes (i - 1), Bot);
         end loop;
      end BuildGoesTable;

      ---------------------------

      function FindGoes (Into : Part) return GoesIndexRange
      --# global in Goes;
      is
         Result : GoesIndexRange;
      begin
         Result := 0;
         for i in reverse Integer range 1 .. 9 loop
            if (LesserPart (Goes (i), Into)) or (Goes (i) = Into) then
               Result := i;
               exit;
            end if;
         end loop;
         return Result;
      end FindGoes;

      ---------------------------

      procedure StoreDigit (Dig  : in     GoesIndexRange;
                            Dest : in out Part)
      --# derives Dest from *,
      --#                   Dig;
      --NB. This method of stroring digit automatically excludes leading zeros
      --    since Length only increases once a non-zero is present.
      is
      begin
         Dest              := ShiftUpPart (Dest);
         Dest.Numerals (1) := Digit (Dig);
      end StoreDigit;

      ---------------------------

   begin -- DivPart
      BuildGoesTable;

      ResultLocal := ZeroPart;
      CurrentTry  := ZeroPart;
      Column      := Top.Length;           -- start at MSD
      loop
         exit when Column = 0;

         StoreDigit (Integer (Top.Numerals (Column)), CurrentTry);
         Column    := Column - 1;
         GoesDigit := FindGoes (CurrentTry);
         StoreDigit (GoesDigit, ResultLocal);
         if GoesDigit /= 0 then
            CurrentTry := SubtractPart (CurrentTry, Goes (GoesDigit));
         end if;
      end loop;

      return Division_Result_Type'(Quotient => ResultLocal,
                                   Remnant  => CurrentTry);
   end DivPart;

   --------------------------------------------------------------------------
   -- Conversions too and from Part Values
   --------------------------------------------------------------------------

   --Converts an Ada Natural type to a Part Value;
   function NaturalToPart (Int : Natural) return Part is
      IntLocal : Natural;
      Result   : Part;
   begin
      Result        := ZeroPart;
      IntLocal      := Int;
      Result.Length := 0;
      while IntLocal > 0 loop
         Result.Length                   := Result.Length + 1;
         Result.Numerals (Result.Length) := Digit (IntLocal mod 10);
         IntLocal                        := IntLocal / 10;
      end loop;
      return Result;
   end NaturalToPart;

   --------------------------------------------------------------------------

   -- Convert String to Natural - NB. not done symbolically
   -- Assumes decimal
   function String_To_Natural (Str : E_Strings.T) return Natural is
      Position_Multiplier, Total : Natural;
   begin
      Position_Multiplier := 1;
      Total               := 0;

      for I in reverse E_Strings.Positions range 1 .. E_Strings.Get_Length (E_Str => Str) loop
         Total := Total + Position_Multiplier * Natural (CharToDigit (E_Strings.Get_Element (E_Str => Str,
                                                                                             Pos   => I)));

         Position_Multiplier := Position_Multiplier * 10;
      end loop;
      return Total;
   end String_To_Natural;

   --------------------------------------------------------------------------
   -- Produces Value Part from string interpreting it as being to base
   function String_To_Part (Base : Natural;
                            Str  : E_Strings.T) return Part is
      Position_Multiplier, Base_Part, Result : Part;
   begin
      Result              := ZeroPart;
      Position_Multiplier := OnePart;
      Base_Part           := NaturalToPart (Base);

      for I in reverse E_Strings.Positions range 1 .. E_Strings.Get_Length (E_Str => Str) loop
         Result              :=
           AddPart
           (Result,
            SingleDigitMult (Position_Multiplier, CharToDigit (E_Strings.Get_Element (E_Str => Str,
                                                                                      Pos   => I))));
         Position_Multiplier := MultPart (Position_Multiplier, Base_Part);
      end loop;
      return Result;
   end String_To_Part;

   --------------------------------------------------------------------------
   -- Produces Value Part from decimal string more quickly than String_To_Part
   function Dec_String_To_Part (Str : E_Strings.T) return Part is
      Result : Part;
      Hi     : Natural;
   begin
      Result := ZeroPart;
      Hi     := E_Strings.Get_Length (E_Str => Str);
      if Hi <= MaxLength then
         for I in Natural range 1 .. Hi loop
            Result.Numerals (I) := CharToDigit (E_Strings.Get_Element (E_Str => Str,
                                                                       Pos   => (Hi - I) + 1));
         end loop;
         Result.Length := E_Strings.Get_Length (E_Str => Str);
      else
         Result.Overflowed := True;
      end if;
      return Result;
   end Dec_String_To_Part;

   --------------------------------------------------------------------------
   -- Normalization routines for rational pairs
   --------------------------------------------------------------------------

   -- removes zeros symmetrically from Numerator and Denominator
   procedure NormalizeByTen (Num : in out Value)
   --# derives Num from *;
   is
   begin
      -- strip mutual trailing zeros
      while (Num.Numerator.Numerals (1) = 0) and (Num.Denominator.Numerals (1) = 0) loop
         Num.Numerator   := ShiftDownPart (Num.Numerator);
         Num.Denominator := ShiftDownPart (Num.Denominator);
      end loop;
   end NormalizeByTen;

   --------------------------------------------------------------------------
   -- Routine to find GCD of 2 Parts
   function GCD (FirstPart, SecondPart : Part) return Part is
      FirstPartLocal, SecondPartLocal : Part;
      DivisionResult                  : Division_Result_Type;
   begin
      FirstPartLocal  := FirstPart;
      SecondPartLocal := SecondPart;
      loop
         exit when SecondPartLocal = ZeroPart;

         DivisionResult  := DivPart (FirstPartLocal, SecondPartLocal);
         FirstPartLocal  := SecondPartLocal;
         SecondPartLocal := DivisionResult.Remnant;
      end loop;
      return FirstPartLocal;
   end GCD;

   --------------------------------------------------------------------------

   procedure Normalize (Num : in out Value)
   --# derives Num from *;
   is
      Divisor : Part;
   begin
      NormalizeByTen (Num); --does not reduce no of GCD iterations but shortens each one
      if not (Num.Numerator.Overflowed or Num.Denominator.Overflowed) then
         Divisor         := GCD (Num.Numerator, Num.Denominator);
         Num.Numerator   := DivPart (Num.Numerator, Divisor).Quotient;
         Num.Denominator := DivPart (Num.Denominator, Divisor).Quotient;
      end if;
   end Normalize;

   --------------------------------------------------------------------------
   -- Higher level arithmetic routines
   --------------------------------------------------------------------------

   -- Correctly handles ordering and sign of subtraction of +ve Numerator Parts
   -- of Value but leaves Denominator and Value types alone so can be
   -- use by both Integer and Real routines.  Callers should ensure that field
   -- Sort of Result is set correctly.
   procedure NumeratorSubtract (FirstNum, SecondNum : in     Value;
                                Result              : in out Value)
   --# derives Result from *,
   --#                     FirstNum,
   --#                     SecondNum;
   is
   begin
      if GreaterPart (FirstNum.Numerator, SecondNum.Numerator) then
         Result.Numerator := SubtractPart (FirstNum.Numerator, SecondNum.Numerator);
      elsif GreaterPart (SecondNum.Numerator, FirstNum.Numerator) then
         Result.Numerator  := SubtractPart (SecondNum.Numerator, FirstNum.Numerator);
         Result.IsPositive := False;
      else
         Result.Numerator := ZeroPart;
      end if;
   end NumeratorSubtract;

   --------------------------------------------------------------------------
   -- Correctly handles adding (including ordering and signs) of numerator Parts
   -- of Values but leaves Denominator and Value types alone so can be
   -- use by both Integer and Real routines.  Callers should ensure that field
   -- Sort of Result is set correctly.
   procedure NumeratorAdd (FirstNum, SecondNum : in     Value;
                           Result              : in out Value)
   --# derives Result from *,
   --#                     FirstNum,
   --#                     SecondNum;
   is
   begin
      if FirstNum.IsPositive and SecondNum.IsPositive then
         Result.Numerator := AddPart (FirstNum.Numerator, SecondNum.Numerator);

      elsif not (FirstNum.IsPositive or SecondNum.IsPositive) then
         Result.Numerator  := AddPart (FirstNum.Numerator, SecondNum.Numerator);
         Result.IsPositive := False;

      elsif FirstNum.IsPositive and not SecondNum.IsPositive then
         NumeratorSubtract (FirstNum  => FirstNum,
                            SecondNum => SecondNum,
                            Result    => Result);

      elsif not FirstNum.IsPositive and SecondNum.IsPositive then
         NumeratorSubtract (FirstNum  => SecondNum,
                            SecondNum => FirstNum,
                            Result    => Result);
      end if;
   end NumeratorAdd;

   --------------------------------------------------------------------------
   -- Modifies 2 Values such that their Denominators are the same
   procedure CommonDenominator (FirstNum, SecondNum           : in     Value;
                                ModifiedFirst, ModifiedSecond :    out Value)
   --# derives ModifiedFirst,
   --#         ModifiedSecond from FirstNum,
   --#                             SecondNum;
   is
      CommonDenom : Part;
   begin
      ModifiedFirst  := FirstNum;
      ModifiedSecond := SecondNum;
      if not (FirstNum.Denominator = SecondNum.Denominator) then
         CommonDenom                := MultPart (FirstNum.Denominator, SecondNum.Denominator);
         ModifiedFirst.Numerator    := MultPart (FirstNum.Numerator, SecondNum.Denominator);
         ModifiedSecond.Numerator   := MultPart (SecondNum.Numerator, FirstNum.Denominator);
         ModifiedFirst.Denominator  := CommonDenom;
         ModifiedSecond.Denominator := CommonDenom;
      end if;
   end CommonDenominator;

   --------------------------------------------------------------------------

   procedure RealAdd (FirstNum, SecondNum : in     Value;
                      Result              :    out Value;
                      Ok                  :    out ErrorCode)
   --# derives Ok,
   --#         Result from FirstNum,
   --#                     SecondNum;
   is
      FirstNumLocal, SecondNumLocal, Res : Value;
   begin
      Res := ZeroReal;
      CommonDenominator (FirstNum, SecondNum, FirstNumLocal, SecondNumLocal);
      NumeratorAdd (FirstNumLocal, SecondNumLocal, Res);
      Res.Denominator := FirstNumLocal.Denominator;
      Normalize (Res);
      Result := Res;
      Ok     := OverflowCheck (NoError, Res);
   end RealAdd;

   --------------------------------------------------------------------------

   procedure IntegerMultiply (FirstNum, SecondNum : in     Value;
                              Result              :    out Value;
                              Ok                  :    out ErrorCode)
   --# derives Ok,
   --#         Result from FirstNum,
   --#                     SecondNum;
   is
      Res : Value;
   begin
      Res            := ZeroInteger;
      Res.Numerator  := StripLeadingZeros (MultPart (FirstNum.Numerator, SecondNum.Numerator));
      Res.IsPositive := (FirstNum.IsPositive = SecondNum.IsPositive);
      Ok             := OverflowCheck (NoError, Res);
      Result         := Res;
   end IntegerMultiply;

   --------------------------------------------------------------------------

   procedure RealMultiply (FirstNum, SecondNum : in     Value;
                           Result              :    out Value;
                           Ok                  :    out ErrorCode)
   --# derives Ok,
   --#         Result from FirstNum,
   --#                     SecondNum;
   is
      Res : Value;
   begin
      Res             := ZeroReal;
      Res.Numerator   := MultPart (FirstNum.Numerator, SecondNum.Numerator);
      Res.Denominator := MultPart (FirstNum.Denominator, SecondNum.Denominator);
      Res.IsPositive  := (FirstNum.IsPositive = SecondNum.IsPositive);
      Normalize (Res);
      Result := Res;
      Ok     := OverflowCheck (NoError, Res);
   end RealMultiply;

   --------------------------------------------------------------------------

   procedure RealDivide (FirstNum, SecondNum : in     Value;
                         Result              :    out Value;
                         Ok                  :    out ErrorCode)
   --# derives Ok,
   --#         Result from FirstNum,
   --#                     SecondNum;
   is

      ----------------------------------------

      function Invert (N : Value) return Value is
      begin
         return Value'(Numerator   => N.Denominator,
                       Denominator => N.Numerator,
                       IsPositive  => N.IsPositive,
                       Sort        => N.Sort);
      end Invert;

      ----------------------------------------

   begin -- RealDivide
      RealMultiply (FirstNum, Invert (SecondNum), Result, Ok);
   end RealDivide;

   --------------------------------------------------------------------------

   procedure GreaterLocal (FirstNum, SecondNum : in     Value;
                           Result              :    out Boolean;
                           Ok                  :    out ErrorCode)
   --# derives Ok,
   --#         Result from FirstNum,
   --#                     SecondNum;
   is
      FirstLocal, SecondLocal : Value;
      LocalOk                 : ErrorCode;
   begin
      LocalOk := NoError;
      if FirstNum.Sort /= SecondNum.Sort then
         Result  := False;
         LocalOk := TypeMismatch;
      else --legal types
         if FirstNum.Sort = RealValue then           --put on common Denominator
            CommonDenominator (FirstNum, SecondNum, FirstLocal, SecondLocal);
         else --this covers Integer, enumeration and Boolean
            FirstLocal  := FirstNum;
            SecondLocal := SecondNum;
         end if;
         if FirstLocal.IsPositive then
            if SecondLocal.IsPositive then                  -- both positive
               Result := GreaterPart (FirstLocal.Numerator, SecondLocal.Numerator);
            else  -- first positive and second Value is negative
               Result := True;
            end if;
         else  --first Value is negative
            if SecondLocal.IsPositive then     -- first negative, second positive
               Result := False;
            else  --both Values are negative
               Result := LesserPart (FirstLocal.Numerator, SecondLocal.Numerator);
            end if;
         end if;
         LocalOk := OverflowCheck (LocalOk, FirstLocal);
         LocalOk := OverflowCheck (LocalOk, SecondLocal);
      end if;
      Ok := LocalOk;
   end GreaterLocal;

   --------------------------------------------------------------------------
   -- Non-exported procedure used by Remainder and Modulus Only
   --------------------------------------------------------------------------

   procedure ModRemLegalityCheck (FirstNum, SecondNum : in     Value;
                                  Err                 :    out ErrorCode)
   --# derives Err from FirstNum,
   --#                  SecondNum;
   is
   begin
      if (FirstNum.Sort = IntegerValue) and (SecondNum.Sort = IntegerValue) then
         if SecondNum.Numerator = ZeroPart then
            Err := DivideByZero;
         else  --this is the legal case we can do something with
            Err := NoError;
         end if;
      elsif (FirstNum.Sort = RealValue) and (SecondNum.Sort = RealValue) then
         Err := IllegalOperation;
      else
         Err := TypeMismatch;
      end if;
   end ModRemLegalityCheck;

   -------------------------------------------------------------------------

   procedure ParseString
     (S                                               : in     E_Strings.T;
      Decimal_Point_Found, Exponent_Found, Base_Found :    out Boolean;
      Base                                            :    out Natural;
      Core_String, Exp_String                         :    out E_Strings.T;
      Exp_Sign                                        :    out Character;
      Places_After_Point                              :    out E_Strings.Lengths;
      Legal_Syntax                                    :    out Boolean)
   --# derives Base,
   --#         Base_Found,
   --#         Core_String,
   --#         Decimal_Point_Found,
   --#         Exponent_Found,
   --#         Exp_Sign,
   --#         Exp_String,
   --#         Legal_Syntax,
   --#         Places_After_Point  from S;

   -- NOTES
   --   BaseString will be set to "10" if Base_Found = False
   --   Exp_String is "0" if ExpFound = False
   --   Exp_Sign is plus if ExpFound = False
   --   PlacesAferPoint is 0 if Decimal_Point_Found = False
   --   Legal_Syntax only implies that String looks like an Ada literal
      is separate;

   -------------------------------------------------------------------------
   -- PartToBits - converts a Part into a Bits array, with LSB in element 0
   -- All insignificant MSBs are set to False
   -------------------------------------------------------------------------
   function PartToBits (A : in Part) return Bits is
      R     : Bits;
      Power : Part;
      Q     : Division_Result_Type;
   begin
      R     := ZeroBits;
      Power := OnePart;

      for J in BinaryLengthRange loop
         Q     := DivPart (DivPart (A, Power).Quotient, TwoPart);
         R (J) := Q.Remnant /= ZeroPart;
         Power := SingleDigitMult (Power, 2);
         exit when Power.Overflowed;
      end loop;
      return R;
   end PartToBits;

   -------------------------------------------------------------------------
   -- BitsToPart - converts a Bits array into a Part, assuming LSB in
   -- element 0.
   -------------------------------------------------------------------------
   function BitsToPart (B : in Bits) return Part is
      P     : Part;
      Power : Part;
   begin
      P     := ZeroPart;
      Power := OnePart;

      for J in BinaryLengthRange loop
         if B (J) then
            P := AddPart (P, Power);
         end if;
         Power := SingleDigitMult (Power, 2);
         exit when Power.Overflowed;
      end loop;

      return P;
   end BitsToPart;

   -------------------------------------------------------------------------
   --------------------------------------------------------------------------
   --                        Exported Procedures
   --------------------------------------------------------------------------
   --------------------------------------------------------------------------

   procedure LiteralToValue (Str : in     LexTokenManager.Lex_String;
                             Num :    out Value;
                             OK  :    out ErrorCode) is separate;

   ----------------------------------------------------------------------------

   function IntegerToValue (I : Integer) return Value is
      IsPositive : Boolean;
      Numerator  : Part;
      ValSoFar   : Integer;
      NextDigit  : Digit;
      Length     : LengthRange;
   begin --IntegerToValue
      Numerator  := ZeroPart;
      IsPositive := I >= 0;
      ValSoFar   := abs (I);

      if ValSoFar /= 0 then
         Length := 1;
         loop
            NextDigit                   := Digit (ValSoFar mod 10);
            ValSoFar                    := ValSoFar / 10;
            Numerator.Numerals (Length) := NextDigit;
            exit when ValSoFar = 0;

            Length := Length + 1;
         end loop;
         Numerator.Length := Length;
      end if;

      return Value'(Numerator   => Numerator,
                    Denominator => OnePart,
                    IsPositive  => IsPositive,
                    Sort        => IntegerValue);
   end IntegerToValue;

   ---------------------------------------------------------------------------

   procedure StorageRep (Num : in     Value;
                         Rep :    out LexTokenManager.Lex_String)
   --670
   is
      Str      : E_Strings.T := E_Strings.Empty_String;
      StoreRep : LexTokenManager.Lex_String;

      procedure BuildString (IsPos   : in Boolean;
                             PartVal : in Part)
      --# global in out Str;
      --# derives Str from *,
      --#                  IsPos,
      --#                  PartVal;
      is
      begin
         if not IsPos then
            E_Strings.Append_Char (E_Str => Str,
                                   Ch    => '-');
         end if;
         for I in LengthRange range 1 .. PartVal.Length loop
            E_Strings.Append_Char (E_Str => Str,
                                   Ch    => DigitToChar (PartVal.Numerals (I)));
         end loop;
      end BuildString;

      -----------------

      procedure AppendDenominator (PartVal : in Part)
      --# global in out Str;
      --# derives Str from *,
      --#                  PartVal;
      is
      begin
         E_Strings.Append_Char (E_Str => Str,
                                Ch    => '/');
         for I in LengthRange range 1 .. PartVal.Length loop
            E_Strings.Append_Char (E_Str => Str,
                                   Ch    => DigitToChar (PartVal.Numerals (I)));
         end loop;
      end AppendDenominator;

   begin
      case Num.Sort is
         when UnknownValue =>
            StoreRep := LexTokenManager.Null_String;
         when TruthValue =>
            if Num.IsPositive then
               StoreRep := LexTokenManager.True_Token;
            else
               StoreRep := LexTokenManager.False_Token;
            end if;
         when IntegerValue =>
            BuildString (Num.IsPositive, Num.Numerator);
            LexTokenManager.Insert_Examiner_String (Str     => Str,
                                                    Lex_Str => StoreRep);
         when RealValue =>
            BuildString (Num.IsPositive, Num.Numerator);
            AppendDenominator (Num.Denominator);
            LexTokenManager.Insert_Examiner_String (Str     => Str,
                                                    Lex_Str => StoreRep);
      end case;
      Rep := StoreRep;
   end StorageRep;

   ----------------------------------------------------------------------------

   function ValueRep (StoreRep : LexTokenManager.Lex_String) return Value is
      Str           : E_Strings.T;
      ValIsPositive : Boolean;
      PartVal       : Part;
      Ptr           : E_Strings.Positions;
      SlashFound    : Boolean;
      Val           : Value;

      ----------------

      procedure GetSign
      --# global in     Str;
      --#           out Ptr;
      --#           out ValIsPositive;
      --# derives Ptr,
      --#         ValIsPositive from Str;
      is
      begin
         if E_Strings.Get_Element (E_Str => Str,
                                   Pos   => 1) = '-' then
            ValIsPositive := False;
            Ptr           := 2;
         else
            ValIsPositive := True;
            Ptr           := 1;
         end if;
      end GetSign;

      ---------------

      procedure GetPart (PartVal : out Part)
      --# global in     Str;
      --#        in out Ptr;
      --#           out SlashFound;
      --# derives PartVal,
      --#         Ptr,
      --#         SlashFound from Ptr,
      --#                         Str;
      is
         Len : LengthRange;
      begin
         SlashFound := False;
         PartVal    := ZeroPart;
         Len        := 0;
         loop
            if E_Strings.Get_Element (E_Str => Str,
                                      Pos   => Ptr) = '/' then
               Ptr        := Ptr + 1; --skip over '/'
               SlashFound := True;
               exit;
            end if;

            --here we are neither at the end of the string nor have we reached a /
            Len                    := Len + 1;
            PartVal.Numerals (Len) := CharToDigit (E_Strings.Get_Element (E_Str => Str,
                                                                          Pos   => Ptr));

            exit when Ptr = E_Strings.Get_Length (E_Str => Str);

            Ptr := Ptr + 1;
         end loop;
         PartVal.Length := Len;
      end GetPart;

   begin -- ValueRep
      if LexTokenManager.Lex_String_Case_Insensitive_Compare (Lex_Str1 => StoreRep,
                                                              Lex_Str2 => LexTokenManager.Null_String) =
        LexTokenManager.Str_Eq then
         Val := NoValue;
      elsif LexTokenManager.Lex_String_Case_Insensitive_Compare (Lex_Str1 => StoreRep,
                                                                 Lex_Str2 => LexTokenManager.True_Token) =
        LexTokenManager.Str_Eq then
         Val := TrueValue;
      elsif LexTokenManager.Lex_String_Case_Insensitive_Compare (Lex_Str1 => StoreRep,
                                                                 Lex_Str2 => LexTokenManager.False_Token) =
        LexTokenManager.Str_Eq then
         Val := FalseValue;
      else --some Sort of Number
         Val := ZeroInteger;  --set up all fields with suitable default Values
         Str := LexTokenManager.Lex_String_To_String (Lex_Str => StoreRep);
         GetSign;
         Val.IsPositive := ValIsPositive;
         GetPart (PartVal);
         Val.Numerator := PartVal;
         if SlashFound then
            Val.Sort := RealValue;
            --# accept F, 10, SlashFound, "SlashFound not used here" &
            --#        F, 10, Ptr, "Ptr not used here";
            GetPart (PartVal);
            --# end accept;
            Val.Denominator := PartVal;
         end if;
      end if;
      return Val;
   end ValueRep;

   ----------------------------------------------------------------------------

   function HasNoValue (Num : Value) return Boolean is
   begin
      return Num.Sort = UnknownValue;
   end HasNoValue;

   ----------------------------------------------------------------------------

   function ValueToString (Num : Value) return E_Strings.T is separate;

   --------------------------------------------------------------------------

   procedure ValueToInteger (Num : in     Value;
                             Int :    out Integer;
                             Ok  :    out ErrorCode) is
      Column, IntLocal, PosMult : Natural;
   begin
      if Num.Sort = IntegerValue then
         if GreaterPart (Num.Numerator, NaturalToPart (Integer'Last)) then
            Int := 0;
            Ok  := OverFlow;
         else
            IntLocal := 0;
            PosMult  := 1;
            Column   := 1;
            loop
               IntLocal := IntLocal + PosMult * Natural (Num.Numerator.Numerals (Column));
               exit when Column = Num.Numerator.Length;

               Column  := Column + 1;
               PosMult := PosMult * 10;
            end loop;
            Ok := NoError;
            if Num.IsPositive then
               Int := IntLocal;
            else
               Int := -IntLocal;
            end if;
         end if;
      else ------------------------its not an Integer
         Int := 0;
         Ok  := TypeMismatch;
      end if;
   end ValueToInteger;

   --------------------------------------------------------------------------

   procedure Add (FirstNum, SecondNum : in     Value;
                  Result              :    out Value;
                  Ok                  :    out ErrorCode) is
      Res : Value;
   begin
      if HasNoValue (FirstNum) or else HasNoValue (SecondNum) then
         Result := NoValue;
         Ok     := NoError;

      elsif (FirstNum.Sort = IntegerValue) and then (SecondNum.Sort = IntegerValue) then
         Res := ZeroInteger;
         NumeratorAdd (FirstNum, SecondNum, Res);
         Result := Res;
         Ok     := OverflowCheck (NoError, Res);

      elsif (FirstNum.Sort = RealValue) and then (SecondNum.Sort = RealValue) then
         RealAdd (FirstNum, SecondNum, Result, Ok);

      else
         Result := NoValue;
         Ok     := TypeMismatch;
      end if;
   end Add;

   --------------------------------------------------------------------------

   procedure Negate (Num : in out Value) is
   begin
      if Num.Sort /= UnknownValue and then Num.Numerator /= ZeroPart then
         Num.IsPositive := not Num.IsPositive;
      end if;
   end Negate;

   --------------------------------------------------------------------------

   procedure Absolute (Num : in out Value) is
   begin
      Num.IsPositive := True;
   end Absolute;

   --------------------------------------------------------------------------

   procedure ConvertToInteger (Num : in out Value) is
   begin
      Num.Sort := IntegerValue;
   end ConvertToInteger;

   ----------------------------------------------------------------------------

   procedure ConvertToReal (Num : in out Value) is
   begin
      Num.Sort := RealValue;
   end ConvertToReal;

   ----------------------------------------------------------------------------

   procedure Subtract (FirstNum, SecondNum : in     Value;
                       Result              :    out Value;
                       Ok                  :    out ErrorCode) is
      deductor : Value;
   begin
      if HasNoValue (FirstNum) or else HasNoValue (SecondNum) then
         Result := NoValue;
         Ok     := NoError;
      else
         deductor := SecondNum;
         Negate (deductor);
         Add (FirstNum, deductor, Result, Ok);
      end if;
   end Subtract;

   --------------------------------------------------------------------------

   procedure Multiply (FirstNum, SecondNum : in     Value;
                       Result              :    out Value;
                       Ok                  :    out ErrorCode) is
   begin
      if HasNoValue (FirstNum) or else HasNoValue (SecondNum) then
         Result := NoValue;
         Ok     := NoError;

      elsif (FirstNum.Sort = IntegerValue) and then (SecondNum.Sort = IntegerValue) then
         IntegerMultiply (FirstNum, SecondNum, Result, Ok);

      else
         RealMultiply (FirstNum, SecondNum, Result, Ok);
      end if;
   end Multiply;

   --------------------------------------------------------------------------

   procedure Divide (FirstNum, SecondNum : in     Value;
                     Result              :    out Value;
                     Ok                  :    out ErrorCode) is
   begin
      if HasNoValue (FirstNum) or else HasNoValue (SecondNum) then
         Result := NoValue;
         Ok     := NoError;

      elsif (FirstNum.Sort = IntegerValue) and then (SecondNum.Sort = IntegerValue) then
         if SecondNum = ZeroInteger then
            Result := NoValue;
            Ok     := DivideByZero;
         else
            Result            := ZeroInteger;
            Result.Numerator  := DivPart (FirstNum.Numerator, SecondNum.Numerator).Quotient;
            Result.IsPositive := (FirstNum.IsPositive = SecondNum.IsPositive);
            Ok                := NoError;
         end if;

      elsif (FirstNum.Sort = RealValue) then
         if SecondNum = ZeroReal then
            Result := NoValue;
            Ok     := DivideByZero;
         else
            RealDivide (FirstNum, SecondNum, Result, Ok);
         end if;
      else
         Result := NoValue;
         Ok     := TypeMismatch;
      end if;
   end Divide;

   --------------------------------------------------------------------------

   procedure Remainder (FirstNum, SecondNum : in     Value;
                        Result              :    out Value;
                        Ok                  :    out ErrorCode) is
      OkLocal     : ErrorCode;
      ResultLocal : Value;
   begin
      if HasNoValue (FirstNum) or else HasNoValue (SecondNum) then
         Result := NoValue;
         Ok     := NoError;
      else
         ModRemLegalityCheck (FirstNum, SecondNum, OkLocal);
         if OkLocal = NoError then
            ResultLocal           := ZeroInteger;
            ResultLocal.Numerator := DivPart (FirstNum.Numerator, SecondNum.Numerator).Remnant;
            if not (ResultLocal = ZeroInteger) then
               ResultLocal.IsPositive := FirstNum.IsPositive;
            end if;
            Result := ResultLocal;

         else --some Sort of error
            Result := NoValue;
         end if;
         Ok := OkLocal;
      end if;
   end Remainder;

   --------------------------------------------------------------------------

   procedure Modulus (FirstNum, SecondNum : in     Value;
                      Result              :    out Value;
                      Ok                  :    out ErrorCode) is
      OkLocal        : ErrorCode;
      ResultLocal    : Value;
      DivisionResult : Division_Result_Type;
   begin
      if HasNoValue (FirstNum) or else HasNoValue (SecondNum) then
         Result := NoValue;
         Ok     := NoError;

      elsif FirstNum.IsPositive = SecondNum.IsPositive then
         Remainder (FirstNum, SecondNum, Result, Ok);

      else       -- special handling for MOD with mixed signs
         ModRemLegalityCheck (FirstNum, SecondNum, OkLocal);

         if OkLocal = NoError then
            ResultLocal    := ZeroInteger;
            DivisionResult := DivPart (FirstNum.Numerator, SecondNum.Numerator);

            if DivisionResult.Remnant = ZeroPart then  -- modulus is zero
               Result := ZeroInteger;

            else                                  -- modulus is non zero
               ResultLocal.Numerator :=
                 SubtractPart (MultPart (SecondNum.Numerator, AddPart (DivisionResult.Quotient, OnePart)), FirstNum.Numerator);

               ResultLocal.IsPositive := SecondNum.IsPositive;
               Result                 := ResultLocal;
            end if;  -- of either the zero remainder or actual remainder case
            Ok := OkLocal;
         else -----------------------------some Sort of error
            Result := NoValue;
            Ok     := OkLocal;
         end if;
      end if;
   end Modulus;

   --------------------------------------------------------------------------

   function IsAPositivePowerOf2 (Num : in Value) return Boolean is
      NormNum       : Value;
      DR            : Division_Result_Type;
      D             : Part;
      IsAPowerOfTwo : Boolean;
   begin
      IsAPowerOfTwo := True;

      if Num.Sort = IntegerValue and then Num.IsPositive then

         NormNum := Num;
         Normalize (NormNum);

         D := NormNum.Numerator;

         if D /= ZeroPart then

            -- A positive power of 2 has a single "1" digit in binary,
            -- so if we shift right (i.e. divide by 2) repeatedly, we should
            -- never get a remainder until the current value is 1.
            while D /= ZeroPart loop
               DR := DivPart (D, TwoPart);

               if D /= OnePart and DR.Remnant /= ZeroPart then
                  -- We have a non-zero remainder, and D is not 1, so
                  -- the original number could not have been a power of 2.
                  IsAPowerOfTwo := False;
                  exit;
               end if;

               D := DR.Quotient;
            end loop;
         else
            IsAPowerOfTwo := False;
         end if;
      else
         IsAPowerOfTwo := False;
      end if;

      return IsAPowerOfTwo;
   end IsAPositivePowerOf2;

   --------------------------------------------------------------------------

   function BoolToValue (B : Boolean) return Value is
      Result : Value;
   begin
      if B then
         Result := TrueValue;
      else
         Result := FalseValue;
      end if;
      return Result;
   end BoolToValue;

   ----------------------------------------------------------------------------

   procedure Greater (FirstNum, SecondNum : in     Value;
                      Result              :    out Value;
                      Ok                  :    out ErrorCode) is
      BoolResult : Boolean;
      OkLocal    : ErrorCode;
   begin
      if HasNoValue (FirstNum) or else HasNoValue (SecondNum) then
         Result  := NoValue;
         OkLocal := NoError;
      else
         GreaterLocal (FirstNum, SecondNum, BoolResult, OkLocal);
         if OkLocal = NoError then
            Result := BoolToValue (BoolResult);
         else
            Result := NoValue;
         end if;
      end if;
      Ok := OkLocal;
   end Greater;

   --------------------------------------------------------------------------

   procedure Lesser (FirstNum, SecondNum : in     Value;
                     Result              :    out Value;
                     Ok                  :    out ErrorCode) is
      BoolResult : Boolean;
      OkLocal    : ErrorCode;
   begin
      if HasNoValue (FirstNum) or else HasNoValue (SecondNum) then
         Result  := NoValue;
         OkLocal := NoError;
      else
         GreaterLocal (SecondNum, FirstNum, BoolResult, OkLocal);
         if OkLocal = NoError then
            Result := BoolToValue (BoolResult);
         else
            Result := NoValue;
         end if;
      end if;
      Ok := OkLocal;
   end Lesser;

   ----------------------------------------------------------------------------

   procedure FloorCeilInternal
     (Val     : in     Value;
      DoFloor :        Boolean;
      Result  :    out Value;
      OK      :    out ErrorCode)
   --# derives OK,
   --#         Result from DoFloor,
   --#                     Val;
   is
      Num          : Value;
      Denom        : Value;
      Temp_Swap    : Part;
      Mod_Val      : Value;
      New_Num      : Value;
      Error        : ErrorCode;
      Ceil_Temp    : Value;
      Final_Result : Value;
   begin
      -- floor (Num / Denom) = (Num - Num mod Denom) / Denom
      -- if_zero (val, result) = if val = 0 then (result) else (0);
      -- ceiling (Num / Denom) = (Nun - Num mod Denom + if_zero (Num mod Denom, Denom)) /
      --                                                                             Denom

      if Val.Sort = IntegerValue then
         Result := Val;
         OK     := NoError;
      elsif Val.Sort = RealValue then
         Num             := Val;
         Num.Denominator := OnePart;
         Num.Sort        := IntegerValue;
         Normalize (Num);

         Denom             := Val;
         Temp_Swap         := Denom.Denominator;
         Denom.Denominator := OnePart;
         Denom.Numerator   := Temp_Swap;
         Denom.Sort        := IntegerValue;
         Denom.IsPositive  := True;
         Normalize (Denom);

         Modulus (Num, Denom, Mod_Val, Error);

         if not DoFloor then -- doing a ceiling operation, so check if addition req'd
            Greater (Mod_Val, ZeroInteger, Ceil_Temp, Error);
            if Ceil_Temp = TrueValue then
               Add (Num, Denom, Ceil_Temp, Error);
               Num := Ceil_Temp;
            end if;
         end if;

         if Error = NoError then
            Subtract (Num, Mod_Val, New_Num, Error);
            if Error = NoError then
               Normalize (New_Num);
               Divide (New_Num, Denom, Final_Result, Error);
               if Error /= NoError then
                  Result := NoValue;
                  OK     := Error;
               else
                  Final_Result.Sort := RealValue;
                  Normalize (Final_Result);
                  Result := Final_Result;
                  OK     := NoError;
               end if;
            else
               Result := NoValue;
               OK     := Error;
            end if;
         else
            Result := NoValue;
            OK     := NoError;
         end if;

      elsif HasNoValue (Val) then
         OK     := NoError;
         Result := NoValue;
      else
         OK     := IllegalValue;
         Result := NoValue;
      end if;

   end FloorCeilInternal;

   ----------------------------------------------------------------------------

   procedure Floor (Val    : in     Value;
                    Result :    out Value;
                    OK     :    out ErrorCode) is
   begin
      FloorCeilInternal (Val, True, Result, OK);
   end Floor;

   ----------------------------------------------------------------------------

   procedure Ceiling (Val    : in     Value;
                      Result :    out Value;
                      OK     :    out ErrorCode) is
   begin
      FloorCeilInternal (Val, False, Result, OK);
   end Ceiling;

   --------------------------------------------------------------------------

   procedure LesserOrEqual (FirstNum, SecondNum : in     Value;
                            Result              :    out Value;
                            Ok                  :    out ErrorCode) is
      Res : Value;
   begin
      if HasNoValue (FirstNum) or else HasNoValue (SecondNum) then
         Result := NoValue;
         Ok     := NoError;
      else
         Lesser (FirstNum, SecondNum, Res, Ok);
         if Res.Sort = TruthValue then
            Result := BoolToValue ((Res = TrueValue) or (FirstNum = SecondNum));
         else
            Result := NoValue;
         end if;
      end if;
   end LesserOrEqual;

   --------------------------------------------------------------------------

   procedure GreaterOrEqual (FirstNum, SecondNum : in     Value;
                             Result              :    out Value;
                             Ok                  :    out ErrorCode) is
      Res     : Boolean;
      OkLocal : ErrorCode;
   begin
      if HasNoValue (FirstNum) or else HasNoValue (SecondNum) then
         Result  := NoValue;
         OkLocal := NoError;
      else
         GreaterLocal (FirstNum, SecondNum, Res, OkLocal);
         if OkLocal = NoError then
            Result := BoolToValue (Res or (FirstNum = SecondNum));
         else
            Result := NoValue;
         end if;
      end if;
      Ok := OkLocal;
   end GreaterOrEqual;

   --------------------------------------------------------------------------

   procedure RaiseByPower (FirstNum, SecondNum : in     Value;
                           Result              :    out Value;
                           Ok                  :    out ErrorCode) is
      Swap : Part;
      N    : Part;
      Q    : Division_Result_Type;
      Y    : Value;
      Z    : Value;
      Temp : Value;
      Err  : ErrorCode;
   begin
      if HasNoValue (FirstNum) or else HasNoValue (SecondNum) then
         Result := NoValue;
         Ok     := NoError;

      elsif (SecondNum.Sort = RealValue) then
         Result := NoValue;
         Ok     := IllegalOperation;

         --608--new elsif to catch INT ** NEGATIVE
      elsif ((FirstNum.Sort = IntegerValue) and (not SecondNum.IsPositive)) then
         Result := NoValue;
         Ok     := ConstraintError;

         --822--new elsif to catch 0.0 ** negative
      elsif ((FirstNum = ZeroReal) and then (not SecondNum.IsPositive)) then
         Result := NoValue;
         Ok     := ConstraintError;

      elsif SecondNum = ZeroInteger then         -- we must return 1
         if FirstNum.Sort = IntegerValue then
            Result := ZeroInteger;
         else
            Result := ZeroReal;
         end if;
         Result.Numerator := OnePart;
         Ok               := NoError;
      else -- we have legal and meaningful operation

         -- Bit-wise algorithm.  See Knuth Volume 2, section 4.6.3.
         N   := SecondNum.Numerator;
         Y   := OneInteger;
         Z   := FirstNum;
         Err := NoError;

         loop
            Q := DivPart (N, TwoPart);
            if Q.Remnant = OnePart then
               -- N is odd, so
               -- Y := Y * Z;
               Multiply (Y, Z, Temp, Err);
               Y := Temp;
            end if;
            -- N := Floor(N/2);
            N := Q.Quotient;

            exit when N = ZeroPart;

            -- Z := Z * Z;
            Multiply (Z, Z, Temp, Err);
            Z := Temp;
         end loop;

         -- If exponent was negative then form the
         -- reciprocal of Y
         if not SecondNum.IsPositive then   --reciprocate
            Swap          := Y.Numerator;
            Y.Numerator   := Y.Denominator;
            Y.Denominator := Swap;
            Normalize (Y);
         end if;
         Result := Y;
         Ok     := Err;
      end if;
   end RaiseByPower;

   ----------------------------------------------------------------------------
   -- Support of non-Numeric types
   ----------------------------------------------------------------------------

   procedure ValueToBool (TheVal : in     Value;
                          Result :    out Boolean;
                          Ok     :    out ErrorCode) is
   begin
      if TheVal.Sort = TruthValue then
         Ok     := NoError;
         Result := TheVal.IsPositive;
      else
         Result := False;
         Ok     := TypeMismatch;
      end if;
   end ValueToBool;

   ----------------------------------------------------------------------------

   function AndOp (LeftVal, RightVal : Value) return Value is
      Result : Value;
   begin
      if LeftVal.Sort = TruthValue and RightVal.Sort = TruthValue then
         -- Boolean "and"
         Result            := FalseValue;
         Result.IsPositive := LeftVal.IsPositive and RightVal.IsPositive;

      elsif LeftVal.Sort = IntegerValue and RightVal.Sort = IntegerValue then
         -- Must be a modular (bitwise) "and" operator
         Result :=
           Value'
           (Numerator   => BitsToPart (PartToBits (LeftVal.Numerator) and PartToBits (RightVal.Numerator)),
            Denominator => OnePart,
            IsPositive  => True,
            Sort        => IntegerValue);

      else
         Result := NoValue;
      end if;

      return Result;

   end AndOp;

   ----------------------------------------------------------------------------

   function OrOp (LeftVal, RightVal : Value) return Value is
      Result : Value;
   begin
      if LeftVal.Sort = TruthValue and RightVal.Sort = TruthValue then
         -- Boolean "or"
         Result            := FalseValue;
         Result.IsPositive := LeftVal.IsPositive or RightVal.IsPositive;

      elsif LeftVal.Sort = IntegerValue and RightVal.Sort = IntegerValue then
         -- Must be a modular (bitwise) "or" operator
         Result :=
           Value'
           (Numerator   => BitsToPart (PartToBits (LeftVal.Numerator) or PartToBits (RightVal.Numerator)),
            Denominator => OnePart,
            IsPositive  => True,
            Sort        => IntegerValue);

      else
         Result := NoValue;
      end if;

      return Result;

   end OrOp;

   ----------------------------------------------------------------------------
   function XorOp (LeftVal, RightVal : Value) return Value is
      Result : Value;
   begin
      if LeftVal.Sort = TruthValue and RightVal.Sort = TruthValue then
         -- Boolean "xor"
         Result            := FalseValue;
         Result.IsPositive := LeftVal.IsPositive xor RightVal.IsPositive;

      elsif LeftVal.Sort = IntegerValue and RightVal.Sort = IntegerValue then
         -- Must be a modular (bitwise) "xor" operator
         Result :=
           Value'
           (Numerator   => BitsToPart (PartToBits (LeftVal.Numerator) xor PartToBits (RightVal.Numerator)),
            Denominator => OnePart,
            IsPositive  => True,
            Sort        => IntegerValue);
      else
         Result := NoValue;
      end if;

      return Result;
   end XorOp;

   ----------------------------------------------------------------------------

   procedure NotOp (TheVal : in out Value) is
   begin
      if not HasNoValue (TheVal) then
         TheVal.IsPositive := not TheVal.IsPositive;
      end if;
   end NotOp;

   ----------------------------------------------------------------------------

   -- nb this implementation ONLY WORKS iff TheModulus is a positive power of 2!
   procedure ModularNotOp (TheVal     : in out Value;
                           TheModulus : in     Value) is
      TypeLast, NumeratorPart : Part;
   begin
      -- for type T is 2**N, then the LRM 4.5.6(5) says that
      -- not X == (T'Last - X) == ((T'Modulus - 1) - X), so...

      TypeLast      := SubtractPart (TheModulus.Numerator, OnePart);
      NumeratorPart := SubtractPart (TypeLast, TheVal.Numerator);
      TheVal        := Value'(Numerator   => NumeratorPart,
                              Denominator => OnePart,
                              IsPositive  => True,
                              Sort        => IntegerValue);
   end ModularNotOp;

   ----------------------------------------------------------------------------

   procedure InsideRange (Val, LowerBound, UpperBound : in     Value;
                          Result                      :    out Value;
                          Ok                          :    out ErrorCode) is
      Result1, Result2 : Value;
      Tmp_Ok1, Tmp_Ok2 : ErrorCode;
   begin
      --# accept F, 10, Tmp_Ok1, "Tmp_Ok1 not used here" &
      --#        F, 33, Tmp_Ok1, "Tmp_Ok1 not used here" &
      --#        F, 10, Tmp_Ok2, "Tmp_Ok2 not used here" &
      --#        F, 33, Tmp_Ok2, "Tmp_Ok2 not used here";
      if Val.Sort = LowerBound.Sort and then LowerBound.Sort = UpperBound.Sort then
         GreaterOrEqual (Val, LowerBound, Result1, Tmp_Ok1);
         LesserOrEqual (Val, UpperBound, Result2, Tmp_Ok2);
         if Result1.Sort = UnknownValue or else Result2.Sort = UnknownValue then
            Result := NoValue;
         else
            Result := AndOp (Result1, Result2);
         end if;
         Ok := NoError;
      else
         Result := NoValue;
         Ok     := TypeMismatch;
      end if;
   end InsideRange;

   ----------------------------------------------------------------------------

   procedure OutsideRange (Val, LowerBound, UpperBound : in     Value;
                           Result                      :    out Value;
                           Ok                          :    out ErrorCode) is
      Result1, Result2 : Value;
      Tmp_Ok1, Tmp_Ok2 : ErrorCode;
   begin
      --# accept F, 10, Tmp_Ok1, "Tmp_Ok1 not used here" &
      --#        F, 33, Tmp_Ok1, "Tmp_Ok1 not used here" &
      --#        F, 10, Tmp_Ok2, "Tmp_Ok2 not used here" &
      --#        F, 33, Tmp_Ok2, "Tmp_Ok2 not used here";
      if Val.Sort = LowerBound.Sort and then LowerBound.Sort = UpperBound.Sort then
         Greater (Val, -- CFR 430 Flow error here: internal anomaly to be fixed
                  UpperBound, Result1, Tmp_Ok1);
         Lesser (Val, LowerBound, Result2, Tmp_Ok2);
         if Result1.Sort = UnknownValue or else Result2.Sort = UnknownValue then
            Result := NoValue;
         else
            Result := OrOp (Result1, Result2);
         end if;
         Ok := NoError;
      else
         Result := NoValue;
         Ok     := TypeMismatch;
      end if;
   end OutsideRange;

   ----------------------------------------------------------------------------
   --attribute ops
   ----------------------------------------------------------------------------

   procedure PredOp (TheVal : in out Value;
                     Ok     :    out ErrorCode) is
      DedVal : Value;
   begin
      if HasNoValue (TheVal) then
         Ok := NoError;
      elsif TheVal.Sort = RealValue then
         Ok     := TypeMismatch;
         TheVal := NoValue;
      else
         Subtract (TheVal, OneInteger,
                   -- to get
                   DedVal, Ok);
         TheVal := DedVal;
      end if;
   end PredOp;

   ----------------------------------------------------------------------------

   procedure SuccOp (TheVal : in out Value;
                     Ok     :    out ErrorCode) is
      AddVal : Value;
   begin
      if HasNoValue (TheVal) then
         Ok := NoError;
      elsif TheVal.Sort = RealValue then
         Ok     := TypeMismatch;
         TheVal := NoValue;
      else
         Add (TheVal, OneInteger,
              -- to get
              AddVal, Ok);
         TheVal := AddVal;
      end if;
   end SuccOp;

   ----------------------------------------------------------------------------

   function MakeEnum (Pos : Natural) return Value is
      Result : Value;
   begin
      Result           := ZeroInteger;
      Result.Numerator := NaturalToPart (Pos);
      return Result;
   end MakeEnum;

   ----------------------------------------------------------------------------

   function IsIntegerValue (Val : Value) return Boolean is
   begin
      return Val.Sort = IntegerValue;
   end IsIntegerValue;

   ------------------------------------

   function IsRealValue (Val : Value) return Boolean is
   begin
      return Val.Sort = RealValue;
   end IsRealValue;

   ----------------------------------------------------------------------------

   function Ada95RealToInteger (TheReal : Value) return Value is
      Temp        : Value;
      Result      : Value;
      TheRem      : Value;
      RoundResult : Value;
      Unused      : ErrorCode;
      DivRes      : Division_Result_Type;
   begin
      --# accept F, 10, Unused, "Unused unused here" &
      --#        F, 33, Unused, "Unused unused here";
      if HasNoValue (TheReal) then
         Result := NoValue;
      else
         --get Quotient and remainder
         DivRes := DivPart (TheReal.Numerator, TheReal.Denominator);

         --create Integer truncated Part from Quotient
         Result := Value'(Numerator   => DivRes.Quotient,
                          Denominator => OnePart,
                          IsPositive  => True,
                          Sort        => IntegerValue);

         --create fractional remainder Part
         TheRem := Value'(Numerator   => DivRes.Remnant,
                          Denominator => TheReal.Denominator,
                          IsPositive  => True,
                          Sort        => RealValue);

         --see if remainder >= 0.5
         GreaterOrEqual (TheRem, ExactHalf,
                         --to get
                         RoundResult, Unused);

         if RoundResult = TrueValue then
            Temp := Result;  --needed because of aliasing in next call
            Add (Temp, OneInteger,
                 --to get
                 Result, Unused);
         end if;

         if Result /= ZeroInteger then
            Result.IsPositive := TheReal.IsPositive; --restore sign
         end if;
      end if;

      return Result;

   end Ada95RealToInteger;

   ----------------------------------------------------------------------------
   --                        Initialisation
   --------------------------------------------------------------------------

   --none

end Maths;