aboutsummaryrefslogtreecommitdiff
path: root/manuals/bcl.3.md
blob: fa566d161c4357742e889284532c16d1681f1742 (plain)
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
<!---

SPDX-License-Identifier: BSD-2-Clause

Copyright (c) 2018-2023 Gavin D. Howard and contributors.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
  list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
  this list of conditions and the following disclaimer in the documentation
  and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.

-->

# NAME

bcl - library of arbitrary precision decimal arithmetic

# SYNOPSIS

## Use

*#include <bcl.h>*

Link with *-lbcl*, and on POSIX systems, *-lpthread* is also required.

## Setup

These items allow clients to set up bcl(3).

**BclError bcl_start(**_void_**);**

**void bcl_end(**_void_**);**

**BclError bcl_init(**_void_**);**

**void bcl_free(**_void_**);**

**bool bcl_abortOnFatalError(**_void_**);**

**void bcl_setAbortOnFatalError(bool** _abrt_**);**

**bool bcl_leadingZeroes(**_void_**);**

**void bcl_setLeadingZeroes(bool** _leadingZeroes_**);**

**void bcl_gc(**_void_**);**

**bool bcl_digitClamp(**_void_**);**

**void bcl_setDigitClamp(bool** _digitClamp_**);**

## Contexts

These items will allow clients to handle contexts, which are isolated from each
other. This allows more than one client to use bcl(3) in the same program.

**struct BclCtxt;**

**typedef struct BclCtxt\* BclContext;**

**BclContext bcl_ctxt_create(**_void_**);**

**void bcl_ctxt_free(BclContext** _ctxt_**);**

**BclError bcl_pushContext(BclContext** _ctxt_**);**

**void bcl_popContext(**_void_**);**

**BclContext bcl_context(**_void_**);**

**void bcl_ctxt_freeNums(BclContext** _ctxt_**);**

**size_t bcl_ctxt_scale(BclContext** _ctxt_**);**

**void bcl_ctxt_setScale(BclContext** _ctxt_**, size_t** _scale_**);**

**size_t bcl_ctxt_ibase(BclContext** _ctxt_**);**

**void bcl_ctxt_setIbase(BclContext** _ctxt_**, size_t** _ibase_**);**

**size_t bcl_ctxt_obase(BclContext** _ctxt_**);**

**void bcl_ctxt_setObase(BclContext** _ctxt_**, size_t** _obase_**);**

## Errors

These items allow clients to handle errors.

**typedef enum BclError BclError;**

**BclError bcl_err(BclNumber** _n_**);**

## Numbers

These items allow clients to manipulate and query the arbitrary-precision
numbers managed by bcl(3).

**typedef struct { size_t i; } BclNumber;**

**BclNumber bcl_num_create(**_void_**);**

**void bcl_num_free(BclNumber** _n_**);**

**bool bcl_num_neg(BclNumber** _n_**);**

**void bcl_num_setNeg(BclNumber** _n_**, bool** _neg_**);**

**size_t bcl_num_scale(BclNumber** _n_**);**

**BclError bcl_num_setScale(BclNumber** _n_**, size_t** _scale_**);**

**size_t bcl_num_len(BclNumber** _n_**);**

## Conversion

These items allow clients to convert numbers into and from strings and integers.

**BclNumber bcl_parse(const char \*restrict** _val_**);**

**char\* bcl_string(BclNumber** _n_**);**

**char\* bcl_string_keep(BclNumber** _n_**);**

**BclError bcl_bigdig(BclNumber** _n_**, BclBigDig \***_result_**);**

**BclError bcl_bigdig_keep(BclNumber** _n_**, BclBigDig \***_result_**);**

**BclNumber bcl_bigdig2num(BclBigDig** _val_**);**

## Math

These items allow clients to run math on numbers.

**BclNumber bcl_add(BclNumber** _a_**, BclNumber** _b_**);**

**BclNumber bcl_add_keep(BclNumber** _a_**, BclNumber** _b_**);**

**BclNumber bcl_sub(BclNumber** _a_**, BclNumber** _b_**);**

**BclNumber bcl_sub_keep(BclNumber** _a_**, BclNumber** _b_**);**

**BclNumber bcl_mul(BclNumber** _a_**, BclNumber** _b_**);**

**BclNumber bcl_mul_keep(BclNumber** _a_**, BclNumber** _b_**);**

**BclNumber bcl_div(BclNumber** _a_**, BclNumber** _b_**);**

**BclNumber bcl_div_keep(BclNumber** _a_**, BclNumber** _b_**);**

**BclNumber bcl_mod(BclNumber** _a_**, BclNumber** _b_**);**

**BclNumber bcl_mod_keep(BclNumber** _a_**, BclNumber** _b_**);**

**BclNumber bcl_pow(BclNumber** _a_**, BclNumber** _b_**);**

**BclNumber bcl_pow_keep(BclNumber** _a_**, BclNumber** _b_**);**

**BclNumber bcl_lshift(BclNumber** _a_**, BclNumber** _b_**);**

**BclNumber bcl_lshift_keep(BclNumber** _a_**, BclNumber** _b_**);**

**BclNumber bcl_rshift(BclNumber** _a_**, BclNumber** _b_**);**

**BclNumber bcl_rshift_keep(BclNumber** _a_**, BclNumber** _b_**);**

**BclNumber bcl_sqrt(BclNumber** _a_**);**

**BclNumber bcl_sqrt_keep(BclNumber** _a_**);**

**BclError bcl_divmod(BclNumber** _a_**, BclNumber** _b_**, BclNumber \***_c_**, BclNumber \***_d_**);**

**BclError bcl_divmod_keep(BclNumber** _a_**, BclNumber** _b_**, BclNumber \***_c_**, BclNumber \***_d_**);**

**BclNumber bcl_modexp(BclNumber** _a_**, BclNumber** _b_**, BclNumber** _c_**);**

**BclNumber bcl_modexp_keep(BclNumber** _a_**, BclNumber** _b_**, BclNumber** _c_**);**

## Miscellaneous

These items are miscellaneous.

**void bcl_zero(BclNumber** _n_**);**

**void bcl_one(BclNumber** _n_**);**

**ssize_t bcl_cmp(BclNumber** _a_**, BclNumber** _b_**);**

**BclError bcl_copy(BclNumber** _d_**, BclNumber** _s_**);**

**BclNumber bcl_dup(BclNumber** _s_**);**

## Pseudo-Random Number Generator

These items allow clients to manipulate the seeded pseudo-random number
generator in bcl(3).

**#define BCL_SEED_ULONGS**

**#define BCL_SEED_SIZE**

**typedef unsigned long BclBigDig;**

**typedef unsigned long BclRandInt;**

**BclNumber bcl_irand(BclNumber** _a_**);**

**BclNumber bcl_irand_keep(BclNumber** _a_**);**

**BclNumber bcl_frand(size_t** _places_**);**

**BclNumber bcl_ifrand(BclNumber** _a_**, size_t** _places_**);**

**BclNumber bcl_ifrand_keep(BclNumber** _a_**, size_t** _places_**);**

**BclError bcl_rand_seedWithNum(BclNumber** _n_**);**

**BclError bcl_rand_seedWithNum_keep(BclNumber** _n_**);**

**BclError bcl_rand_seed(unsigned char** _seed_**[**_BCL_SEED_SIZE_**]);**

**void bcl_rand_reseed(**_void_**);**

**BclNumber bcl_rand_seed2num(**_void_**);**

**BclRandInt bcl_rand_int(**_void_**);**

**BclRandInt bcl_rand_bounded(BclRandInt** _bound_**);**

# DESCRIPTION

bcl(3) is a library that implements arbitrary-precision decimal math, as
standardized by POSIX
(https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html) in bc(1).

bcl(3) assumes that it is allowed to use the **bcl**, **Bcl**, **bc**, and
**Bc** prefixes for symbol names without collision.

All of the items in its interface are described below. See the documentation for
each function for what each function can return.

## Setup

**BclError bcl_start(**_void_**)**

:   Initializes this library. This function can be called multiple times, but
    **bcl_end()** must only be called *once*. This is to make it possible for
    multiple libraries and applications to initialize bcl(3) without problem.

    It is suggested that client libraries call this function, but do not call
    **bcl_end()**, and client applications should call both.

    If there was no error, **BCL_ERROR_NONE** is returned. Otherwise, this
    function can return:

    * **BCL_ERROR_FATAL_ALLOC_ERR**

    This function must be the first one clients call. Calling any other
    function without calling this one first is undefined behavior.

**void bcl_end(**_void_**)**

:   Deinitializes this library. This function must only be called *once*.

    All data must have been freed before calling this function.

    This function must be the last one clients call. Calling this function
    before calling any other function is undefined behavior.

**BclError bcl_init(**_void_**)**

:   Initializes the library for the current thread. This function can be called
    multiple times, but each call must be matched by a call to
    **bcl_free(**_void_**)**. This is to make it possible for multiple libraries
    and applications to initialize threads for bcl(3) without problem.

    This function *must* be called from the thread that it is supposed to
    initialize.

    If there was no error, **BCL_ERROR_NONE** is returned. Otherwise, this
    function can return:

    * **BCL_ERROR_FATAL_ALLOC_ERR**

    This function must be the second one clients call. Calling any other
    function without calling **bcl_start()** and then this one first is
    undefined behavior, except in the case of new threads. New threads can
    safely call this function without calling **bcl_start()** if another thread
    has previously called **bcl_start()**. But this function must still be the
    first function in bcl(3) called by that new thread.

**void bcl_free(**_void_**)**

:   Decrements bcl(3)'s reference count and frees the data associated with it if
    the reference count is **0**.

    This function *must* be called from the thread that it is supposed to
    deinitialize.

    This function must be the second to last one clients call. Calling this
    function before calling any other function besides **bcl_end()** is
    undefined behavior.

**bool bcl_abortOnFatalError(**_void_**)**

:   Queries and returns the current state of calling **abort()** on fatal
    errors. If **true** is returned, bcl(3) will cause a **SIGABRT** if a fatal
    error occurs.

    If activated, clients do not need to check for fatal errors.

    This value is *thread-local*; it applies to just the thread it is read on.

    The default is **false**.

**void bcl_setAbortOnFatalError(bool** _abrt_**)**

:   Sets the state of calling **abort()** on fatal errors. If *abrt* is
    **false**, bcl(3) will not cause a **SIGABRT** on fatal errors after the
    call. If *abrt* is **true**, bcl(3) will cause a **SIGABRT** on fatal errors
    after the call.

    This value is *thread-local*; it applies to just the thread it is set on.

    If activated, clients do not need to check for fatal errors.

**bool bcl_leadingZeroes(**_void_**)**

:   Queries and returns the state of whether leading zeroes are added to strings
    returned by **bcl_string()** when numbers are greater than **-1**, less than
    **1**, and not equal to **0**. If **true** is returned, then leading zeroes
    will be added.

    This value is *thread-local*; it applies to just the thread it is read on.

    The default is **false**.

**void bcl_setLeadingZeroes(bool** _leadingZeroes_**)**

:   Sets the state of whether leading zeroes are added to strings returned by
    **bcl_string()** when numbers are greater than **-1**, less than **1**, and
    not equal to **0**. If *leadingZeroes* is **true**, leading zeroes will be
    added to strings returned by **bcl_string()**.

    This value is *thread-local*; it applies to just the thread it is set on.

**bool bcl_digitClamp(**_void_**)**

:   Queries and returns the state of whether digits in number strings that are
    greater than or equal to the current **ibase** are clamped or not.

    If **true** is returned, then digits are treated as though they are equal to
    the value of **ibase** minus **1**. If this is *not* true, then digits are
    treated as though they are equal to the value they would have if **ibase**
    was large enough. They are then multiplied by the appropriate power of
    **ibase**.

    For example, with clamping off and an **ibase** of **3**, the string "AB"
    would equal **3\^1\*A+3\^0\*B**, which is **3** times **10** plus **11**, or
    **41**, while with clamping on and an **ibase** of **3**, the string "AB"
    would be equal to **3\^1\*2+3\^0\*2**, which is **3** times **2** plus
    **2**, or **8**.

    This value is *thread-local*; it applies to just the thread it is read on.

    The default is **true**.

**void bcl_setDigitClamp(bool** _digitClamp_**)**

:   Sets the state of whether digits in number strings that are greater than or
    equal to the current **ibase** are clamped or not. For more information, see
    the **bcl_digitClamp(**_void_**)** function.

    This value is *thread-local*; it applies to just the thread it is set on.

**void bcl_gc(**_void_**)**

:   Garbage collects cached instances of arbitrary-precision numbers. This only
    frees the memory of numbers that are *not* in use, so it is safe to call at
    any time.

## Contexts

All procedures that take a **BclContext** parameter a require a valid context as
an argument.

**struct BclCtxt**

:   A forward declaration for a hidden **struct** type. Clients cannot access
    the internals of the **struct** type directly. All interactions with the
    type are done through pointers. See **BclContext** below.

**BclContext**

:   A typedef to a pointer of **struct BclCtxt**. This is the only handle
    clients can get to **struct BclCtxt**.

    A **BclContext** contains the values **scale**, **ibase**, and **obase**, as
    well as a list of numbers.

    **scale** is a value used to control how many decimal places calculations
    should use. A value of **0** means that calculations are done on integers
    only, where applicable, and a value of 20, for example, means that all
    applicable calculations return results with 20 decimal places. The default
    is **0**.

    **ibase** is a value used to control the input base. The minimum **ibase**
    is **2**, and the maximum is **36**. If **ibase** is **2**, numbers are
    parsed as though they are in binary, and any digits larger than **1** are
    clamped. Likewise, a value of **10** means that numbers are parsed as though
    they are decimal, and any larger digits are clamped. The default is **10**.

    **obase** is a value used to control the output base. The minimum **obase**
    is **0** and the maximum is **BC_BASE_MAX** (see the **LIMITS** section).

    Numbers created in one context are not valid in another context. It is
    undefined behavior to use a number created in a different context. Contexts
    are meant to isolate the numbers used by different clients in the same
    application.

    Different threads also have different contexts, so any numbers created in
    one thread are not valid in another thread. To pass values between contexts
    and threads, use **bcl_string()** to produce a string to pass around, and
    use **bcl_parse()** to parse the string. It is suggested that the **obase**
    used to create the string be passed around with the string and used as the
    **ibase** for **bcl_parse()** to ensure that the number will be the same.

**BclContext bcl_ctxt_create(**_void_**)**

:   Creates a context and returns it. Returns **NULL** if there was an error.

**void bcl_ctxt_free(BclContext** _ctxt_**)**

:   Frees *ctxt*, after which it is no longer valid. It is undefined behavior to
    attempt to use an invalid context.

**BclError bcl_pushContext(BclContext** _ctxt_**)**

:   Pushes *ctxt* onto bcl(3)'s stack of contexts. *ctxt* must have been created
    with **bcl_ctxt_create(**_void_**)**.

    If there was no error, **BCL_ERROR_NONE** is returned. Otherwise, this
    function can return:

    * **BCL_ERROR_FATAL_ALLOC_ERR**

    There *must* be a valid context to do any arithmetic.

**void bcl_popContext(**_void_**)**

:   Pops the current context off of the stack, if one exists.

**BclContext bcl_context(**_void_**)**

:   Returns the current context, or **NULL** if no context exists.

**void bcl_ctxt_freeNums(BclContext** _ctxt_**)**

:   Frees all numbers in use that are associated with *ctxt*. It is undefined
    behavior to attempt to use a number associated with *ctxt* after calling
    this procedure unless such numbers have been created with
    **bcl_num_create(**_void_**)** after calling this procedure.

**size_t bcl_ctxt_scale(BclContext** _ctxt_**)**

:   Returns the **scale** for given context.

**void bcl_ctxt_setScale(BclContext** _ctxt_**, size_t** _scale_**)**

:   Sets the **scale** for the given context to the argument *scale*.

**size_t bcl_ctxt_ibase(BclContext** _ctxt_**)**

:   Returns the **ibase** for the given context.

**void bcl_ctxt_setIbase(BclContext** _ctxt_**, size_t** _ibase_**)**

:   Sets the **ibase** for the given context to the argument *ibase*. If the
    argument *ibase* is invalid, it clamped, so an *ibase* of **0** or **1** is
    clamped to **2**, and any values above **36** are clamped to **36**.

**size_t bcl_ctxt_obase(BclContext** _ctxt_**)**

:   Returns the **obase** for the given context.

**void bcl_ctxt_setObase(BclContext** _ctxt_**, size_t** _obase_**)**

:   Sets the **obase** for the given context to the argument *obase*.

## Errors

**BclError**

:   An **enum** of possible error codes. See the **ERRORS** section for a
    complete listing the codes.

**BclError bcl_err(BclNumber** _n_**)**

:   Checks for errors in a **BclNumber**. All functions that can return a
    **BclNumber** can encode an error in the number, and this function will
    return the error, if any. If there was no error, it will return
    **BCL_ERROR_NONE**.

    There must be a valid current context.

## Numbers

All procedures in this section require a valid current context.

**BclNumber**

:   A handle to an arbitrary-precision number. The actual number type is not
    exposed; the **BclNumber** handle is the only way clients can refer to
    instances of arbitrary-precision numbers.

**BclNumber bcl_num_create(**_void_**)**

:   Creates and returns a **BclNumber**.

    bcl(3) will encode an error in the return value, if there was one. The error
    can be queried with **bcl_err(BclNumber)**. Possible errors include:

    * **BCL_ERROR_INVALID_CONTEXT**
    * **BCL_ERROR_FATAL_ALLOC_ERR**

**void bcl_num_free(BclNumber** _n_**)**

:   Frees *n*. It is undefined behavior to use *n* after calling this function.

**bool bcl_num_neg(BclNumber** _n_**)**

:   Returns **true** if *n* is negative, **false** otherwise.

**void bcl_num_setNeg(BclNumber** _n_**, bool** _neg_**)**

:   Sets *n*'s sign to *neg*, where **true** is negative, and **false** is
    positive.

**size_t bcl_num_scale(BclNumber** _n_**)**

:   Returns the *scale* of *n*.

    The *scale* of a number is the number of decimal places it has after the
    radix (decimal point).

**BclError bcl_num_setScale(BclNumber** _n_**, size_t** _scale_**)**

:   Sets the *scale* of *n* to the argument *scale*. If the argument *scale* is
    greater than the *scale* of *n*, *n* is extended. If the argument *scale* is
    less than the *scale* of *n*, *n* is truncated.

    If there was no error, **BCL_ERROR_NONE** is returned. Otherwise, this
    function can return:

    * **BCL_ERROR_INVALID_NUM**
    * **BCL_ERROR_INVALID_CONTEXT**
    * **BCL_ERROR_FATAL_ALLOC_ERR**

**size_t bcl_num_len(BclNumber** _n_**)**

:   Returns the number of *significant decimal digits* in *n*.

## Conversion

All procedures in this section require a valid current context.

All procedures in this section without the **_keep** suffix in their name
consume the given **BclNumber** arguments that are not given to pointer
arguments. See the **Consumption and Propagation** subsection below.

**BclNumber bcl_parse(const char \*restrict** _val_**)**

:   Parses a number string according to the current context's **ibase** and
    returns the resulting number.

    *val* must be non-**NULL** and a valid string. See
    **BCL_ERROR_PARSE_INVALID_STR** in the **ERRORS** section for more
    information.

    bcl(3) will encode an error in the return value, if there was one. The error
    can be queried with **bcl_err(BclNumber)**. Possible errors include:

    * **BCL_ERROR_INVALID_NUM**
    * **BCL_ERROR_INVALID_CONTEXT**
    * **BCL_ERROR_PARSE_INVALID_STR**
    * **BCL_ERROR_FATAL_ALLOC_ERR**

**char\* bcl_string(BclNumber** _n_**)**

:   Returns a string representation of *n* according the the current context's
    **ibase**. The string is dynamically allocated and must be freed by the
    caller.

    *n* is consumed; it cannot be used after the call. See the
    **Consumption and Propagation** subsection below.

**char\* bcl_string_keep(BclNumber** _n_**)**

:   Returns a string representation of *n* according the the current context's
    **ibase**. The string is dynamically allocated and must be freed by the
    caller.

**BclError bcl_bigdig(BclNumber** _n_**, BclBigDig \***_result_**)**

:   Converts *n* into a **BclBigDig** and returns the result in the space
    pointed to by *result*.

    *a* must be smaller than **BC_OVERFLOW_MAX**. See the **LIMITS** section.

    If there was no error, **BCL_ERROR_NONE** is returned. Otherwise, this
    function can return:

    * **BCL_ERROR_INVALID_NUM**
    * **BCL_ERROR_INVALID_CONTEXT**
    * **BCL_ERROR_MATH_OVERFLOW**

    *n* is consumed; it cannot be used after the call. See the
    **Consumption and Propagation** subsection below.

**BclError bcl_bigdig_keep(BclNumber** _n_**, BclBigDig \***_result_**)**

:   Converts *n* into a **BclBigDig** and returns the result in the space
    pointed to by *result*.

    *a* must be smaller than **BC_OVERFLOW_MAX**. See the **LIMITS** section.

    If there was no error, **BCL_ERROR_NONE** is returned. Otherwise, this
    function can return:

    * **BCL_ERROR_INVALID_NUM**
    * **BCL_ERROR_INVALID_CONTEXT**
    * **BCL_ERROR_MATH_OVERFLOW**

**BclNumber bcl_bigdig2num(BclBigDig** _val_**)**

:   Creates a **BclNumber** from *val*.

    bcl(3) will encode an error in the return value, if there was one. The error
    can be queried with **bcl_err(BclNumber)**. Possible errors include:

    * **BCL_ERROR_INVALID_CONTEXT**
    * **BCL_ERROR_FATAL_ALLOC_ERR**

## Math

All procedures in this section require a valid current context.

All procedures in this section without the **_keep** suffix in their name
consume the given **BclNumber** arguments that are not given to pointer
arguments. See the **Consumption and Propagation** subsection below.

All procedures in this section can return the following errors:

* **BCL_ERROR_INVALID_NUM**
* **BCL_ERROR_INVALID_CONTEXT**
* **BCL_ERROR_FATAL_ALLOC_ERR**

**BclNumber bcl_add(BclNumber** _a_**, BclNumber** _b_**)**

:   Adds *a* and *b* and returns the result. The *scale* of the result is the
    max of the *scale*s of *a* and *b*.

    *a* and *b* are consumed; they cannot be used after the call. See the
    **Consumption and Propagation** subsection below.

    *a* and *b* can be the same number.

    bcl(3) will encode an error in the return value, if there was one. The error
    can be queried with **bcl_err(BclNumber)**. Possible errors include:

    * **BCL_ERROR_INVALID_NUM**
    * **BCL_ERROR_INVALID_CONTEXT**
    * **BCL_ERROR_FATAL_ALLOC_ERR**

**BclNumber bcl_add_keep(BclNumber** _a_**, BclNumber** _b_**)**

:   Adds *a* and *b* and returns the result. The *scale* of the result is the
    max of the *scale*s of *a* and *b*.

    *a* and *b* can be the same number.

    bcl(3) will encode an error in the return value, if there was one. The error
    can be queried with **bcl_err(BclNumber)**. Possible errors include:

    * **BCL_ERROR_INVALID_NUM**
    * **BCL_ERROR_INVALID_CONTEXT**
    * **BCL_ERROR_FATAL_ALLOC_ERR**

**BclNumber bcl_sub(BclNumber** _a_**, BclNumber** _b_**)**

:   Subtracts *b* from *a* and returns the result. The *scale* of the result is
    the max of the *scale*s of *a* and *b*.

    *a* and *b* are consumed; they cannot be used after the call. See the
    **Consumption and Propagation** subsection below.

    *a* and *b* can be the same number.

    bcl(3) will encode an error in the return value, if there was one. The error
    can be queried with **bcl_err(BclNumber)**. Possible errors include:

    * **BCL_ERROR_INVALID_NUM**
    * **BCL_ERROR_INVALID_CONTEXT**
    * **BCL_ERROR_FATAL_ALLOC_ERR**

**BclNumber bcl_sub_keep(BclNumber** _a_**, BclNumber** _b_**)**

:   Subtracts *b* from *a* and returns the result. The *scale* of the result is
    the max of the *scale*s of *a* and *b*.

    *a* and *b* can be the same number.

    bcl(3) will encode an error in the return value, if there was one. The error
    can be queried with **bcl_err(BclNumber)**. Possible errors include:

    * **BCL_ERROR_INVALID_NUM**
    * **BCL_ERROR_INVALID_CONTEXT**
    * **BCL_ERROR_FATAL_ALLOC_ERR**

**BclNumber bcl_mul(BclNumber** _a_**, BclNumber** _b_**)**

:   Multiplies *a* and *b* and returns the result. If *ascale* is the *scale* of
    *a* and *bscale* is the *scale* of *b*, the *scale* of the result is equal
    to **min(ascale+bscale,max(scale,ascale,bscale))**, where **min()** and
    **max()** return the obvious values.

    *a* and *b* are consumed; they cannot be used after the call. See the
    **Consumption and Propagation** subsection below.

    *a* and *b* can be the same number.

    bcl(3) will encode an error in the return value, if there was one. The error
    can be queried with **bcl_err(BclNumber)**. Possible errors include:

    * **BCL_ERROR_INVALID_NUM**
    * **BCL_ERROR_INVALID_CONTEXT**
    * **BCL_ERROR_FATAL_ALLOC_ERR**

**BclNumber bcl_mul_keep(BclNumber** _a_**, BclNumber** _b_**)**

:   Multiplies *a* and *b* and returns the result. If *ascale* is the *scale* of
    *a* and *bscale* is the *scale* of *b*, the *scale* of the result is equal
    to **min(ascale+bscale,max(scale,ascale,bscale))**, where **min()** and
    **max()** return the obvious values.

    *a* and *b* can be the same number.

    bcl(3) will encode an error in the return value, if there was one. The error
    can be queried with **bcl_err(BclNumber)**. Possible errors include:

    * **BCL_ERROR_INVALID_NUM**
    * **BCL_ERROR_INVALID_CONTEXT**
    * **BCL_ERROR_FATAL_ALLOC_ERR**

**BclNumber bcl_div(BclNumber** _a_**, BclNumber** _b_**)**

:   Divides *a* by *b* and returns the result. The *scale* of the result is the
    *scale* of the current context.

    *b* cannot be **0**.

    *a* and *b* are consumed; they cannot be used after the call. See the
    **Consumption and Propagation** subsection below.

    *a* and *b* can be the same number.

    bcl(3) will encode an error in the return value, if there was one. The error
    can be queried with **bcl_err(BclNumber)**. Possible errors include:

    * **BCL_ERROR_INVALID_NUM**
    * **BCL_ERROR_INVALID_CONTEXT**
    * **BCL_ERROR_MATH_DIVIDE_BY_ZERO**
    * **BCL_ERROR_FATAL_ALLOC_ERR**

**BclNumber bcl_div_keep(BclNumber** _a_**, BclNumber** _b_**)**

:   Divides *a* by *b* and returns the result. The *scale* of the result is the
    *scale* of the current context.

    *b* cannot be **0**.

    *a* and *b* can be the same number.

    bcl(3) will encode an error in the return value, if there was one. The error
    can be queried with **bcl_err(BclNumber)**. Possible errors include:

    * **BCL_ERROR_INVALID_NUM**
    * **BCL_ERROR_INVALID_CONTEXT**
    * **BCL_ERROR_MATH_DIVIDE_BY_ZERO**
    * **BCL_ERROR_FATAL_ALLOC_ERR**

**BclNumber bcl_mod(BclNumber** _a_**, BclNumber** _b_**)**

:   Divides *a* by *b* to the *scale* of the current context, computes the
    modulus **a-(a/b)\*b**, and returns the modulus.

    *b* cannot be **0**.

    *a* and *b* are consumed; they cannot be used after the call. See the
    **Consumption and Propagation** subsection below.

    *a* and *b* can be the same number.

    bcl(3) will encode an error in the return value, if there was one. The error
    can be queried with **bcl_err(BclNumber)**. Possible errors include:

    * **BCL_ERROR_INVALID_NUM**
    * **BCL_ERROR_INVALID_CONTEXT**
    * **BCL_ERROR_MATH_DIVIDE_BY_ZERO**
    * **BCL_ERROR_FATAL_ALLOC_ERR**

**BclNumber bcl_mod_keep(BclNumber** _a_**, BclNumber** _b_**)**

:   Divides *a* by *b* to the *scale* of the current context, computes the
    modulus **a-(a/b)\*b**, and returns the modulus.

    *b* cannot be **0**.

    *a* and *b* can be the same number.

    bcl(3) will encode an error in the return value, if there was one. The error
    can be queried with **bcl_err(BclNumber)**. Possible errors include:

    * **BCL_ERROR_INVALID_NUM**
    * **BCL_ERROR_INVALID_CONTEXT**
    * **BCL_ERROR_MATH_DIVIDE_BY_ZERO**
    * **BCL_ERROR_FATAL_ALLOC_ERR**

**BclNumber bcl_pow(BclNumber** _a_**, BclNumber** _b_**)**

:   Calculates *a* to the power of *b* to the *scale* of the current context.
    *b* must be an integer, but can be negative. If it is negative, *a* must
    be non-zero.

    *b* must be an integer. If *b* is negative, *a* must not be **0**.

    *a* must be smaller than **BC_OVERFLOW_MAX**. See the **LIMITS** section.

    *a* and *b* are consumed; they cannot be used after the call. See the
    **Consumption and Propagation** subsection below.

    *a* and *b* can be the same number.

    bcl(3) will encode an error in the return value, if there was one. The error
    can be queried with **bcl_err(BclNumber)**. Possible errors include:

    * **BCL_ERROR_INVALID_NUM**
    * **BCL_ERROR_INVALID_CONTEXT**
    * **BCL_ERROR_MATH_NON_INTEGER**
    * **BCL_ERROR_MATH_OVERFLOW**
    * **BCL_ERROR_MATH_DIVIDE_BY_ZERO**
    * **BCL_ERROR_FATAL_ALLOC_ERR**

**BclNumber bcl_pow_keep(BclNumber** _a_**, BclNumber** _b_**)**

:   Calculates *a* to the power of *b* to the *scale* of the current context.
    *b* must be an integer, but can be negative. If it is negative, *a* must
    be non-zero.

    *b* must be an integer. If *b* is negative, *a* must not be **0**.

    *a* must be smaller than **BC_OVERFLOW_MAX**. See the **LIMITS** section.

    *a* and *b* can be the same number.

    bcl(3) will encode an error in the return value, if there was one. The error
    can be queried with **bcl_err(BclNumber)**. Possible errors include:

    * **BCL_ERROR_INVALID_NUM**
    * **BCL_ERROR_INVALID_CONTEXT**
    * **BCL_ERROR_MATH_NON_INTEGER**
    * **BCL_ERROR_MATH_OVERFLOW**
    * **BCL_ERROR_MATH_DIVIDE_BY_ZERO**
    * **BCL_ERROR_FATAL_ALLOC_ERR**

**BclNumber bcl_lshift(BclNumber** _a_**, BclNumber** _b_**)**

:   Shifts *a* left (moves the radix right) by *b* places and returns the
    result. This is done in decimal. *b* must be an integer.

    *b* must be an integer.

    *a* and *b* are consumed; they cannot be used after the call. See the
    **Consumption and Propagation** subsection below.

    *a* and *b* can be the same number.

    bcl(3) will encode an error in the return value, if there was one. The error
    can be queried with **bcl_err(BclNumber)**. Possible errors include:

    * **BCL_ERROR_INVALID_NUM**
    * **BCL_ERROR_INVALID_CONTEXT**
    * **BCL_ERROR_MATH_NON_INTEGER**
    * **BCL_ERROR_FATAL_ALLOC_ERR**

**BclNumber bcl_lshift_keep(BclNumber** _a_**, BclNumber** _b_**)**

:   Shifts *a* left (moves the radix right) by *b* places and returns the
    result. This is done in decimal. *b* must be an integer.

    *b* must be an integer.

    *a* and *b* can be the same number.

    bcl(3) will encode an error in the return value, if there was one. The error
    can be queried with **bcl_err(BclNumber)**. Possible errors include:

    * **BCL_ERROR_INVALID_NUM**
    * **BCL_ERROR_INVALID_CONTEXT**
    * **BCL_ERROR_MATH_NON_INTEGER**
    * **BCL_ERROR_FATAL_ALLOC_ERR**

**BclNumber bcl_rshift(BclNumber** _a_**, BclNumber** _b_**)**

:   Shifts *a* right (moves the radix left) by *b* places and returns the
    result. This is done in decimal. *b* must be an integer.

    *b* must be an integer.

    *a* and *b* are consumed; they cannot be used after the call. See the
    **Consumption and Propagation** subsection below.

    *a* and *b* can be the same number.

    bcl(3) will encode an error in the return value, if there was one. The error
    can be queried with **bcl_err(BclNumber)**. Possible errors include:

    * **BCL_ERROR_INVALID_NUM**
    * **BCL_ERROR_INVALID_CONTEXT**
    * **BCL_ERROR_MATH_NON_INTEGER**
    * **BCL_ERROR_FATAL_ALLOC_ERR**

**BclNumber bcl_rshift_keep(BclNumber** _a_**, BclNumber** _b_**)**

:   Shifts *a* right (moves the radix left) by *b* places and returns the
    result. This is done in decimal. *b* must be an integer.

    *b* must be an integer.

    *a* and *b* can be the same number.

    bcl(3) will encode an error in the return value, if there was one. The error
    can be queried with **bcl_err(BclNumber)**. Possible errors include:

    * **BCL_ERROR_INVALID_NUM**
    * **BCL_ERROR_INVALID_CONTEXT**
    * **BCL_ERROR_MATH_NON_INTEGER**
    * **BCL_ERROR_FATAL_ALLOC_ERR**

**BclNumber bcl_sqrt(BclNumber** _a_**)**

:   Calculates the square root of *a* and returns the result. The *scale* of the
    result is equal to the **scale** of the current context.

    *a* cannot be negative.

    *a* is consumed; it cannot be used after the call. See the
    **Consumption and Propagation** subsection below.

    bcl(3) will encode an error in the return value, if there was one. The error
    can be queried with **bcl_err(BclNumber)**. Possible errors include:

    * **BCL_ERROR_INVALID_NUM**
    * **BCL_ERROR_INVALID_CONTEXT**
    * **BCL_ERROR_MATH_NEGATIVE**
    * **BCL_ERROR_FATAL_ALLOC_ERR**

**BclNumber bcl_sqrt_keep(BclNumber** _a_**)**

:   Calculates the square root of *a* and returns the result. The *scale* of the
    result is equal to the **scale** of the current context.

    *a* cannot be negative.

    bcl(3) will encode an error in the return value, if there was one. The error
    can be queried with **bcl_err(BclNumber)**. Possible errors include:

    * **BCL_ERROR_INVALID_NUM**
    * **BCL_ERROR_INVALID_CONTEXT**
    * **BCL_ERROR_MATH_NEGATIVE**
    * **BCL_ERROR_FATAL_ALLOC_ERR**

**BclError bcl_divmod(BclNumber** _a_**, BclNumber** _b_**, BclNumber \***_c_**, BclNumber \***_d_**)**

:   Divides *a* by *b* and returns the quotient in a new number which is put
    into the space pointed to by *c*, and puts the modulus in a new number which
    is put into the space pointed to by *d*.

    *b* cannot be **0**.

    *a* and *b* are consumed; they cannot be used after the call. See the
    **Consumption and Propagation** subsection below.

    *c* and *d* cannot point to the same place, nor can they point to the space
    occupied by *a* or *b*.

    If there was no error, **BCL_ERROR_NONE** is returned. Otherwise, this
    function can return:

    * **BCL_ERROR_INVALID_NUM**
    * **BCL_ERROR_INVALID_CONTEXT**
    * **BCL_ERROR_MATH_DIVIDE_BY_ZERO**
    * **BCL_ERROR_FATAL_ALLOC_ERR**

**BclError bcl_divmod_keep(BclNumber** _a_**, BclNumber** _b_**, BclNumber \***_c_**, BclNumber \***_d_**)**

:   Divides *a* by *b* and returns the quotient in a new number which is put
    into the space pointed to by *c*, and puts the modulus in a new number which
    is put into the space pointed to by *d*.

    *b* cannot be **0**.

    *c* and *d* cannot point to the same place, nor can they point to the space
    occupied by *a* or *b*.

    If there was no error, **BCL_ERROR_NONE** is returned. Otherwise, this
    function can return:

    * **BCL_ERROR_INVALID_NUM**
    * **BCL_ERROR_INVALID_CONTEXT**
    * **BCL_ERROR_MATH_DIVIDE_BY_ZERO**
    * **BCL_ERROR_FATAL_ALLOC_ERR**

**BclNumber bcl_modexp(BclNumber** _a_**, BclNumber** _b_**, BclNumber** _c_**)**

:   Computes a modular exponentiation where *a* is the base, *b* is the
    exponent, and *c* is the modulus, and returns the result. The *scale* of the
    result is equal to the **scale** of the current context.

    *a*, *b*, and *c* must be integers. *c* must not be **0**. *b* must not be
    negative.

    *a*, *b*, and *c* are consumed; they cannot be used after the call. See the
    **Consumption and Propagation** subsection below.

    bcl(3) will encode an error in the return value, if there was one. The error
    can be queried with **bcl_err(BclNumber)**. Possible errors include:

    * **BCL_ERROR_INVALID_NUM**
    * **BCL_ERROR_INVALID_CONTEXT**
    * **BCL_ERROR_MATH_NEGATIVE**
    * **BCL_ERROR_MATH_NON_INTEGER**
    * **BCL_ERROR_MATH_DIVIDE_BY_ZERO**
    * **BCL_ERROR_FATAL_ALLOC_ERR**

**BclNumber bcl_modexp_keep(BclNumber** _a_**, BclNumber** _b_**, BclNumber** _c_**)**

:   Computes a modular exponentiation where *a* is the base, *b* is the
    exponent, and *c* is the modulus, and returns the result. The *scale* of the
    result is equal to the **scale** of the current context.

    *a*, *b*, and *c* must be integers. *c* must not be **0**. *b* must not be
    negative.

    bcl(3) will encode an error in the return value, if there was one. The error
    can be queried with **bcl_err(BclNumber)**. Possible errors include:

    * **BCL_ERROR_INVALID_NUM**
    * **BCL_ERROR_INVALID_CONTEXT**
    * **BCL_ERROR_MATH_NEGATIVE**
    * **BCL_ERROR_MATH_NON_INTEGER**
    * **BCL_ERROR_MATH_DIVIDE_BY_ZERO**
    * **BCL_ERROR_FATAL_ALLOC_ERR**

## Miscellaneous

**void bcl_zero(BclNumber** _n_**)**

:   Sets *n* to **0**.

**void bcl_one(BclNumber** _n_**)**

:   Sets *n* to **1**.

**ssize_t bcl_cmp(BclNumber** _a_**, BclNumber** _b_**)**

:   Compares *a* and *b* and returns **0** if *a* and *b* are equal, **<0** if
    *a* is less than *b*, and **>0** if *a* is greater than *b*.

**BclError bcl_copy(BclNumber** _d_**, BclNumber** _s_**)**

:   Copies *s* into *d*.

    If there was no error, **BCL_ERROR_NONE** is returned. Otherwise, this
    function can return:

    * **BCL_ERROR_INVALID_NUM**
    * **BCL_ERROR_INVALID_CONTEXT**
    * **BCL_ERROR_FATAL_ALLOC_ERR**

**BclNumber bcl_dup(BclNumber** _s_**)**

:   Creates and returns a new **BclNumber** that is a copy of *s*.

    bcl(3) will encode an error in the return value, if there was one. The error
    can be queried with **bcl_err(BclNumber)**. Possible errors include:

    * **BCL_ERROR_INVALID_NUM**
    * **BCL_ERROR_INVALID_CONTEXT**
    * **BCL_ERROR_FATAL_ALLOC_ERR**

## Pseudo-Random Number Generator

The pseudo-random number generator in bcl(3) is a *seeded* PRNG. Given the same
seed twice, it will produce the same sequence of pseudo-random numbers twice.

By default, bcl(3) attempts to seed the PRNG with data from **/dev/urandom**. If
that fails, it seeds itself with by calling **libc**'s **srand(time(NULL))** and
then calling **rand()** for each byte, since **rand()** is only guaranteed to
return **15** bits.

This should provide fairly good seeding in the standard case while also
remaining fairly portable.

If necessary, the PRNG can be reseeded with one of the following functions:

* **bcl_rand_seedWithNum(BclNumber)**
* **bcl_rand_seed(unsigned char[**_BCL_SEED_SIZE_**])**
* **bcl_rand_reseed(**_void_**)**

All procedures in this section without the **_keep** suffix in their name
consume the given **BclNumber** arguments that are not given to pointer
arguments. See the **Consumption and Propagation** subsection below.

The following items allow clients to use the pseudo-random number generator. All
procedures require a valid current context.

**BCL_SEED_ULONGS**

:   The number of **unsigned long**'s in a seed for bcl(3)'s random number
    generator.

**BCL_SEED_SIZE**

:   The size, in **char**'s, of a seed for bcl(3)'s random number generator.

**BclBigDig**

:   bcl(3)'s overflow type (see the **PERFORMANCE** section).

**BclRandInt**

:   An unsigned integer type returned by bcl(3)'s random number generator.

**BclNumber bcl_irand(BclNumber** _a_**)**

:   Returns a random number that is not larger than *a* in a new number. If *a*
    is **0** or **1**, the new number is equal to **0**. The bound is unlimited,
    so it is not bound to the size of **BclRandInt**. This is done by generating
    as many random numbers as necessary, multiplying them by certain exponents,
    and adding them all together.

    *a* must be an integer and non-negative.

    *a* is consumed; it cannot be used after the call. See the **Consumption and
    Propagation** subsection below.

    This procedure requires a valid current context.

    bcl(3) will encode an error in the return value, if there was one. The error
    can be queried with **bcl_err(BclNumber)**. Possible errors include:

    * **BCL_ERROR_INVALID_NUM**
    * **BCL_ERROR_INVALID_CONTEXT**
    * **BCL_ERROR_MATH_NEGATIVE**
    * **BCL_ERROR_MATH_NON_INTEGER**
    * **BCL_ERROR_FATAL_ALLOC_ERR**

**BclNumber bcl_irand_keep(BclNumber** _a_**)**

:   Returns a random number that is not larger than *a* in a new number. If *a*
    is **0** or **1**, the new number is equal to **0**. The bound is unlimited,
    so it is not bound to the size of **BclRandInt**. This is done by generating
    as many random numbers as necessary, multiplying them by certain exponents,
    and adding them all together.

    *a* must be an integer and non-negative.

    This procedure requires a valid current context.

    bcl(3) will encode an error in the return value, if there was one. The error
    can be queried with **bcl_err(BclNumber)**. Possible errors include:

    * **BCL_ERROR_INVALID_NUM**
    * **BCL_ERROR_INVALID_CONTEXT**
    * **BCL_ERROR_MATH_NEGATIVE**
    * **BCL_ERROR_MATH_NON_INTEGER**
    * **BCL_ERROR_FATAL_ALLOC_ERR**

**BclNumber bcl_frand(size_t** _places_**)**

:   Returns a random number between **0** (inclusive) and **1** (exclusive) that
    has *places* decimal digits after the radix (decimal point). There are no
    limits on *places*.

    This procedure requires a valid current context.

    bcl(3) will encode an error in the return value, if there was one. The error
    can be queried with **bcl_err(BclNumber)**. Possible errors include:

    * **BCL_ERROR_INVALID_CONTEXT**
    * **BCL_ERROR_FATAL_ALLOC_ERR**

**BclNumber bcl_ifrand(BclNumber** _a_**, size_t** _places_**)**

:   Returns a random number less than *a* with *places* decimal digits after the
    radix (decimal point). There are no limits on *a* or *places*.

    *a* must be an integer and non-negative.

    *a* is consumed; it cannot be used after the call. See the **Consumption and
    Propagation** subsection below.

    This procedure requires a valid current context.

    bcl(3) will encode an error in the return value, if there was one. The error
    can be queried with **bcl_err(BclNumber)**. Possible errors include:

    * **BCL_ERROR_INVALID_NUM**
    * **BCL_ERROR_INVALID_CONTEXT**
    * **BCL_ERROR_MATH_NEGATIVE**
    * **BCL_ERROR_MATH_NON_INTEGER**
    * **BCL_ERROR_FATAL_ALLOC_ERR**

**BclNumber bcl_ifrand_keep(BclNumber** _a_**, size_t** _places_**)**

:   Returns a random number less than *a* with *places* decimal digits after the
    radix (decimal point). There are no limits on *a* or *places*.

    *a* must be an integer and non-negative.

    This procedure requires a valid current context.

    bcl(3) will encode an error in the return value, if there was one. The error
    can be queried with **bcl_err(BclNumber)**. Possible errors include:

    * **BCL_ERROR_INVALID_NUM**
    * **BCL_ERROR_INVALID_CONTEXT**
    * **BCL_ERROR_MATH_NEGATIVE**
    * **BCL_ERROR_MATH_NON_INTEGER**
    * **BCL_ERROR_FATAL_ALLOC_ERR**

**BclError bcl_rand_seedWithNum(BclNumber** _n_**)**

:   Seeds the PRNG with *n*.

    *n* is consumed.

    This procedure requires a valid current context.

    If there was no error, **BCL_ERROR_NONE** is returned. Otherwise, this
    function can return:

    * **BCL_ERROR_INVALID_NUM**
    * **BCL_ERROR_INVALID_CONTEXT**

    Note that if **bcl_rand_seed2num(**_void_**)** or
    **bcl_rand_seed2num_err(BclNumber)** are called right after this function,
    they are not guaranteed to return a number equal to *n*.

**BclError bcl_rand_seedWithNum_keep(BclNumber** _n_**)**

:   Seeds the PRNG with *n*.

    This procedure requires a valid current context.

    If there was no error, **BCL_ERROR_NONE** is returned. Otherwise, this
    function can return:

    * **BCL_ERROR_INVALID_NUM**
    * **BCL_ERROR_INVALID_CONTEXT**

    Note that if **bcl_rand_seed2num(**_void_**)** or
    **bcl_rand_seed2num_err(BclNumber)** are called right after this function,
    they are not guaranteed to return a number equal to *n*.

**BclError bcl_rand_seed(unsigned char** _seed_**[**_BCL_SEED_SIZE_**])**

:   Seeds the PRNG with the bytes in *seed*.

    If there was no error, **BCL_ERROR_NONE** is returned. Otherwise, this
    function can return:

    * **BCL_ERROR_INVALID_CONTEXT**

**void bcl_rand_reseed(**_void_**)**

:   Reseeds the PRNG with the default reseeding behavior. First, it attempts to
    read data from **/dev/urandom** and falls back to **libc**'s **rand()**.

    This procedure cannot fail.

**BclNumber bcl_rand_seed2num(**_void_**)**

:   Returns the current seed of the PRNG as a **BclNumber**.

    This procedure requires a valid current context.

    bcl(3) will encode an error in the return value, if there was one. The error
    can be queried with **bcl_err(BclNumber)**. Possible errors include:

    * **BCL_ERROR_INVALID_CONTEXT**
    * **BCL_ERROR_FATAL_ALLOC_ERR**

**BclRandInt bcl_rand_int(**_void_**)**

:   Returns a random integer between **0** and **BC_RAND_MAX** (inclusive).

    This procedure cannot fail.

**BclRandInt bcl_rand_bounded(BclRandInt** _bound_**)**

:   Returns a random integer between **0** and *bound* (exclusive). Bias is
    removed before returning the integer.

    This procedure cannot fail.

## Consumption and Propagation

Some functions are listed as consuming some or all of their arguments. This
means that the arguments are freed, regardless of if there were errors or not.

This is to enable compact code like the following:

    BclNumber n = bcl_num_add(bcl_num_mul(a, b), bcl_num_div(c, d));

If arguments to those functions were not consumed, memory would be leaked until
reclaimed with **bcl_ctxt_freeNums(BclContext)**.

When errors occur, they are propagated through. The result should always be
checked with **bcl_err(BclNumber)**, so the example above should properly
be:

    BclNumber n = bcl_num_add(bcl_num_mul(a, b), bcl_num_div(c, d));
    if (bcl_err(n) != BCL_ERROR_NONE) {
        // Handle the error.
    }

# ERRORS

Most functions in bcl(3) return, directly or indirectly, any one of the error
codes defined in **BclError**. The complete list of codes is the following:

**BCL_ERROR_NONE**

:   Success; no error occurred.

**BCL_ERROR_INVALID_NUM**

:   An invalid **BclNumber** was given as a parameter.

**BCL_ERROR_INVALID_CONTEXT**

:   An invalid **BclContext** is being used.

**BCL_ERROR_MATH_NEGATIVE**

:   A negative number was given as an argument to a parameter that cannot accept
    negative numbers, such as for square roots.

**BCL_ERROR_MATH_NON_INTEGER**

:   A non-integer was given as an argument to a parameter that cannot accept
    non-integer numbers, such as for the second parameter of **bcl_num_pow()**.

**BCL_ERROR_MATH_OVERFLOW**

:   A number that would overflow its result was given as an argument, such as
    for converting a **BclNumber** to a **BclBigDig**.

**BCL_ERROR_MATH_DIVIDE_BY_ZERO**

:   A divide by zero occurred.

**BCL_ERROR_PARSE_INVALID_STR**

:   An invalid number string was passed to a parsing function.

    A valid number string can only be one radix (period). In addition, any
    lowercase ASCII letters, symbols, or non-ASCII characters are invalid. It is
    allowed for the first character to be a dash. In that case, the number is
    considered to be negative.

    There is one exception to the above: one lowercase **e** is allowed in the
    number, after the radix, if it exists. If the letter **e** exists, the
    number is considered to be in scientific notation, where the part before the
    **e** is the number, and the part after, which must be an integer, is the
    exponent. There can be a dash right after the **e** to indicate a negative
    exponent.

    **WARNING**: Both the number and the exponent in scientific notation are
    interpreted according to the current **ibase**, but the number is still
    multiplied by **10\^exponent** regardless of the current **ibase**. For
    example, if **ibase** is **16** and bcl(3) is given the number string
    **FFeA**, the resulting decimal number will be **2550000000000**, and if
    bcl(3) is given the number string **10e-4**, the resulting decimal number
    will be **0.0016**.

**BCL_ERROR_FATAL_ALLOC_ERR**

:   bcl(3) failed to allocate memory.

    If clients call **bcl_setAbortOnFatalError()** with an **true** argument,
    this error will cause bcl(3) to throw a **SIGABRT**. This behavior can also
    be turned off later by calling that same function with a **false** argument.
    By default, this behavior is off.

    It is highly recommended that client libraries do *not* activate this
    behavior.

**BCL_ERROR_FATAL_UNKNOWN_ERR**

:   An unknown error occurred.

    If clients call **bcl_setAbortOnFatalError()** with an **true** argument,
    this error will cause bcl(3) to throw a **SIGABRT**. This behavior can also
    be turned off later by calling that same function with a **false** argument.
    By default, this behavior is off.

    It is highly recommended that client libraries do *not* activate this
    behavior.

# ATTRIBUTES

bcl(3) is *MT-Safe*: it is safe to call any functions from more than one thread.
However, is is *not* safe to pass any data between threads except for strings
returned by **bcl_string()**.

bcl(3) is not *async-signal-safe*. It was not possible to make bcl(3) safe with
signals and also make it safe with multiple threads. If it is necessary to be
able to interrupt bcl(3), spawn a separate thread to run the calculation.

# PERFORMANCE

Most bc(1) implementations use **char** types to calculate the value of **1**
decimal digit at a time, but that can be slow. bcl(3) does something
different.

It uses large integers to calculate more than **1** decimal digit at a time. If
built in a environment where **BC_LONG_BIT** (see the **LIMITS** section) is
**64**, then each integer has **9** decimal digits. If built in an environment
where **BC_LONG_BIT** is **32** then each integer has **4** decimal digits. This
value (the number of decimal digits per large integer) is called
**BC_BASE_DIGS**.

In addition, this bcl(3) uses an even larger integer for overflow checking. This
integer type depends on the value of **BC_LONG_BIT**, but is always at least
twice as large as the integer type used to store digits.

# LIMITS

The following are the limits on bcl(3):

**BC_LONG_BIT**

:   The number of bits in the **long** type in the environment where bcl(3) was
    built. This determines how many decimal digits can be stored in a single
    large integer (see the **PERFORMANCE** section).

**BC_BASE_DIGS**

:   The number of decimal digits per large integer (see the **PERFORMANCE**
    section). Depends on **BC_LONG_BIT**.

**BC_BASE_POW**

:   The max decimal number that each large integer can store (see
    **BC_BASE_DIGS**) plus **1**. Depends on **BC_BASE_DIGS**.

**BC_OVERFLOW_MAX**

:   The max number that the overflow type (see the **PERFORMANCE** section) can
    hold. Depends on **BC_LONG_BIT**.

**BC_BASE_MAX**

:   The maximum output base. Set at **BC_BASE_POW**.

**BC_SCALE_MAX**

:   The maximum **scale**. Set at **BC_OVERFLOW_MAX-1**.

**BC_NUM_MAX**

:   The maximum length of a number (in decimal digits), which includes digits
    after the decimal point. Set at **BC_OVERFLOW_MAX-1**.

**BC_RAND_MAX**

:   The maximum integer (inclusive) returned by the **bcl_rand_int()** function.
    Set at **2\^BC_LONG_BIT-1**.

Exponent

:   The maximum allowable exponent (positive or negative). Set at
    **BC_OVERFLOW_MAX**.

These limits are meant to be effectively non-existent; the limits are so large
(at least on 64-bit machines) that there should not be any point at which they
become a problem. In fact, memory should be exhausted before these limits should
be hit.

# SEE ALSO

bc(1) and dc(1)

# STANDARDS

bcl(3) is compliant with the arithmetic defined in the IEEE Std 1003.1-2017
(“POSIX.1-2017”) specification at
https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html for bc(1).

Note that the specification explicitly says that bc(1) only accepts numbers that
use a period (**.**) as a radix point, regardless of the value of
**LC_NUMERIC**. This is also true of bcl(3).

# BUGS

None are known. Report bugs at https://git.gavinhoward.com/gavin/bc.

# AUTHORS

Gavin D. Howard <gavin@gavinhoward.com> and contributors.