xref: /freebsd/contrib/bc/manuals/bcl.3.md (revision a970610a3af63b3f4df5b69d91c6b4093a00ed8f)
150696a6eSStefan Eßer<!---
250696a6eSStefan Eßer
350696a6eSStefan EßerSPDX-License-Identifier: BSD-2-Clause
450696a6eSStefan Eßer
5*a970610aSStefan EßerCopyright (c) 2018-2024 Gavin D. Howard and contributors.
650696a6eSStefan Eßer
750696a6eSStefan EßerRedistribution and use in source and binary forms, with or without
850696a6eSStefan Eßermodification, are permitted provided that the following conditions are met:
950696a6eSStefan Eßer
1050696a6eSStefan Eßer* Redistributions of source code must retain the above copyright notice, this
1150696a6eSStefan Eßer  list of conditions and the following disclaimer.
1250696a6eSStefan Eßer
1350696a6eSStefan Eßer* Redistributions in binary form must reproduce the above copyright notice,
1450696a6eSStefan Eßer  this list of conditions and the following disclaimer in the documentation
1550696a6eSStefan Eßer  and/or other materials provided with the distribution.
1650696a6eSStefan Eßer
1750696a6eSStefan EßerTHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
1850696a6eSStefan EßerAND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1950696a6eSStefan EßerIMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2050696a6eSStefan EßerARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
2150696a6eSStefan EßerLIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2250696a6eSStefan EßerCONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2350696a6eSStefan EßerSUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2450696a6eSStefan EßerINTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2550696a6eSStefan EßerCONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2650696a6eSStefan EßerARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2750696a6eSStefan EßerPOSSIBILITY OF SUCH DAMAGE.
2850696a6eSStefan Eßer
2950696a6eSStefan Eßer-->
3050696a6eSStefan Eßer
3150696a6eSStefan Eßer# NAME
3250696a6eSStefan Eßer
3350696a6eSStefan Eßerbcl - library of arbitrary precision decimal arithmetic
3450696a6eSStefan Eßer
3550696a6eSStefan Eßer# SYNOPSIS
3650696a6eSStefan Eßer
3750696a6eSStefan Eßer## Use
3850696a6eSStefan Eßer
3950696a6eSStefan Eßer*#include <bcl.h>*
4050696a6eSStefan Eßer
41d101cdd6SStefan EßerLink with *-lbcl*, and on POSIX systems, *-lpthread* is also required.
4250696a6eSStefan Eßer
4350696a6eSStefan Eßer## Setup
4450696a6eSStefan Eßer
4550696a6eSStefan EßerThese items allow clients to set up bcl(3).
4650696a6eSStefan Eßer
47d101cdd6SStefan Eßer**BclError bcl_start(**_void_**);**
48d101cdd6SStefan Eßer
49d101cdd6SStefan Eßer**void bcl_end(**_void_**);**
50d101cdd6SStefan Eßer
5144d4804dSStefan Eßer**BclError bcl_init(**_void_**);**
5250696a6eSStefan Eßer
5344d4804dSStefan Eßer**void bcl_free(**_void_**);**
5450696a6eSStefan Eßer
5544d4804dSStefan Eßer**bool bcl_abortOnFatalError(**_void_**);**
5650696a6eSStefan Eßer
5744d4804dSStefan Eßer**void bcl_setAbortOnFatalError(bool** _abrt_**);**
5850696a6eSStefan Eßer
59d43fa8efSStefan Eßer**bool bcl_leadingZeroes(**_void_**);**
60d43fa8efSStefan Eßer
61d43fa8efSStefan Eßer**void bcl_setLeadingZeroes(bool** _leadingZeroes_**);**
62d43fa8efSStefan Eßer
6344d4804dSStefan Eßer**void bcl_gc(**_void_**);**
6450696a6eSStefan Eßer
65d101cdd6SStefan Eßer**bool bcl_digitClamp(**_void_**);**
66d101cdd6SStefan Eßer
67d101cdd6SStefan Eßer**void bcl_setDigitClamp(bool** _digitClamp_**);**
68d101cdd6SStefan Eßer
6950696a6eSStefan Eßer## Contexts
7050696a6eSStefan Eßer
7150696a6eSStefan EßerThese items will allow clients to handle contexts, which are isolated from each
7250696a6eSStefan Eßerother. This allows more than one client to use bcl(3) in the same program.
7350696a6eSStefan Eßer
7450696a6eSStefan Eßer**struct BclCtxt;**
7550696a6eSStefan Eßer
7650696a6eSStefan Eßer**typedef struct BclCtxt\* BclContext;**
7750696a6eSStefan Eßer
7844d4804dSStefan Eßer**BclContext bcl_ctxt_create(**_void_**);**
7950696a6eSStefan Eßer
8044d4804dSStefan Eßer**void bcl_ctxt_free(BclContext** _ctxt_**);**
8150696a6eSStefan Eßer
8244d4804dSStefan Eßer**BclError bcl_pushContext(BclContext** _ctxt_**);**
8350696a6eSStefan Eßer
8444d4804dSStefan Eßer**void bcl_popContext(**_void_**);**
8550696a6eSStefan Eßer
8644d4804dSStefan Eßer**BclContext bcl_context(**_void_**);**
8750696a6eSStefan Eßer
8844d4804dSStefan Eßer**void bcl_ctxt_freeNums(BclContext** _ctxt_**);**
8950696a6eSStefan Eßer
9044d4804dSStefan Eßer**size_t bcl_ctxt_scale(BclContext** _ctxt_**);**
9150696a6eSStefan Eßer
9244d4804dSStefan Eßer**void bcl_ctxt_setScale(BclContext** _ctxt_**, size_t** _scale_**);**
9350696a6eSStefan Eßer
9444d4804dSStefan Eßer**size_t bcl_ctxt_ibase(BclContext** _ctxt_**);**
9550696a6eSStefan Eßer
9644d4804dSStefan Eßer**void bcl_ctxt_setIbase(BclContext** _ctxt_**, size_t** _ibase_**);**
9750696a6eSStefan Eßer
9844d4804dSStefan Eßer**size_t bcl_ctxt_obase(BclContext** _ctxt_**);**
9950696a6eSStefan Eßer
10044d4804dSStefan Eßer**void bcl_ctxt_setObase(BclContext** _ctxt_**, size_t** _obase_**);**
10150696a6eSStefan Eßer
10250696a6eSStefan Eßer## Errors
10350696a6eSStefan Eßer
10450696a6eSStefan EßerThese items allow clients to handle errors.
10550696a6eSStefan Eßer
10650696a6eSStefan Eßer**typedef enum BclError BclError;**
10750696a6eSStefan Eßer
10844d4804dSStefan Eßer**BclError bcl_err(BclNumber** _n_**);**
10950696a6eSStefan Eßer
11050696a6eSStefan Eßer## Numbers
11150696a6eSStefan Eßer
11250696a6eSStefan EßerThese items allow clients to manipulate and query the arbitrary-precision
11350696a6eSStefan Eßernumbers managed by bcl(3).
11450696a6eSStefan Eßer
11550696a6eSStefan Eßer**typedef struct { size_t i; } BclNumber;**
11650696a6eSStefan Eßer
11744d4804dSStefan Eßer**BclNumber bcl_num_create(**_void_**);**
11850696a6eSStefan Eßer
11944d4804dSStefan Eßer**void bcl_num_free(BclNumber** _n_**);**
12050696a6eSStefan Eßer
12144d4804dSStefan Eßer**bool bcl_num_neg(BclNumber** _n_**);**
12250696a6eSStefan Eßer
12344d4804dSStefan Eßer**void bcl_num_setNeg(BclNumber** _n_**, bool** _neg_**);**
12450696a6eSStefan Eßer
12544d4804dSStefan Eßer**size_t bcl_num_scale(BclNumber** _n_**);**
12650696a6eSStefan Eßer
12744d4804dSStefan Eßer**BclError bcl_num_setScale(BclNumber** _n_**, size_t** _scale_**);**
12850696a6eSStefan Eßer
12944d4804dSStefan Eßer**size_t bcl_num_len(BclNumber** _n_**);**
13050696a6eSStefan Eßer
13150696a6eSStefan Eßer## Conversion
13250696a6eSStefan Eßer
13350696a6eSStefan EßerThese items allow clients to convert numbers into and from strings and integers.
13450696a6eSStefan Eßer
13544d4804dSStefan Eßer**BclNumber bcl_parse(const char \*restrict** _val_**);**
13650696a6eSStefan Eßer
13744d4804dSStefan Eßer**char\* bcl_string(BclNumber** _n_**);**
13850696a6eSStefan Eßer
139175a4d10SStefan Eßer**char\* bcl_string_keep(BclNumber** _n_**);**
140175a4d10SStefan Eßer
14144d4804dSStefan Eßer**BclError bcl_bigdig(BclNumber** _n_**, BclBigDig \***_result_**);**
14250696a6eSStefan Eßer
143175a4d10SStefan Eßer**BclError bcl_bigdig_keep(BclNumber** _n_**, BclBigDig \***_result_**);**
144175a4d10SStefan Eßer
14544d4804dSStefan Eßer**BclNumber bcl_bigdig2num(BclBigDig** _val_**);**
14650696a6eSStefan Eßer
14750696a6eSStefan Eßer## Math
14850696a6eSStefan Eßer
14950696a6eSStefan EßerThese items allow clients to run math on numbers.
15050696a6eSStefan Eßer
15144d4804dSStefan Eßer**BclNumber bcl_add(BclNumber** _a_**, BclNumber** _b_**);**
15250696a6eSStefan Eßer
153175a4d10SStefan Eßer**BclNumber bcl_add_keep(BclNumber** _a_**, BclNumber** _b_**);**
154175a4d10SStefan Eßer
15544d4804dSStefan Eßer**BclNumber bcl_sub(BclNumber** _a_**, BclNumber** _b_**);**
15650696a6eSStefan Eßer
157175a4d10SStefan Eßer**BclNumber bcl_sub_keep(BclNumber** _a_**, BclNumber** _b_**);**
158175a4d10SStefan Eßer
15944d4804dSStefan Eßer**BclNumber bcl_mul(BclNumber** _a_**, BclNumber** _b_**);**
16050696a6eSStefan Eßer
161175a4d10SStefan Eßer**BclNumber bcl_mul_keep(BclNumber** _a_**, BclNumber** _b_**);**
162175a4d10SStefan Eßer
16344d4804dSStefan Eßer**BclNumber bcl_div(BclNumber** _a_**, BclNumber** _b_**);**
16450696a6eSStefan Eßer
165175a4d10SStefan Eßer**BclNumber bcl_div_keep(BclNumber** _a_**, BclNumber** _b_**);**
166175a4d10SStefan Eßer
16744d4804dSStefan Eßer**BclNumber bcl_mod(BclNumber** _a_**, BclNumber** _b_**);**
16850696a6eSStefan Eßer
169175a4d10SStefan Eßer**BclNumber bcl_mod_keep(BclNumber** _a_**, BclNumber** _b_**);**
170175a4d10SStefan Eßer
17144d4804dSStefan Eßer**BclNumber bcl_pow(BclNumber** _a_**, BclNumber** _b_**);**
17250696a6eSStefan Eßer
173175a4d10SStefan Eßer**BclNumber bcl_pow_keep(BclNumber** _a_**, BclNumber** _b_**);**
174175a4d10SStefan Eßer
17544d4804dSStefan Eßer**BclNumber bcl_lshift(BclNumber** _a_**, BclNumber** _b_**);**
17650696a6eSStefan Eßer
177175a4d10SStefan Eßer**BclNumber bcl_lshift_keep(BclNumber** _a_**, BclNumber** _b_**);**
178175a4d10SStefan Eßer
17944d4804dSStefan Eßer**BclNumber bcl_rshift(BclNumber** _a_**, BclNumber** _b_**);**
18050696a6eSStefan Eßer
181175a4d10SStefan Eßer**BclNumber bcl_rshift_keep(BclNumber** _a_**, BclNumber** _b_**);**
182175a4d10SStefan Eßer
18344d4804dSStefan Eßer**BclNumber bcl_sqrt(BclNumber** _a_**);**
18450696a6eSStefan Eßer
185175a4d10SStefan Eßer**BclNumber bcl_sqrt_keep(BclNumber** _a_**);**
186175a4d10SStefan Eßer
18744d4804dSStefan Eßer**BclError bcl_divmod(BclNumber** _a_**, BclNumber** _b_**, BclNumber \***_c_**, BclNumber \***_d_**);**
18850696a6eSStefan Eßer
189175a4d10SStefan Eßer**BclError bcl_divmod_keep(BclNumber** _a_**, BclNumber** _b_**, BclNumber \***_c_**, BclNumber \***_d_**);**
190175a4d10SStefan Eßer
19144d4804dSStefan Eßer**BclNumber bcl_modexp(BclNumber** _a_**, BclNumber** _b_**, BclNumber** _c_**);**
19250696a6eSStefan Eßer
193175a4d10SStefan Eßer**BclNumber bcl_modexp_keep(BclNumber** _a_**, BclNumber** _b_**, BclNumber** _c_**);**
194175a4d10SStefan Eßer
19550696a6eSStefan Eßer## Miscellaneous
19650696a6eSStefan Eßer
19750696a6eSStefan EßerThese items are miscellaneous.
19850696a6eSStefan Eßer
19944d4804dSStefan Eßer**void bcl_zero(BclNumber** _n_**);**
20050696a6eSStefan Eßer
20144d4804dSStefan Eßer**void bcl_one(BclNumber** _n_**);**
20250696a6eSStefan Eßer
20344d4804dSStefan Eßer**ssize_t bcl_cmp(BclNumber** _a_**, BclNumber** _b_**);**
20450696a6eSStefan Eßer
20544d4804dSStefan Eßer**BclError bcl_copy(BclNumber** _d_**, BclNumber** _s_**);**
20650696a6eSStefan Eßer
20744d4804dSStefan Eßer**BclNumber bcl_dup(BclNumber** _s_**);**
20850696a6eSStefan Eßer
20950696a6eSStefan Eßer## Pseudo-Random Number Generator
21050696a6eSStefan Eßer
21150696a6eSStefan EßerThese items allow clients to manipulate the seeded pseudo-random number
21250696a6eSStefan Eßergenerator in bcl(3).
21350696a6eSStefan Eßer
21450696a6eSStefan Eßer**#define BCL_SEED_ULONGS**
21550696a6eSStefan Eßer
21650696a6eSStefan Eßer**#define BCL_SEED_SIZE**
21750696a6eSStefan Eßer
21850696a6eSStefan Eßer**typedef unsigned long BclBigDig;**
21950696a6eSStefan Eßer
22050696a6eSStefan Eßer**typedef unsigned long BclRandInt;**
22150696a6eSStefan Eßer
22244d4804dSStefan Eßer**BclNumber bcl_irand(BclNumber** _a_**);**
22350696a6eSStefan Eßer
224175a4d10SStefan Eßer**BclNumber bcl_irand_keep(BclNumber** _a_**);**
225175a4d10SStefan Eßer
22644d4804dSStefan Eßer**BclNumber bcl_frand(size_t** _places_**);**
22750696a6eSStefan Eßer
22844d4804dSStefan Eßer**BclNumber bcl_ifrand(BclNumber** _a_**, size_t** _places_**);**
22950696a6eSStefan Eßer
230175a4d10SStefan Eßer**BclNumber bcl_ifrand_keep(BclNumber** _a_**, size_t** _places_**);**
231175a4d10SStefan Eßer
23244d4804dSStefan Eßer**BclError bcl_rand_seedWithNum(BclNumber** _n_**);**
23350696a6eSStefan Eßer
234175a4d10SStefan Eßer**BclError bcl_rand_seedWithNum_keep(BclNumber** _n_**);**
235175a4d10SStefan Eßer
23644d4804dSStefan Eßer**BclError bcl_rand_seed(unsigned char** _seed_**[**_BCL_SEED_SIZE_**]);**
23750696a6eSStefan Eßer
23844d4804dSStefan Eßer**void bcl_rand_reseed(**_void_**);**
23950696a6eSStefan Eßer
24044d4804dSStefan Eßer**BclNumber bcl_rand_seed2num(**_void_**);**
24150696a6eSStefan Eßer
24244d4804dSStefan Eßer**BclRandInt bcl_rand_int(**_void_**);**
24350696a6eSStefan Eßer
24444d4804dSStefan Eßer**BclRandInt bcl_rand_bounded(BclRandInt** _bound_**);**
24550696a6eSStefan Eßer
24650696a6eSStefan Eßer# DESCRIPTION
24750696a6eSStefan Eßer
24850696a6eSStefan Eßerbcl(3) is a library that implements arbitrary-precision decimal math, as
24978bc019dSStefan Eßerstandardized by POSIX
25078bc019dSStefan Eßer(https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html) in bc(1).
25150696a6eSStefan Eßer
252d43fa8efSStefan Eßerbcl(3) assumes that it is allowed to use the **bcl**, **Bcl**, **bc**, and
253d43fa8efSStefan Eßer**Bc** prefixes for symbol names without collision.
25444d4804dSStefan Eßer
25550696a6eSStefan EßerAll of the items in its interface are described below. See the documentation for
25650696a6eSStefan Eßereach function for what each function can return.
25750696a6eSStefan Eßer
25850696a6eSStefan Eßer## Setup
25950696a6eSStefan Eßer
260d101cdd6SStefan Eßer**BclError bcl_start(**_void_**)**
26150696a6eSStefan Eßer
26250696a6eSStefan Eßer:   Initializes this library. This function can be called multiple times, but
263d101cdd6SStefan Eßer    **bcl_end()** must only be called *once*. This is to make it possible for
264d101cdd6SStefan Eßer    multiple libraries and applications to initialize bcl(3) without problem.
265d101cdd6SStefan Eßer
266d101cdd6SStefan Eßer    It is suggested that client libraries call this function, but do not call
267d101cdd6SStefan Eßer    **bcl_end()**, and client applications should call both.
26850696a6eSStefan Eßer
26950696a6eSStefan Eßer    If there was no error, **BCL_ERROR_NONE** is returned. Otherwise, this
27050696a6eSStefan Eßer    function can return:
27150696a6eSStefan Eßer
27250696a6eSStefan Eßer    * **BCL_ERROR_FATAL_ALLOC_ERR**
27350696a6eSStefan Eßer
27450696a6eSStefan Eßer    This function must be the first one clients call. Calling any other
27550696a6eSStefan Eßer    function without calling this one first is undefined behavior.
27650696a6eSStefan Eßer
277d101cdd6SStefan Eßer**void bcl_end(**_void_**)**
278d101cdd6SStefan Eßer
279d101cdd6SStefan Eßer:   Deinitializes this library. This function must only be called *once*.
280d101cdd6SStefan Eßer
281d101cdd6SStefan Eßer    All data must have been freed before calling this function.
282d101cdd6SStefan Eßer
283d101cdd6SStefan Eßer    This function must be the last one clients call. Calling this function
284d101cdd6SStefan Eßer    before calling any other function is undefined behavior.
285d101cdd6SStefan Eßer
286d101cdd6SStefan Eßer**BclError bcl_init(**_void_**)**
287d101cdd6SStefan Eßer
288d101cdd6SStefan Eßer:   Initializes the library for the current thread. This function can be called
289d101cdd6SStefan Eßer    multiple times, but each call must be matched by a call to
290d101cdd6SStefan Eßer    **bcl_free(**_void_**)**. This is to make it possible for multiple libraries
291d101cdd6SStefan Eßer    and applications to initialize threads for bcl(3) without problem.
292d101cdd6SStefan Eßer
293d101cdd6SStefan Eßer    This function *must* be called from the thread that it is supposed to
294d101cdd6SStefan Eßer    initialize.
295d101cdd6SStefan Eßer
296d101cdd6SStefan Eßer    If there was no error, **BCL_ERROR_NONE** is returned. Otherwise, this
297d101cdd6SStefan Eßer    function can return:
298d101cdd6SStefan Eßer
299d101cdd6SStefan Eßer    * **BCL_ERROR_FATAL_ALLOC_ERR**
300d101cdd6SStefan Eßer
301d101cdd6SStefan Eßer    This function must be the second one clients call. Calling any other
302d101cdd6SStefan Eßer    function without calling **bcl_start()** and then this one first is
303d101cdd6SStefan Eßer    undefined behavior, except in the case of new threads. New threads can
304d101cdd6SStefan Eßer    safely call this function without calling **bcl_start()** if another thread
305d101cdd6SStefan Eßer    has previously called **bcl_start()**. But this function must still be the
306d101cdd6SStefan Eßer    first function in bcl(3) called by that new thread.
307d101cdd6SStefan Eßer
30844d4804dSStefan Eßer**void bcl_free(**_void_**)**
30950696a6eSStefan Eßer
31050696a6eSStefan Eßer:   Decrements bcl(3)'s reference count and frees the data associated with it if
31150696a6eSStefan Eßer    the reference count is **0**.
31250696a6eSStefan Eßer
313d101cdd6SStefan Eßer    This function *must* be called from the thread that it is supposed to
314d101cdd6SStefan Eßer    deinitialize.
315d101cdd6SStefan Eßer
316d101cdd6SStefan Eßer    This function must be the second to last one clients call. Calling this
317d101cdd6SStefan Eßer    function before calling any other function besides **bcl_end()** is
318d101cdd6SStefan Eßer    undefined behavior.
31950696a6eSStefan Eßer
32044d4804dSStefan Eßer**bool bcl_abortOnFatalError(**_void_**)**
32150696a6eSStefan Eßer
32250696a6eSStefan Eßer:   Queries and returns the current state of calling **abort()** on fatal
32350696a6eSStefan Eßer    errors. If **true** is returned, bcl(3) will cause a **SIGABRT** if a fatal
32450696a6eSStefan Eßer    error occurs.
32550696a6eSStefan Eßer
32650696a6eSStefan Eßer    If activated, clients do not need to check for fatal errors.
32750696a6eSStefan Eßer
328d101cdd6SStefan Eßer    This value is *thread-local*; it applies to just the thread it is read on.
329d101cdd6SStefan Eßer
330d43fa8efSStefan Eßer    The default is **false**.
331d43fa8efSStefan Eßer
33244d4804dSStefan Eßer**void bcl_setAbortOnFatalError(bool** _abrt_**)**
33350696a6eSStefan Eßer
33450696a6eSStefan Eßer:   Sets the state of calling **abort()** on fatal errors. If *abrt* is
33550696a6eSStefan Eßer    **false**, bcl(3) will not cause a **SIGABRT** on fatal errors after the
33650696a6eSStefan Eßer    call. If *abrt* is **true**, bcl(3) will cause a **SIGABRT** on fatal errors
33750696a6eSStefan Eßer    after the call.
33850696a6eSStefan Eßer
339d101cdd6SStefan Eßer    This value is *thread-local*; it applies to just the thread it is set on.
340d101cdd6SStefan Eßer
34150696a6eSStefan Eßer    If activated, clients do not need to check for fatal errors.
34250696a6eSStefan Eßer
343d43fa8efSStefan Eßer**bool bcl_leadingZeroes(**_void_**)**
344d43fa8efSStefan Eßer
345d43fa8efSStefan Eßer:   Queries and returns the state of whether leading zeroes are added to strings
346d43fa8efSStefan Eßer    returned by **bcl_string()** when numbers are greater than **-1**, less than
347d43fa8efSStefan Eßer    **1**, and not equal to **0**. If **true** is returned, then leading zeroes
348d43fa8efSStefan Eßer    will be added.
349d43fa8efSStefan Eßer
350d101cdd6SStefan Eßer    This value is *thread-local*; it applies to just the thread it is read on.
351d101cdd6SStefan Eßer
352d43fa8efSStefan Eßer    The default is **false**.
353d43fa8efSStefan Eßer
354d43fa8efSStefan Eßer**void bcl_setLeadingZeroes(bool** _leadingZeroes_**)**
355d43fa8efSStefan Eßer
356d43fa8efSStefan Eßer:   Sets the state of whether leading zeroes are added to strings returned by
357d43fa8efSStefan Eßer    **bcl_string()** when numbers are greater than **-1**, less than **1**, and
358d43fa8efSStefan Eßer    not equal to **0**. If *leadingZeroes* is **true**, leading zeroes will be
359d43fa8efSStefan Eßer    added to strings returned by **bcl_string()**.
360d43fa8efSStefan Eßer
361d101cdd6SStefan Eßer    This value is *thread-local*; it applies to just the thread it is set on.
362d101cdd6SStefan Eßer
363d101cdd6SStefan Eßer**bool bcl_digitClamp(**_void_**)**
364d101cdd6SStefan Eßer
365d101cdd6SStefan Eßer:   Queries and returns the state of whether digits in number strings that are
366d101cdd6SStefan Eßer    greater than or equal to the current **ibase** are clamped or not.
367d101cdd6SStefan Eßer
368d101cdd6SStefan Eßer    If **true** is returned, then digits are treated as though they are equal to
369d101cdd6SStefan Eßer    the value of **ibase** minus **1**. If this is *not* true, then digits are
370d101cdd6SStefan Eßer    treated as though they are equal to the value they would have if **ibase**
371d101cdd6SStefan Eßer    was large enough. They are then multiplied by the appropriate power of
372d101cdd6SStefan Eßer    **ibase**.
373d101cdd6SStefan Eßer
374d101cdd6SStefan Eßer    For example, with clamping off and an **ibase** of **3**, the string "AB"
375d101cdd6SStefan Eßer    would equal **3\^1\*A+3\^0\*B**, which is **3** times **10** plus **11**, or
376d101cdd6SStefan Eßer    **41**, while with clamping on and an **ibase** of **3**, the string "AB"
377d101cdd6SStefan Eßer    would be equal to **3\^1\*2+3\^0\*2**, which is **3** times **2** plus
378d101cdd6SStefan Eßer    **2**, or **8**.
379d101cdd6SStefan Eßer
380d101cdd6SStefan Eßer    This value is *thread-local*; it applies to just the thread it is read on.
381d101cdd6SStefan Eßer
382d101cdd6SStefan Eßer    The default is **true**.
383d101cdd6SStefan Eßer
384d101cdd6SStefan Eßer**void bcl_setDigitClamp(bool** _digitClamp_**)**
385d101cdd6SStefan Eßer
386d101cdd6SStefan Eßer:   Sets the state of whether digits in number strings that are greater than or
387d101cdd6SStefan Eßer    equal to the current **ibase** are clamped or not. For more information, see
388d101cdd6SStefan Eßer    the **bcl_digitClamp(**_void_**)** function.
389d101cdd6SStefan Eßer
390d101cdd6SStefan Eßer    This value is *thread-local*; it applies to just the thread it is set on.
391d101cdd6SStefan Eßer
39244d4804dSStefan Eßer**void bcl_gc(**_void_**)**
39350696a6eSStefan Eßer
39450696a6eSStefan Eßer:   Garbage collects cached instances of arbitrary-precision numbers. This only
39550696a6eSStefan Eßer    frees the memory of numbers that are *not* in use, so it is safe to call at
39650696a6eSStefan Eßer    any time.
39750696a6eSStefan Eßer
39850696a6eSStefan Eßer## Contexts
39950696a6eSStefan Eßer
40050696a6eSStefan EßerAll procedures that take a **BclContext** parameter a require a valid context as
40150696a6eSStefan Eßeran argument.
40250696a6eSStefan Eßer
40350696a6eSStefan Eßer**struct BclCtxt**
40450696a6eSStefan Eßer
40550696a6eSStefan Eßer:   A forward declaration for a hidden **struct** type. Clients cannot access
40650696a6eSStefan Eßer    the internals of the **struct** type directly. All interactions with the
40750696a6eSStefan Eßer    type are done through pointers. See **BclContext** below.
40850696a6eSStefan Eßer
40950696a6eSStefan Eßer**BclContext**
41050696a6eSStefan Eßer
41150696a6eSStefan Eßer:   A typedef to a pointer of **struct BclCtxt**. This is the only handle
41250696a6eSStefan Eßer    clients can get to **struct BclCtxt**.
41350696a6eSStefan Eßer
41450696a6eSStefan Eßer    A **BclContext** contains the values **scale**, **ibase**, and **obase**, as
41550696a6eSStefan Eßer    well as a list of numbers.
41650696a6eSStefan Eßer
41750696a6eSStefan Eßer    **scale** is a value used to control how many decimal places calculations
41850696a6eSStefan Eßer    should use. A value of **0** means that calculations are done on integers
41950696a6eSStefan Eßer    only, where applicable, and a value of 20, for example, means that all
42050696a6eSStefan Eßer    applicable calculations return results with 20 decimal places. The default
42150696a6eSStefan Eßer    is **0**.
42250696a6eSStefan Eßer
42350696a6eSStefan Eßer    **ibase** is a value used to control the input base. The minimum **ibase**
42450696a6eSStefan Eßer    is **2**, and the maximum is **36**. If **ibase** is **2**, numbers are
42550696a6eSStefan Eßer    parsed as though they are in binary, and any digits larger than **1** are
42650696a6eSStefan Eßer    clamped. Likewise, a value of **10** means that numbers are parsed as though
42750696a6eSStefan Eßer    they are decimal, and any larger digits are clamped. The default is **10**.
42850696a6eSStefan Eßer
42950696a6eSStefan Eßer    **obase** is a value used to control the output base. The minimum **obase**
43050696a6eSStefan Eßer    is **0** and the maximum is **BC_BASE_MAX** (see the **LIMITS** section).
43150696a6eSStefan Eßer
43250696a6eSStefan Eßer    Numbers created in one context are not valid in another context. It is
43350696a6eSStefan Eßer    undefined behavior to use a number created in a different context. Contexts
43450696a6eSStefan Eßer    are meant to isolate the numbers used by different clients in the same
43550696a6eSStefan Eßer    application.
43650696a6eSStefan Eßer
437d101cdd6SStefan Eßer    Different threads also have different contexts, so any numbers created in
438d101cdd6SStefan Eßer    one thread are not valid in another thread. To pass values between contexts
439d101cdd6SStefan Eßer    and threads, use **bcl_string()** to produce a string to pass around, and
440d101cdd6SStefan Eßer    use **bcl_parse()** to parse the string. It is suggested that the **obase**
441d101cdd6SStefan Eßer    used to create the string be passed around with the string and used as the
442d101cdd6SStefan Eßer    **ibase** for **bcl_parse()** to ensure that the number will be the same.
443d101cdd6SStefan Eßer
44444d4804dSStefan Eßer**BclContext bcl_ctxt_create(**_void_**)**
44550696a6eSStefan Eßer
44650696a6eSStefan Eßer:   Creates a context and returns it. Returns **NULL** if there was an error.
44750696a6eSStefan Eßer
44844d4804dSStefan Eßer**void bcl_ctxt_free(BclContext** _ctxt_**)**
44950696a6eSStefan Eßer
45050696a6eSStefan Eßer:   Frees *ctxt*, after which it is no longer valid. It is undefined behavior to
45150696a6eSStefan Eßer    attempt to use an invalid context.
45250696a6eSStefan Eßer
45344d4804dSStefan Eßer**BclError bcl_pushContext(BclContext** _ctxt_**)**
45450696a6eSStefan Eßer
45550696a6eSStefan Eßer:   Pushes *ctxt* onto bcl(3)'s stack of contexts. *ctxt* must have been created
45644d4804dSStefan Eßer    with **bcl_ctxt_create(**_void_**)**.
45750696a6eSStefan Eßer
45850696a6eSStefan Eßer    If there was no error, **BCL_ERROR_NONE** is returned. Otherwise, this
45950696a6eSStefan Eßer    function can return:
46050696a6eSStefan Eßer
46150696a6eSStefan Eßer    * **BCL_ERROR_FATAL_ALLOC_ERR**
46250696a6eSStefan Eßer
46350696a6eSStefan Eßer    There *must* be a valid context to do any arithmetic.
46450696a6eSStefan Eßer
46544d4804dSStefan Eßer**void bcl_popContext(**_void_**)**
46650696a6eSStefan Eßer
46750696a6eSStefan Eßer:   Pops the current context off of the stack, if one exists.
46850696a6eSStefan Eßer
46944d4804dSStefan Eßer**BclContext bcl_context(**_void_**)**
47050696a6eSStefan Eßer
47150696a6eSStefan Eßer:   Returns the current context, or **NULL** if no context exists.
47250696a6eSStefan Eßer
47344d4804dSStefan Eßer**void bcl_ctxt_freeNums(BclContext** _ctxt_**)**
47450696a6eSStefan Eßer
47550696a6eSStefan Eßer:   Frees all numbers in use that are associated with *ctxt*. It is undefined
47650696a6eSStefan Eßer    behavior to attempt to use a number associated with *ctxt* after calling
47750696a6eSStefan Eßer    this procedure unless such numbers have been created with
47844d4804dSStefan Eßer    **bcl_num_create(**_void_**)** after calling this procedure.
47950696a6eSStefan Eßer
48044d4804dSStefan Eßer**size_t bcl_ctxt_scale(BclContext** _ctxt_**)**
48150696a6eSStefan Eßer
48250696a6eSStefan Eßer:   Returns the **scale** for given context.
48350696a6eSStefan Eßer
48444d4804dSStefan Eßer**void bcl_ctxt_setScale(BclContext** _ctxt_**, size_t** _scale_**)**
48550696a6eSStefan Eßer
48650696a6eSStefan Eßer:   Sets the **scale** for the given context to the argument *scale*.
48750696a6eSStefan Eßer
48844d4804dSStefan Eßer**size_t bcl_ctxt_ibase(BclContext** _ctxt_**)**
48950696a6eSStefan Eßer
49050696a6eSStefan Eßer:   Returns the **ibase** for the given context.
49150696a6eSStefan Eßer
49244d4804dSStefan Eßer**void bcl_ctxt_setIbase(BclContext** _ctxt_**, size_t** _ibase_**)**
49350696a6eSStefan Eßer
49450696a6eSStefan Eßer:   Sets the **ibase** for the given context to the argument *ibase*. If the
49550696a6eSStefan Eßer    argument *ibase* is invalid, it clamped, so an *ibase* of **0** or **1** is
49650696a6eSStefan Eßer    clamped to **2**, and any values above **36** are clamped to **36**.
49750696a6eSStefan Eßer
49844d4804dSStefan Eßer**size_t bcl_ctxt_obase(BclContext** _ctxt_**)**
49950696a6eSStefan Eßer
50050696a6eSStefan Eßer:   Returns the **obase** for the given context.
50150696a6eSStefan Eßer
50244d4804dSStefan Eßer**void bcl_ctxt_setObase(BclContext** _ctxt_**, size_t** _obase_**)**
50350696a6eSStefan Eßer
50450696a6eSStefan Eßer:   Sets the **obase** for the given context to the argument *obase*.
50550696a6eSStefan Eßer
50650696a6eSStefan Eßer## Errors
50750696a6eSStefan Eßer
50850696a6eSStefan Eßer**BclError**
50950696a6eSStefan Eßer
51050696a6eSStefan Eßer:   An **enum** of possible error codes. See the **ERRORS** section for a
51150696a6eSStefan Eßer    complete listing the codes.
51250696a6eSStefan Eßer
51344d4804dSStefan Eßer**BclError bcl_err(BclNumber** _n_**)**
51450696a6eSStefan Eßer
51550696a6eSStefan Eßer:   Checks for errors in a **BclNumber**. All functions that can return a
51650696a6eSStefan Eßer    **BclNumber** can encode an error in the number, and this function will
51750696a6eSStefan Eßer    return the error, if any. If there was no error, it will return
51850696a6eSStefan Eßer    **BCL_ERROR_NONE**.
51950696a6eSStefan Eßer
52050696a6eSStefan Eßer    There must be a valid current context.
52150696a6eSStefan Eßer
52250696a6eSStefan Eßer## Numbers
52350696a6eSStefan Eßer
52450696a6eSStefan EßerAll procedures in this section require a valid current context.
52550696a6eSStefan Eßer
52650696a6eSStefan Eßer**BclNumber**
52750696a6eSStefan Eßer
52850696a6eSStefan Eßer:   A handle to an arbitrary-precision number. The actual number type is not
52950696a6eSStefan Eßer    exposed; the **BclNumber** handle is the only way clients can refer to
53050696a6eSStefan Eßer    instances of arbitrary-precision numbers.
53150696a6eSStefan Eßer
53244d4804dSStefan Eßer**BclNumber bcl_num_create(**_void_**)**
53350696a6eSStefan Eßer
53450696a6eSStefan Eßer:   Creates and returns a **BclNumber**.
53550696a6eSStefan Eßer
53650696a6eSStefan Eßer    bcl(3) will encode an error in the return value, if there was one. The error
53750696a6eSStefan Eßer    can be queried with **bcl_err(BclNumber)**. Possible errors include:
53850696a6eSStefan Eßer
53950696a6eSStefan Eßer    * **BCL_ERROR_INVALID_CONTEXT**
54050696a6eSStefan Eßer    * **BCL_ERROR_FATAL_ALLOC_ERR**
54150696a6eSStefan Eßer
54244d4804dSStefan Eßer**void bcl_num_free(BclNumber** _n_**)**
54350696a6eSStefan Eßer
54450696a6eSStefan Eßer:   Frees *n*. It is undefined behavior to use *n* after calling this function.
54550696a6eSStefan Eßer
54644d4804dSStefan Eßer**bool bcl_num_neg(BclNumber** _n_**)**
54750696a6eSStefan Eßer
54850696a6eSStefan Eßer:   Returns **true** if *n* is negative, **false** otherwise.
54950696a6eSStefan Eßer
55044d4804dSStefan Eßer**void bcl_num_setNeg(BclNumber** _n_**, bool** _neg_**)**
55150696a6eSStefan Eßer
55250696a6eSStefan Eßer:   Sets *n*'s sign to *neg*, where **true** is negative, and **false** is
55350696a6eSStefan Eßer    positive.
55450696a6eSStefan Eßer
55544d4804dSStefan Eßer**size_t bcl_num_scale(BclNumber** _n_**)**
55650696a6eSStefan Eßer
55750696a6eSStefan Eßer:   Returns the *scale* of *n*.
55850696a6eSStefan Eßer
55950696a6eSStefan Eßer    The *scale* of a number is the number of decimal places it has after the
56050696a6eSStefan Eßer    radix (decimal point).
56150696a6eSStefan Eßer
56244d4804dSStefan Eßer**BclError bcl_num_setScale(BclNumber** _n_**, size_t** _scale_**)**
56350696a6eSStefan Eßer
56450696a6eSStefan Eßer:   Sets the *scale* of *n* to the argument *scale*. If the argument *scale* is
56550696a6eSStefan Eßer    greater than the *scale* of *n*, *n* is extended. If the argument *scale* is
56650696a6eSStefan Eßer    less than the *scale* of *n*, *n* is truncated.
56750696a6eSStefan Eßer
56850696a6eSStefan Eßer    If there was no error, **BCL_ERROR_NONE** is returned. Otherwise, this
56950696a6eSStefan Eßer    function can return:
57050696a6eSStefan Eßer
57150696a6eSStefan Eßer    * **BCL_ERROR_INVALID_NUM**
57250696a6eSStefan Eßer    * **BCL_ERROR_INVALID_CONTEXT**
57350696a6eSStefan Eßer    * **BCL_ERROR_FATAL_ALLOC_ERR**
57450696a6eSStefan Eßer
57544d4804dSStefan Eßer**size_t bcl_num_len(BclNumber** _n_**)**
57650696a6eSStefan Eßer
57750696a6eSStefan Eßer:   Returns the number of *significant decimal digits* in *n*.
57850696a6eSStefan Eßer
57950696a6eSStefan Eßer## Conversion
58050696a6eSStefan Eßer
58150696a6eSStefan EßerAll procedures in this section require a valid current context.
58250696a6eSStefan Eßer
583175a4d10SStefan EßerAll procedures in this section without the **_keep** suffix in their name
584175a4d10SStefan Eßerconsume the given **BclNumber** arguments that are not given to pointer
585175a4d10SStefan Eßerarguments. See the **Consumption and Propagation** subsection below.
58650696a6eSStefan Eßer
58744d4804dSStefan Eßer**BclNumber bcl_parse(const char \*restrict** _val_**)**
58850696a6eSStefan Eßer
58950696a6eSStefan Eßer:   Parses a number string according to the current context's **ibase** and
59050696a6eSStefan Eßer    returns the resulting number.
59150696a6eSStefan Eßer
59250696a6eSStefan Eßer    *val* must be non-**NULL** and a valid string. See
59350696a6eSStefan Eßer    **BCL_ERROR_PARSE_INVALID_STR** in the **ERRORS** section for more
59450696a6eSStefan Eßer    information.
59550696a6eSStefan Eßer
59650696a6eSStefan Eßer    bcl(3) will encode an error in the return value, if there was one. The error
59750696a6eSStefan Eßer    can be queried with **bcl_err(BclNumber)**. Possible errors include:
59850696a6eSStefan Eßer
59950696a6eSStefan Eßer    * **BCL_ERROR_INVALID_NUM**
60050696a6eSStefan Eßer    * **BCL_ERROR_INVALID_CONTEXT**
60150696a6eSStefan Eßer    * **BCL_ERROR_PARSE_INVALID_STR**
60250696a6eSStefan Eßer    * **BCL_ERROR_FATAL_ALLOC_ERR**
60350696a6eSStefan Eßer
60444d4804dSStefan Eßer**char\* bcl_string(BclNumber** _n_**)**
60550696a6eSStefan Eßer
60650696a6eSStefan Eßer:   Returns a string representation of *n* according the the current context's
60750696a6eSStefan Eßer    **ibase**. The string is dynamically allocated and must be freed by the
60850696a6eSStefan Eßer    caller.
60950696a6eSStefan Eßer
61050696a6eSStefan Eßer    *n* is consumed; it cannot be used after the call. See the
61150696a6eSStefan Eßer    **Consumption and Propagation** subsection below.
61250696a6eSStefan Eßer
613175a4d10SStefan Eßer**char\* bcl_string_keep(BclNumber** _n_**)**
614175a4d10SStefan Eßer
615175a4d10SStefan Eßer:   Returns a string representation of *n* according the the current context's
616175a4d10SStefan Eßer    **ibase**. The string is dynamically allocated and must be freed by the
617175a4d10SStefan Eßer    caller.
618175a4d10SStefan Eßer
61944d4804dSStefan Eßer**BclError bcl_bigdig(BclNumber** _n_**, BclBigDig \***_result_**)**
62050696a6eSStefan Eßer
62150696a6eSStefan Eßer:   Converts *n* into a **BclBigDig** and returns the result in the space
62250696a6eSStefan Eßer    pointed to by *result*.
62350696a6eSStefan Eßer
62450696a6eSStefan Eßer    *a* must be smaller than **BC_OVERFLOW_MAX**. See the **LIMITS** section.
62550696a6eSStefan Eßer
62650696a6eSStefan Eßer    If there was no error, **BCL_ERROR_NONE** is returned. Otherwise, this
62750696a6eSStefan Eßer    function can return:
62850696a6eSStefan Eßer
62950696a6eSStefan Eßer    * **BCL_ERROR_INVALID_NUM**
63050696a6eSStefan Eßer    * **BCL_ERROR_INVALID_CONTEXT**
63150696a6eSStefan Eßer    * **BCL_ERROR_MATH_OVERFLOW**
63250696a6eSStefan Eßer
63350696a6eSStefan Eßer    *n* is consumed; it cannot be used after the call. See the
63450696a6eSStefan Eßer    **Consumption and Propagation** subsection below.
63550696a6eSStefan Eßer
636175a4d10SStefan Eßer**BclError bcl_bigdig_keep(BclNumber** _n_**, BclBigDig \***_result_**)**
637175a4d10SStefan Eßer
638175a4d10SStefan Eßer:   Converts *n* into a **BclBigDig** and returns the result in the space
639175a4d10SStefan Eßer    pointed to by *result*.
640175a4d10SStefan Eßer
641175a4d10SStefan Eßer    *a* must be smaller than **BC_OVERFLOW_MAX**. See the **LIMITS** section.
642175a4d10SStefan Eßer
643175a4d10SStefan Eßer    If there was no error, **BCL_ERROR_NONE** is returned. Otherwise, this
644175a4d10SStefan Eßer    function can return:
645175a4d10SStefan Eßer
646175a4d10SStefan Eßer    * **BCL_ERROR_INVALID_NUM**
647175a4d10SStefan Eßer    * **BCL_ERROR_INVALID_CONTEXT**
648175a4d10SStefan Eßer    * **BCL_ERROR_MATH_OVERFLOW**
649175a4d10SStefan Eßer
65044d4804dSStefan Eßer**BclNumber bcl_bigdig2num(BclBigDig** _val_**)**
65150696a6eSStefan Eßer
65250696a6eSStefan Eßer:   Creates a **BclNumber** from *val*.
65350696a6eSStefan Eßer
65450696a6eSStefan Eßer    bcl(3) will encode an error in the return value, if there was one. The error
65550696a6eSStefan Eßer    can be queried with **bcl_err(BclNumber)**. Possible errors include:
65650696a6eSStefan Eßer
65750696a6eSStefan Eßer    * **BCL_ERROR_INVALID_CONTEXT**
65850696a6eSStefan Eßer    * **BCL_ERROR_FATAL_ALLOC_ERR**
65950696a6eSStefan Eßer
66050696a6eSStefan Eßer## Math
66150696a6eSStefan Eßer
66250696a6eSStefan EßerAll procedures in this section require a valid current context.
66350696a6eSStefan Eßer
664175a4d10SStefan EßerAll procedures in this section without the **_keep** suffix in their name
665175a4d10SStefan Eßerconsume the given **BclNumber** arguments that are not given to pointer
666175a4d10SStefan Eßerarguments. See the **Consumption and Propagation** subsection below.
667175a4d10SStefan Eßer
66850696a6eSStefan EßerAll procedures in this section can return the following errors:
66950696a6eSStefan Eßer
67050696a6eSStefan Eßer* **BCL_ERROR_INVALID_NUM**
67150696a6eSStefan Eßer* **BCL_ERROR_INVALID_CONTEXT**
67250696a6eSStefan Eßer* **BCL_ERROR_FATAL_ALLOC_ERR**
67350696a6eSStefan Eßer
67444d4804dSStefan Eßer**BclNumber bcl_add(BclNumber** _a_**, BclNumber** _b_**)**
67550696a6eSStefan Eßer
67650696a6eSStefan Eßer:   Adds *a* and *b* and returns the result. The *scale* of the result is the
67750696a6eSStefan Eßer    max of the *scale*s of *a* and *b*.
67850696a6eSStefan Eßer
67950696a6eSStefan Eßer    *a* and *b* are consumed; they cannot be used after the call. See the
68050696a6eSStefan Eßer    **Consumption and Propagation** subsection below.
68150696a6eSStefan Eßer
68250696a6eSStefan Eßer    *a* and *b* can be the same number.
68350696a6eSStefan Eßer
68450696a6eSStefan Eßer    bcl(3) will encode an error in the return value, if there was one. The error
68550696a6eSStefan Eßer    can be queried with **bcl_err(BclNumber)**. Possible errors include:
68650696a6eSStefan Eßer
68750696a6eSStefan Eßer    * **BCL_ERROR_INVALID_NUM**
68850696a6eSStefan Eßer    * **BCL_ERROR_INVALID_CONTEXT**
68950696a6eSStefan Eßer    * **BCL_ERROR_FATAL_ALLOC_ERR**
69050696a6eSStefan Eßer
691175a4d10SStefan Eßer**BclNumber bcl_add_keep(BclNumber** _a_**, BclNumber** _b_**)**
692175a4d10SStefan Eßer
693175a4d10SStefan Eßer:   Adds *a* and *b* and returns the result. The *scale* of the result is the
694175a4d10SStefan Eßer    max of the *scale*s of *a* and *b*.
695175a4d10SStefan Eßer
696175a4d10SStefan Eßer    *a* and *b* can be the same number.
697175a4d10SStefan Eßer
698175a4d10SStefan Eßer    bcl(3) will encode an error in the return value, if there was one. The error
699175a4d10SStefan Eßer    can be queried with **bcl_err(BclNumber)**. Possible errors include:
700175a4d10SStefan Eßer
701175a4d10SStefan Eßer    * **BCL_ERROR_INVALID_NUM**
702175a4d10SStefan Eßer    * **BCL_ERROR_INVALID_CONTEXT**
703175a4d10SStefan Eßer    * **BCL_ERROR_FATAL_ALLOC_ERR**
704175a4d10SStefan Eßer
70544d4804dSStefan Eßer**BclNumber bcl_sub(BclNumber** _a_**, BclNumber** _b_**)**
70650696a6eSStefan Eßer
70750696a6eSStefan Eßer:   Subtracts *b* from *a* and returns the result. The *scale* of the result is
70850696a6eSStefan Eßer    the max of the *scale*s of *a* and *b*.
70950696a6eSStefan Eßer
71050696a6eSStefan Eßer    *a* and *b* are consumed; they cannot be used after the call. See the
71150696a6eSStefan Eßer    **Consumption and Propagation** subsection below.
71250696a6eSStefan Eßer
71350696a6eSStefan Eßer    *a* and *b* can be the same number.
71450696a6eSStefan Eßer
71550696a6eSStefan Eßer    bcl(3) will encode an error in the return value, if there was one. The error
71650696a6eSStefan Eßer    can be queried with **bcl_err(BclNumber)**. Possible errors include:
71750696a6eSStefan Eßer
71850696a6eSStefan Eßer    * **BCL_ERROR_INVALID_NUM**
71950696a6eSStefan Eßer    * **BCL_ERROR_INVALID_CONTEXT**
72050696a6eSStefan Eßer    * **BCL_ERROR_FATAL_ALLOC_ERR**
72150696a6eSStefan Eßer
722175a4d10SStefan Eßer**BclNumber bcl_sub_keep(BclNumber** _a_**, BclNumber** _b_**)**
723175a4d10SStefan Eßer
724175a4d10SStefan Eßer:   Subtracts *b* from *a* and returns the result. The *scale* of the result is
725175a4d10SStefan Eßer    the max of the *scale*s of *a* and *b*.
726175a4d10SStefan Eßer
727175a4d10SStefan Eßer    *a* and *b* can be the same number.
728175a4d10SStefan Eßer
729175a4d10SStefan Eßer    bcl(3) will encode an error in the return value, if there was one. The error
730175a4d10SStefan Eßer    can be queried with **bcl_err(BclNumber)**. Possible errors include:
731175a4d10SStefan Eßer
732175a4d10SStefan Eßer    * **BCL_ERROR_INVALID_NUM**
733175a4d10SStefan Eßer    * **BCL_ERROR_INVALID_CONTEXT**
734175a4d10SStefan Eßer    * **BCL_ERROR_FATAL_ALLOC_ERR**
735175a4d10SStefan Eßer
73644d4804dSStefan Eßer**BclNumber bcl_mul(BclNumber** _a_**, BclNumber** _b_**)**
73750696a6eSStefan Eßer
73850696a6eSStefan Eßer:   Multiplies *a* and *b* and returns the result. If *ascale* is the *scale* of
73950696a6eSStefan Eßer    *a* and *bscale* is the *scale* of *b*, the *scale* of the result is equal
74050696a6eSStefan Eßer    to **min(ascale+bscale,max(scale,ascale,bscale))**, where **min()** and
74150696a6eSStefan Eßer    **max()** return the obvious values.
74250696a6eSStefan Eßer
74350696a6eSStefan Eßer    *a* and *b* are consumed; they cannot be used after the call. See the
74450696a6eSStefan Eßer    **Consumption and Propagation** subsection below.
74550696a6eSStefan Eßer
74650696a6eSStefan Eßer    *a* and *b* can be the same number.
74750696a6eSStefan Eßer
74850696a6eSStefan Eßer    bcl(3) will encode an error in the return value, if there was one. The error
74950696a6eSStefan Eßer    can be queried with **bcl_err(BclNumber)**. Possible errors include:
75050696a6eSStefan Eßer
75150696a6eSStefan Eßer    * **BCL_ERROR_INVALID_NUM**
75250696a6eSStefan Eßer    * **BCL_ERROR_INVALID_CONTEXT**
75350696a6eSStefan Eßer    * **BCL_ERROR_FATAL_ALLOC_ERR**
75450696a6eSStefan Eßer
755175a4d10SStefan Eßer**BclNumber bcl_mul_keep(BclNumber** _a_**, BclNumber** _b_**)**
756175a4d10SStefan Eßer
757175a4d10SStefan Eßer:   Multiplies *a* and *b* and returns the result. If *ascale* is the *scale* of
758175a4d10SStefan Eßer    *a* and *bscale* is the *scale* of *b*, the *scale* of the result is equal
759175a4d10SStefan Eßer    to **min(ascale+bscale,max(scale,ascale,bscale))**, where **min()** and
760175a4d10SStefan Eßer    **max()** return the obvious values.
761175a4d10SStefan Eßer
762175a4d10SStefan Eßer    *a* and *b* can be the same number.
763175a4d10SStefan Eßer
764175a4d10SStefan Eßer    bcl(3) will encode an error in the return value, if there was one. The error
765175a4d10SStefan Eßer    can be queried with **bcl_err(BclNumber)**. Possible errors include:
766175a4d10SStefan Eßer
767175a4d10SStefan Eßer    * **BCL_ERROR_INVALID_NUM**
768175a4d10SStefan Eßer    * **BCL_ERROR_INVALID_CONTEXT**
769175a4d10SStefan Eßer    * **BCL_ERROR_FATAL_ALLOC_ERR**
770175a4d10SStefan Eßer
77144d4804dSStefan Eßer**BclNumber bcl_div(BclNumber** _a_**, BclNumber** _b_**)**
77250696a6eSStefan Eßer
77350696a6eSStefan Eßer:   Divides *a* by *b* and returns the result. The *scale* of the result is the
77450696a6eSStefan Eßer    *scale* of the current context.
77550696a6eSStefan Eßer
77650696a6eSStefan Eßer    *b* cannot be **0**.
77750696a6eSStefan Eßer
77850696a6eSStefan Eßer    *a* and *b* are consumed; they cannot be used after the call. See the
77950696a6eSStefan Eßer    **Consumption and Propagation** subsection below.
78050696a6eSStefan Eßer
78150696a6eSStefan Eßer    *a* and *b* can be the same number.
78250696a6eSStefan Eßer
78350696a6eSStefan Eßer    bcl(3) will encode an error in the return value, if there was one. The error
78450696a6eSStefan Eßer    can be queried with **bcl_err(BclNumber)**. Possible errors include:
78550696a6eSStefan Eßer
78650696a6eSStefan Eßer    * **BCL_ERROR_INVALID_NUM**
78750696a6eSStefan Eßer    * **BCL_ERROR_INVALID_CONTEXT**
78850696a6eSStefan Eßer    * **BCL_ERROR_MATH_DIVIDE_BY_ZERO**
78950696a6eSStefan Eßer    * **BCL_ERROR_FATAL_ALLOC_ERR**
79050696a6eSStefan Eßer
791175a4d10SStefan Eßer**BclNumber bcl_div_keep(BclNumber** _a_**, BclNumber** _b_**)**
792175a4d10SStefan Eßer
793175a4d10SStefan Eßer:   Divides *a* by *b* and returns the result. The *scale* of the result is the
794175a4d10SStefan Eßer    *scale* of the current context.
795175a4d10SStefan Eßer
796175a4d10SStefan Eßer    *b* cannot be **0**.
797175a4d10SStefan Eßer
798175a4d10SStefan Eßer    *a* and *b* can be the same number.
799175a4d10SStefan Eßer
800175a4d10SStefan Eßer    bcl(3) will encode an error in the return value, if there was one. The error
801175a4d10SStefan Eßer    can be queried with **bcl_err(BclNumber)**. Possible errors include:
802175a4d10SStefan Eßer
803175a4d10SStefan Eßer    * **BCL_ERROR_INVALID_NUM**
804175a4d10SStefan Eßer    * **BCL_ERROR_INVALID_CONTEXT**
805175a4d10SStefan Eßer    * **BCL_ERROR_MATH_DIVIDE_BY_ZERO**
806175a4d10SStefan Eßer    * **BCL_ERROR_FATAL_ALLOC_ERR**
807175a4d10SStefan Eßer
80844d4804dSStefan Eßer**BclNumber bcl_mod(BclNumber** _a_**, BclNumber** _b_**)**
80950696a6eSStefan Eßer
81050696a6eSStefan Eßer:   Divides *a* by *b* to the *scale* of the current context, computes the
81150696a6eSStefan Eßer    modulus **a-(a/b)\*b**, and returns the modulus.
81250696a6eSStefan Eßer
81350696a6eSStefan Eßer    *b* cannot be **0**.
81450696a6eSStefan Eßer
81550696a6eSStefan Eßer    *a* and *b* are consumed; they cannot be used after the call. See the
81650696a6eSStefan Eßer    **Consumption and Propagation** subsection below.
81750696a6eSStefan Eßer
81850696a6eSStefan Eßer    *a* and *b* can be the same number.
81950696a6eSStefan Eßer
82050696a6eSStefan Eßer    bcl(3) will encode an error in the return value, if there was one. The error
82150696a6eSStefan Eßer    can be queried with **bcl_err(BclNumber)**. Possible errors include:
82250696a6eSStefan Eßer
82350696a6eSStefan Eßer    * **BCL_ERROR_INVALID_NUM**
82450696a6eSStefan Eßer    * **BCL_ERROR_INVALID_CONTEXT**
82550696a6eSStefan Eßer    * **BCL_ERROR_MATH_DIVIDE_BY_ZERO**
82650696a6eSStefan Eßer    * **BCL_ERROR_FATAL_ALLOC_ERR**
82750696a6eSStefan Eßer
828175a4d10SStefan Eßer**BclNumber bcl_mod_keep(BclNumber** _a_**, BclNumber** _b_**)**
829175a4d10SStefan Eßer
830175a4d10SStefan Eßer:   Divides *a* by *b* to the *scale* of the current context, computes the
831175a4d10SStefan Eßer    modulus **a-(a/b)\*b**, and returns the modulus.
832175a4d10SStefan Eßer
833175a4d10SStefan Eßer    *b* cannot be **0**.
834175a4d10SStefan Eßer
835175a4d10SStefan Eßer    *a* and *b* can be the same number.
836175a4d10SStefan Eßer
837175a4d10SStefan Eßer    bcl(3) will encode an error in the return value, if there was one. The error
838175a4d10SStefan Eßer    can be queried with **bcl_err(BclNumber)**. Possible errors include:
839175a4d10SStefan Eßer
840175a4d10SStefan Eßer    * **BCL_ERROR_INVALID_NUM**
841175a4d10SStefan Eßer    * **BCL_ERROR_INVALID_CONTEXT**
842175a4d10SStefan Eßer    * **BCL_ERROR_MATH_DIVIDE_BY_ZERO**
843175a4d10SStefan Eßer    * **BCL_ERROR_FATAL_ALLOC_ERR**
844175a4d10SStefan Eßer
84544d4804dSStefan Eßer**BclNumber bcl_pow(BclNumber** _a_**, BclNumber** _b_**)**
84650696a6eSStefan Eßer
84750696a6eSStefan Eßer:   Calculates *a* to the power of *b* to the *scale* of the current context.
84850696a6eSStefan Eßer    *b* must be an integer, but can be negative. If it is negative, *a* must
84950696a6eSStefan Eßer    be non-zero.
85050696a6eSStefan Eßer
85150696a6eSStefan Eßer    *b* must be an integer. If *b* is negative, *a* must not be **0**.
85250696a6eSStefan Eßer
85350696a6eSStefan Eßer    *a* must be smaller than **BC_OVERFLOW_MAX**. See the **LIMITS** section.
85450696a6eSStefan Eßer
85550696a6eSStefan Eßer    *a* and *b* are consumed; they cannot be used after the call. See the
85650696a6eSStefan Eßer    **Consumption and Propagation** subsection below.
85750696a6eSStefan Eßer
85850696a6eSStefan Eßer    *a* and *b* can be the same number.
85950696a6eSStefan Eßer
86050696a6eSStefan Eßer    bcl(3) will encode an error in the return value, if there was one. The error
86150696a6eSStefan Eßer    can be queried with **bcl_err(BclNumber)**. Possible errors include:
86250696a6eSStefan Eßer
86350696a6eSStefan Eßer    * **BCL_ERROR_INVALID_NUM**
86450696a6eSStefan Eßer    * **BCL_ERROR_INVALID_CONTEXT**
86550696a6eSStefan Eßer    * **BCL_ERROR_MATH_NON_INTEGER**
86650696a6eSStefan Eßer    * **BCL_ERROR_MATH_OVERFLOW**
86750696a6eSStefan Eßer    * **BCL_ERROR_MATH_DIVIDE_BY_ZERO**
86850696a6eSStefan Eßer    * **BCL_ERROR_FATAL_ALLOC_ERR**
86950696a6eSStefan Eßer
870175a4d10SStefan Eßer**BclNumber bcl_pow_keep(BclNumber** _a_**, BclNumber** _b_**)**
871175a4d10SStefan Eßer
872175a4d10SStefan Eßer:   Calculates *a* to the power of *b* to the *scale* of the current context.
873175a4d10SStefan Eßer    *b* must be an integer, but can be negative. If it is negative, *a* must
874175a4d10SStefan Eßer    be non-zero.
875175a4d10SStefan Eßer
876175a4d10SStefan Eßer    *b* must be an integer. If *b* is negative, *a* must not be **0**.
877175a4d10SStefan Eßer
878175a4d10SStefan Eßer    *a* must be smaller than **BC_OVERFLOW_MAX**. See the **LIMITS** section.
879175a4d10SStefan Eßer
880175a4d10SStefan Eßer    *a* and *b* can be the same number.
881175a4d10SStefan Eßer
882175a4d10SStefan Eßer    bcl(3) will encode an error in the return value, if there was one. The error
883175a4d10SStefan Eßer    can be queried with **bcl_err(BclNumber)**. Possible errors include:
884175a4d10SStefan Eßer
885175a4d10SStefan Eßer    * **BCL_ERROR_INVALID_NUM**
886175a4d10SStefan Eßer    * **BCL_ERROR_INVALID_CONTEXT**
887175a4d10SStefan Eßer    * **BCL_ERROR_MATH_NON_INTEGER**
888175a4d10SStefan Eßer    * **BCL_ERROR_MATH_OVERFLOW**
889175a4d10SStefan Eßer    * **BCL_ERROR_MATH_DIVIDE_BY_ZERO**
890175a4d10SStefan Eßer    * **BCL_ERROR_FATAL_ALLOC_ERR**
891175a4d10SStefan Eßer
89244d4804dSStefan Eßer**BclNumber bcl_lshift(BclNumber** _a_**, BclNumber** _b_**)**
89350696a6eSStefan Eßer
89450696a6eSStefan Eßer:   Shifts *a* left (moves the radix right) by *b* places and returns the
89550696a6eSStefan Eßer    result. This is done in decimal. *b* must be an integer.
89650696a6eSStefan Eßer
89750696a6eSStefan Eßer    *b* must be an integer.
89850696a6eSStefan Eßer
89950696a6eSStefan Eßer    *a* and *b* are consumed; they cannot be used after the call. See the
90050696a6eSStefan Eßer    **Consumption and Propagation** subsection below.
90150696a6eSStefan Eßer
90250696a6eSStefan Eßer    *a* and *b* can be the same number.
90350696a6eSStefan Eßer
90450696a6eSStefan Eßer    bcl(3) will encode an error in the return value, if there was one. The error
90550696a6eSStefan Eßer    can be queried with **bcl_err(BclNumber)**. Possible errors include:
90650696a6eSStefan Eßer
90750696a6eSStefan Eßer    * **BCL_ERROR_INVALID_NUM**
90850696a6eSStefan Eßer    * **BCL_ERROR_INVALID_CONTEXT**
90950696a6eSStefan Eßer    * **BCL_ERROR_MATH_NON_INTEGER**
91050696a6eSStefan Eßer    * **BCL_ERROR_FATAL_ALLOC_ERR**
91150696a6eSStefan Eßer
912175a4d10SStefan Eßer**BclNumber bcl_lshift_keep(BclNumber** _a_**, BclNumber** _b_**)**
913175a4d10SStefan Eßer
914175a4d10SStefan Eßer:   Shifts *a* left (moves the radix right) by *b* places and returns the
915175a4d10SStefan Eßer    result. This is done in decimal. *b* must be an integer.
916175a4d10SStefan Eßer
917175a4d10SStefan Eßer    *b* must be an integer.
918175a4d10SStefan Eßer
919175a4d10SStefan Eßer    *a* and *b* can be the same number.
920175a4d10SStefan Eßer
921175a4d10SStefan Eßer    bcl(3) will encode an error in the return value, if there was one. The error
922175a4d10SStefan Eßer    can be queried with **bcl_err(BclNumber)**. Possible errors include:
923175a4d10SStefan Eßer
924175a4d10SStefan Eßer    * **BCL_ERROR_INVALID_NUM**
925175a4d10SStefan Eßer    * **BCL_ERROR_INVALID_CONTEXT**
926175a4d10SStefan Eßer    * **BCL_ERROR_MATH_NON_INTEGER**
927175a4d10SStefan Eßer    * **BCL_ERROR_FATAL_ALLOC_ERR**
928175a4d10SStefan Eßer
92944d4804dSStefan Eßer**BclNumber bcl_rshift(BclNumber** _a_**, BclNumber** _b_**)**
93050696a6eSStefan Eßer
93150696a6eSStefan Eßer:   Shifts *a* right (moves the radix left) by *b* places and returns the
93250696a6eSStefan Eßer    result. This is done in decimal. *b* must be an integer.
93350696a6eSStefan Eßer
93450696a6eSStefan Eßer    *b* must be an integer.
93550696a6eSStefan Eßer
93650696a6eSStefan Eßer    *a* and *b* are consumed; they cannot be used after the call. See the
93750696a6eSStefan Eßer    **Consumption and Propagation** subsection below.
93850696a6eSStefan Eßer
93950696a6eSStefan Eßer    *a* and *b* can be the same number.
94050696a6eSStefan Eßer
94150696a6eSStefan Eßer    bcl(3) will encode an error in the return value, if there was one. The error
94250696a6eSStefan Eßer    can be queried with **bcl_err(BclNumber)**. Possible errors include:
94350696a6eSStefan Eßer
94450696a6eSStefan Eßer    * **BCL_ERROR_INVALID_NUM**
94550696a6eSStefan Eßer    * **BCL_ERROR_INVALID_CONTEXT**
94650696a6eSStefan Eßer    * **BCL_ERROR_MATH_NON_INTEGER**
94750696a6eSStefan Eßer    * **BCL_ERROR_FATAL_ALLOC_ERR**
94850696a6eSStefan Eßer
949175a4d10SStefan Eßer**BclNumber bcl_rshift_keep(BclNumber** _a_**, BclNumber** _b_**)**
950175a4d10SStefan Eßer
951175a4d10SStefan Eßer:   Shifts *a* right (moves the radix left) by *b* places and returns the
952175a4d10SStefan Eßer    result. This is done in decimal. *b* must be an integer.
953175a4d10SStefan Eßer
954175a4d10SStefan Eßer    *b* must be an integer.
955175a4d10SStefan Eßer
956175a4d10SStefan Eßer    *a* and *b* can be the same number.
957175a4d10SStefan Eßer
958175a4d10SStefan Eßer    bcl(3) will encode an error in the return value, if there was one. The error
959175a4d10SStefan Eßer    can be queried with **bcl_err(BclNumber)**. Possible errors include:
960175a4d10SStefan Eßer
961175a4d10SStefan Eßer    * **BCL_ERROR_INVALID_NUM**
962175a4d10SStefan Eßer    * **BCL_ERROR_INVALID_CONTEXT**
963175a4d10SStefan Eßer    * **BCL_ERROR_MATH_NON_INTEGER**
964175a4d10SStefan Eßer    * **BCL_ERROR_FATAL_ALLOC_ERR**
965175a4d10SStefan Eßer
96644d4804dSStefan Eßer**BclNumber bcl_sqrt(BclNumber** _a_**)**
96750696a6eSStefan Eßer
96850696a6eSStefan Eßer:   Calculates the square root of *a* and returns the result. The *scale* of the
96950696a6eSStefan Eßer    result is equal to the **scale** of the current context.
97050696a6eSStefan Eßer
97150696a6eSStefan Eßer    *a* cannot be negative.
97250696a6eSStefan Eßer
97350696a6eSStefan Eßer    *a* is consumed; it cannot be used after the call. See the
97450696a6eSStefan Eßer    **Consumption and Propagation** subsection below.
97550696a6eSStefan Eßer
97650696a6eSStefan Eßer    bcl(3) will encode an error in the return value, if there was one. The error
97750696a6eSStefan Eßer    can be queried with **bcl_err(BclNumber)**. Possible errors include:
97850696a6eSStefan Eßer
97950696a6eSStefan Eßer    * **BCL_ERROR_INVALID_NUM**
98050696a6eSStefan Eßer    * **BCL_ERROR_INVALID_CONTEXT**
98150696a6eSStefan Eßer    * **BCL_ERROR_MATH_NEGATIVE**
98250696a6eSStefan Eßer    * **BCL_ERROR_FATAL_ALLOC_ERR**
98350696a6eSStefan Eßer
984175a4d10SStefan Eßer**BclNumber bcl_sqrt_keep(BclNumber** _a_**)**
985175a4d10SStefan Eßer
986175a4d10SStefan Eßer:   Calculates the square root of *a* and returns the result. The *scale* of the
987175a4d10SStefan Eßer    result is equal to the **scale** of the current context.
988175a4d10SStefan Eßer
989175a4d10SStefan Eßer    *a* cannot be negative.
990175a4d10SStefan Eßer
991175a4d10SStefan Eßer    bcl(3) will encode an error in the return value, if there was one. The error
992175a4d10SStefan Eßer    can be queried with **bcl_err(BclNumber)**. Possible errors include:
993175a4d10SStefan Eßer
994175a4d10SStefan Eßer    * **BCL_ERROR_INVALID_NUM**
995175a4d10SStefan Eßer    * **BCL_ERROR_INVALID_CONTEXT**
996175a4d10SStefan Eßer    * **BCL_ERROR_MATH_NEGATIVE**
997175a4d10SStefan Eßer    * **BCL_ERROR_FATAL_ALLOC_ERR**
998175a4d10SStefan Eßer
99944d4804dSStefan Eßer**BclError bcl_divmod(BclNumber** _a_**, BclNumber** _b_**, BclNumber \***_c_**, BclNumber \***_d_**)**
100050696a6eSStefan Eßer
100150696a6eSStefan Eßer:   Divides *a* by *b* and returns the quotient in a new number which is put
100250696a6eSStefan Eßer    into the space pointed to by *c*, and puts the modulus in a new number which
100350696a6eSStefan Eßer    is put into the space pointed to by *d*.
100450696a6eSStefan Eßer
100550696a6eSStefan Eßer    *b* cannot be **0**.
100650696a6eSStefan Eßer
100750696a6eSStefan Eßer    *a* and *b* are consumed; they cannot be used after the call. See the
100850696a6eSStefan Eßer    **Consumption and Propagation** subsection below.
100950696a6eSStefan Eßer
101050696a6eSStefan Eßer    *c* and *d* cannot point to the same place, nor can they point to the space
101150696a6eSStefan Eßer    occupied by *a* or *b*.
101250696a6eSStefan Eßer
101350696a6eSStefan Eßer    If there was no error, **BCL_ERROR_NONE** is returned. Otherwise, this
101450696a6eSStefan Eßer    function can return:
101550696a6eSStefan Eßer
101650696a6eSStefan Eßer    * **BCL_ERROR_INVALID_NUM**
101750696a6eSStefan Eßer    * **BCL_ERROR_INVALID_CONTEXT**
101850696a6eSStefan Eßer    * **BCL_ERROR_MATH_DIVIDE_BY_ZERO**
101950696a6eSStefan Eßer    * **BCL_ERROR_FATAL_ALLOC_ERR**
102050696a6eSStefan Eßer
1021175a4d10SStefan Eßer**BclError bcl_divmod_keep(BclNumber** _a_**, BclNumber** _b_**, BclNumber \***_c_**, BclNumber \***_d_**)**
1022175a4d10SStefan Eßer
1023175a4d10SStefan Eßer:   Divides *a* by *b* and returns the quotient in a new number which is put
1024175a4d10SStefan Eßer    into the space pointed to by *c*, and puts the modulus in a new number which
1025175a4d10SStefan Eßer    is put into the space pointed to by *d*.
1026175a4d10SStefan Eßer
1027175a4d10SStefan Eßer    *b* cannot be **0**.
1028175a4d10SStefan Eßer
1029175a4d10SStefan Eßer    *c* and *d* cannot point to the same place, nor can they point to the space
1030175a4d10SStefan Eßer    occupied by *a* or *b*.
1031175a4d10SStefan Eßer
1032175a4d10SStefan Eßer    If there was no error, **BCL_ERROR_NONE** is returned. Otherwise, this
1033175a4d10SStefan Eßer    function can return:
1034175a4d10SStefan Eßer
1035175a4d10SStefan Eßer    * **BCL_ERROR_INVALID_NUM**
1036175a4d10SStefan Eßer    * **BCL_ERROR_INVALID_CONTEXT**
1037175a4d10SStefan Eßer    * **BCL_ERROR_MATH_DIVIDE_BY_ZERO**
1038175a4d10SStefan Eßer    * **BCL_ERROR_FATAL_ALLOC_ERR**
1039175a4d10SStefan Eßer
104044d4804dSStefan Eßer**BclNumber bcl_modexp(BclNumber** _a_**, BclNumber** _b_**, BclNumber** _c_**)**
104150696a6eSStefan Eßer
104250696a6eSStefan Eßer:   Computes a modular exponentiation where *a* is the base, *b* is the
104350696a6eSStefan Eßer    exponent, and *c* is the modulus, and returns the result. The *scale* of the
104450696a6eSStefan Eßer    result is equal to the **scale** of the current context.
104550696a6eSStefan Eßer
104650696a6eSStefan Eßer    *a*, *b*, and *c* must be integers. *c* must not be **0**. *b* must not be
104750696a6eSStefan Eßer    negative.
104850696a6eSStefan Eßer
104950696a6eSStefan Eßer    *a*, *b*, and *c* are consumed; they cannot be used after the call. See the
105050696a6eSStefan Eßer    **Consumption and Propagation** subsection below.
105150696a6eSStefan Eßer
105250696a6eSStefan Eßer    bcl(3) will encode an error in the return value, if there was one. The error
105350696a6eSStefan Eßer    can be queried with **bcl_err(BclNumber)**. Possible errors include:
105450696a6eSStefan Eßer
105550696a6eSStefan Eßer    * **BCL_ERROR_INVALID_NUM**
105650696a6eSStefan Eßer    * **BCL_ERROR_INVALID_CONTEXT**
105750696a6eSStefan Eßer    * **BCL_ERROR_MATH_NEGATIVE**
105850696a6eSStefan Eßer    * **BCL_ERROR_MATH_NON_INTEGER**
105950696a6eSStefan Eßer    * **BCL_ERROR_MATH_DIVIDE_BY_ZERO**
106050696a6eSStefan Eßer    * **BCL_ERROR_FATAL_ALLOC_ERR**
106150696a6eSStefan Eßer
1062175a4d10SStefan Eßer**BclNumber bcl_modexp_keep(BclNumber** _a_**, BclNumber** _b_**, BclNumber** _c_**)**
1063175a4d10SStefan Eßer
1064175a4d10SStefan Eßer:   Computes a modular exponentiation where *a* is the base, *b* is the
1065175a4d10SStefan Eßer    exponent, and *c* is the modulus, and returns the result. The *scale* of the
1066175a4d10SStefan Eßer    result is equal to the **scale** of the current context.
1067175a4d10SStefan Eßer
1068175a4d10SStefan Eßer    *a*, *b*, and *c* must be integers. *c* must not be **0**. *b* must not be
1069175a4d10SStefan Eßer    negative.
1070175a4d10SStefan Eßer
1071175a4d10SStefan Eßer    bcl(3) will encode an error in the return value, if there was one. The error
1072175a4d10SStefan Eßer    can be queried with **bcl_err(BclNumber)**. Possible errors include:
1073175a4d10SStefan Eßer
1074175a4d10SStefan Eßer    * **BCL_ERROR_INVALID_NUM**
1075175a4d10SStefan Eßer    * **BCL_ERROR_INVALID_CONTEXT**
1076175a4d10SStefan Eßer    * **BCL_ERROR_MATH_NEGATIVE**
1077175a4d10SStefan Eßer    * **BCL_ERROR_MATH_NON_INTEGER**
1078175a4d10SStefan Eßer    * **BCL_ERROR_MATH_DIVIDE_BY_ZERO**
1079175a4d10SStefan Eßer    * **BCL_ERROR_FATAL_ALLOC_ERR**
1080175a4d10SStefan Eßer
108150696a6eSStefan Eßer## Miscellaneous
108250696a6eSStefan Eßer
108344d4804dSStefan Eßer**void bcl_zero(BclNumber** _n_**)**
108450696a6eSStefan Eßer
108550696a6eSStefan Eßer:   Sets *n* to **0**.
108650696a6eSStefan Eßer
108744d4804dSStefan Eßer**void bcl_one(BclNumber** _n_**)**
108850696a6eSStefan Eßer
108950696a6eSStefan Eßer:   Sets *n* to **1**.
109050696a6eSStefan Eßer
109144d4804dSStefan Eßer**ssize_t bcl_cmp(BclNumber** _a_**, BclNumber** _b_**)**
109250696a6eSStefan Eßer
109350696a6eSStefan Eßer:   Compares *a* and *b* and returns **0** if *a* and *b* are equal, **<0** if
109450696a6eSStefan Eßer    *a* is less than *b*, and **>0** if *a* is greater than *b*.
109550696a6eSStefan Eßer
109644d4804dSStefan Eßer**BclError bcl_copy(BclNumber** _d_**, BclNumber** _s_**)**
109750696a6eSStefan Eßer
109850696a6eSStefan Eßer:   Copies *s* into *d*.
109950696a6eSStefan Eßer
110050696a6eSStefan Eßer    If there was no error, **BCL_ERROR_NONE** is returned. Otherwise, this
110150696a6eSStefan Eßer    function can return:
110250696a6eSStefan Eßer
110350696a6eSStefan Eßer    * **BCL_ERROR_INVALID_NUM**
110450696a6eSStefan Eßer    * **BCL_ERROR_INVALID_CONTEXT**
110550696a6eSStefan Eßer    * **BCL_ERROR_FATAL_ALLOC_ERR**
110650696a6eSStefan Eßer
110744d4804dSStefan Eßer**BclNumber bcl_dup(BclNumber** _s_**)**
110850696a6eSStefan Eßer
110950696a6eSStefan Eßer:   Creates and returns a new **BclNumber** that is a copy of *s*.
111050696a6eSStefan Eßer
111150696a6eSStefan Eßer    bcl(3) will encode an error in the return value, if there was one. The error
111250696a6eSStefan Eßer    can be queried with **bcl_err(BclNumber)**. Possible errors include:
111350696a6eSStefan Eßer
111450696a6eSStefan Eßer    * **BCL_ERROR_INVALID_NUM**
111550696a6eSStefan Eßer    * **BCL_ERROR_INVALID_CONTEXT**
111650696a6eSStefan Eßer    * **BCL_ERROR_FATAL_ALLOC_ERR**
111750696a6eSStefan Eßer
111850696a6eSStefan Eßer## Pseudo-Random Number Generator
111950696a6eSStefan Eßer
112050696a6eSStefan EßerThe pseudo-random number generator in bcl(3) is a *seeded* PRNG. Given the same
112150696a6eSStefan Eßerseed twice, it will produce the same sequence of pseudo-random numbers twice.
112250696a6eSStefan Eßer
112350696a6eSStefan EßerBy default, bcl(3) attempts to seed the PRNG with data from **/dev/urandom**. If
112450696a6eSStefan Eßerthat fails, it seeds itself with by calling **libc**'s **srand(time(NULL))** and
112550696a6eSStefan Eßerthen calling **rand()** for each byte, since **rand()** is only guaranteed to
112650696a6eSStefan Eßerreturn **15** bits.
112750696a6eSStefan Eßer
112850696a6eSStefan EßerThis should provide fairly good seeding in the standard case while also
112950696a6eSStefan Eßerremaining fairly portable.
113050696a6eSStefan Eßer
113150696a6eSStefan EßerIf necessary, the PRNG can be reseeded with one of the following functions:
113250696a6eSStefan Eßer
113350696a6eSStefan Eßer* **bcl_rand_seedWithNum(BclNumber)**
113444d4804dSStefan Eßer* **bcl_rand_seed(unsigned char[**_BCL_SEED_SIZE_**])**
113544d4804dSStefan Eßer* **bcl_rand_reseed(**_void_**)**
113650696a6eSStefan Eßer
1137175a4d10SStefan EßerAll procedures in this section without the **_keep** suffix in their name
1138175a4d10SStefan Eßerconsume the given **BclNumber** arguments that are not given to pointer
1139175a4d10SStefan Eßerarguments. See the **Consumption and Propagation** subsection below.
1140175a4d10SStefan Eßer
114150696a6eSStefan EßerThe following items allow clients to use the pseudo-random number generator. All
114250696a6eSStefan Eßerprocedures require a valid current context.
114350696a6eSStefan Eßer
114450696a6eSStefan Eßer**BCL_SEED_ULONGS**
114550696a6eSStefan Eßer
114650696a6eSStefan Eßer:   The number of **unsigned long**'s in a seed for bcl(3)'s random number
114750696a6eSStefan Eßer    generator.
114850696a6eSStefan Eßer
114950696a6eSStefan Eßer**BCL_SEED_SIZE**
115050696a6eSStefan Eßer
115150696a6eSStefan Eßer:   The size, in **char**'s, of a seed for bcl(3)'s random number generator.
115250696a6eSStefan Eßer
115350696a6eSStefan Eßer**BclBigDig**
115450696a6eSStefan Eßer
115550696a6eSStefan Eßer:   bcl(3)'s overflow type (see the **PERFORMANCE** section).
115650696a6eSStefan Eßer
115750696a6eSStefan Eßer**BclRandInt**
115850696a6eSStefan Eßer
115950696a6eSStefan Eßer:   An unsigned integer type returned by bcl(3)'s random number generator.
116050696a6eSStefan Eßer
116144d4804dSStefan Eßer**BclNumber bcl_irand(BclNumber** _a_**)**
116250696a6eSStefan Eßer
116350696a6eSStefan Eßer:   Returns a random number that is not larger than *a* in a new number. If *a*
116450696a6eSStefan Eßer    is **0** or **1**, the new number is equal to **0**. The bound is unlimited,
116550696a6eSStefan Eßer    so it is not bound to the size of **BclRandInt**. This is done by generating
116650696a6eSStefan Eßer    as many random numbers as necessary, multiplying them by certain exponents,
116750696a6eSStefan Eßer    and adding them all together.
116850696a6eSStefan Eßer
116950696a6eSStefan Eßer    *a* must be an integer and non-negative.
117050696a6eSStefan Eßer
1171175a4d10SStefan Eßer    *a* is consumed; it cannot be used after the call. See the **Consumption and
1172175a4d10SStefan Eßer    Propagation** subsection below.
1173175a4d10SStefan Eßer
1174175a4d10SStefan Eßer    This procedure requires a valid current context.
1175175a4d10SStefan Eßer
1176175a4d10SStefan Eßer    bcl(3) will encode an error in the return value, if there was one. The error
1177175a4d10SStefan Eßer    can be queried with **bcl_err(BclNumber)**. Possible errors include:
1178175a4d10SStefan Eßer
1179175a4d10SStefan Eßer    * **BCL_ERROR_INVALID_NUM**
1180175a4d10SStefan Eßer    * **BCL_ERROR_INVALID_CONTEXT**
1181175a4d10SStefan Eßer    * **BCL_ERROR_MATH_NEGATIVE**
1182175a4d10SStefan Eßer    * **BCL_ERROR_MATH_NON_INTEGER**
1183175a4d10SStefan Eßer    * **BCL_ERROR_FATAL_ALLOC_ERR**
1184175a4d10SStefan Eßer
1185175a4d10SStefan Eßer**BclNumber bcl_irand_keep(BclNumber** _a_**)**
1186175a4d10SStefan Eßer
1187175a4d10SStefan Eßer:   Returns a random number that is not larger than *a* in a new number. If *a*
1188175a4d10SStefan Eßer    is **0** or **1**, the new number is equal to **0**. The bound is unlimited,
1189175a4d10SStefan Eßer    so it is not bound to the size of **BclRandInt**. This is done by generating
1190175a4d10SStefan Eßer    as many random numbers as necessary, multiplying them by certain exponents,
1191175a4d10SStefan Eßer    and adding them all together.
1192175a4d10SStefan Eßer
1193175a4d10SStefan Eßer    *a* must be an integer and non-negative.
119450696a6eSStefan Eßer
119550696a6eSStefan Eßer    This procedure requires a valid current context.
119650696a6eSStefan Eßer
119750696a6eSStefan Eßer    bcl(3) will encode an error in the return value, if there was one. The error
119850696a6eSStefan Eßer    can be queried with **bcl_err(BclNumber)**. Possible errors include:
119950696a6eSStefan Eßer
120050696a6eSStefan Eßer    * **BCL_ERROR_INVALID_NUM**
120150696a6eSStefan Eßer    * **BCL_ERROR_INVALID_CONTEXT**
120250696a6eSStefan Eßer    * **BCL_ERROR_MATH_NEGATIVE**
120350696a6eSStefan Eßer    * **BCL_ERROR_MATH_NON_INTEGER**
120450696a6eSStefan Eßer    * **BCL_ERROR_FATAL_ALLOC_ERR**
120550696a6eSStefan Eßer
120644d4804dSStefan Eßer**BclNumber bcl_frand(size_t** _places_**)**
120750696a6eSStefan Eßer
120850696a6eSStefan Eßer:   Returns a random number between **0** (inclusive) and **1** (exclusive) that
120950696a6eSStefan Eßer    has *places* decimal digits after the radix (decimal point). There are no
121050696a6eSStefan Eßer    limits on *places*.
121150696a6eSStefan Eßer
121250696a6eSStefan Eßer    This procedure requires a valid current context.
121350696a6eSStefan Eßer
121450696a6eSStefan Eßer    bcl(3) will encode an error in the return value, if there was one. The error
121550696a6eSStefan Eßer    can be queried with **bcl_err(BclNumber)**. Possible errors include:
121650696a6eSStefan Eßer
121750696a6eSStefan Eßer    * **BCL_ERROR_INVALID_CONTEXT**
121850696a6eSStefan Eßer    * **BCL_ERROR_FATAL_ALLOC_ERR**
121950696a6eSStefan Eßer
122044d4804dSStefan Eßer**BclNumber bcl_ifrand(BclNumber** _a_**, size_t** _places_**)**
122150696a6eSStefan Eßer
122250696a6eSStefan Eßer:   Returns a random number less than *a* with *places* decimal digits after the
122350696a6eSStefan Eßer    radix (decimal point). There are no limits on *a* or *places*.
122450696a6eSStefan Eßer
122550696a6eSStefan Eßer    *a* must be an integer and non-negative.
122650696a6eSStefan Eßer
1227175a4d10SStefan Eßer    *a* is consumed; it cannot be used after the call. See the **Consumption and
1228175a4d10SStefan Eßer    Propagation** subsection below.
1229175a4d10SStefan Eßer
1230175a4d10SStefan Eßer    This procedure requires a valid current context.
1231175a4d10SStefan Eßer
1232175a4d10SStefan Eßer    bcl(3) will encode an error in the return value, if there was one. The error
1233175a4d10SStefan Eßer    can be queried with **bcl_err(BclNumber)**. Possible errors include:
1234175a4d10SStefan Eßer
1235175a4d10SStefan Eßer    * **BCL_ERROR_INVALID_NUM**
1236175a4d10SStefan Eßer    * **BCL_ERROR_INVALID_CONTEXT**
1237175a4d10SStefan Eßer    * **BCL_ERROR_MATH_NEGATIVE**
1238175a4d10SStefan Eßer    * **BCL_ERROR_MATH_NON_INTEGER**
1239175a4d10SStefan Eßer    * **BCL_ERROR_FATAL_ALLOC_ERR**
1240175a4d10SStefan Eßer
1241175a4d10SStefan Eßer**BclNumber bcl_ifrand_keep(BclNumber** _a_**, size_t** _places_**)**
1242175a4d10SStefan Eßer
1243175a4d10SStefan Eßer:   Returns a random number less than *a* with *places* decimal digits after the
1244175a4d10SStefan Eßer    radix (decimal point). There are no limits on *a* or *places*.
1245175a4d10SStefan Eßer
1246175a4d10SStefan Eßer    *a* must be an integer and non-negative.
124750696a6eSStefan Eßer
124850696a6eSStefan Eßer    This procedure requires a valid current context.
124950696a6eSStefan Eßer
125050696a6eSStefan Eßer    bcl(3) will encode an error in the return value, if there was one. The error
125150696a6eSStefan Eßer    can be queried with **bcl_err(BclNumber)**. Possible errors include:
125250696a6eSStefan Eßer
125350696a6eSStefan Eßer    * **BCL_ERROR_INVALID_NUM**
125450696a6eSStefan Eßer    * **BCL_ERROR_INVALID_CONTEXT**
125550696a6eSStefan Eßer    * **BCL_ERROR_MATH_NEGATIVE**
125650696a6eSStefan Eßer    * **BCL_ERROR_MATH_NON_INTEGER**
125750696a6eSStefan Eßer    * **BCL_ERROR_FATAL_ALLOC_ERR**
125850696a6eSStefan Eßer
125944d4804dSStefan Eßer**BclError bcl_rand_seedWithNum(BclNumber** _n_**)**
126050696a6eSStefan Eßer
126150696a6eSStefan Eßer:   Seeds the PRNG with *n*.
126250696a6eSStefan Eßer
1263175a4d10SStefan Eßer    *n* is consumed.
1264175a4d10SStefan Eßer
1265175a4d10SStefan Eßer    This procedure requires a valid current context.
1266175a4d10SStefan Eßer
1267175a4d10SStefan Eßer    If there was no error, **BCL_ERROR_NONE** is returned. Otherwise, this
1268175a4d10SStefan Eßer    function can return:
1269175a4d10SStefan Eßer
1270175a4d10SStefan Eßer    * **BCL_ERROR_INVALID_NUM**
1271175a4d10SStefan Eßer    * **BCL_ERROR_INVALID_CONTEXT**
1272175a4d10SStefan Eßer
1273175a4d10SStefan Eßer    Note that if **bcl_rand_seed2num(**_void_**)** or
1274175a4d10SStefan Eßer    **bcl_rand_seed2num_err(BclNumber)** are called right after this function,
1275175a4d10SStefan Eßer    they are not guaranteed to return a number equal to *n*.
1276175a4d10SStefan Eßer
1277175a4d10SStefan Eßer**BclError bcl_rand_seedWithNum_keep(BclNumber** _n_**)**
1278175a4d10SStefan Eßer
1279175a4d10SStefan Eßer:   Seeds the PRNG with *n*.
128050696a6eSStefan Eßer
128150696a6eSStefan Eßer    This procedure requires a valid current context.
128250696a6eSStefan Eßer
128350696a6eSStefan Eßer    If there was no error, **BCL_ERROR_NONE** is returned. Otherwise, this
128450696a6eSStefan Eßer    function can return:
128550696a6eSStefan Eßer
128650696a6eSStefan Eßer    * **BCL_ERROR_INVALID_NUM**
128750696a6eSStefan Eßer    * **BCL_ERROR_INVALID_CONTEXT**
128850696a6eSStefan Eßer
128944d4804dSStefan Eßer    Note that if **bcl_rand_seed2num(**_void_**)** or
129050696a6eSStefan Eßer    **bcl_rand_seed2num_err(BclNumber)** are called right after this function,
129150696a6eSStefan Eßer    they are not guaranteed to return a number equal to *n*.
129250696a6eSStefan Eßer
129344d4804dSStefan Eßer**BclError bcl_rand_seed(unsigned char** _seed_**[**_BCL_SEED_SIZE_**])**
129450696a6eSStefan Eßer
129550696a6eSStefan Eßer:   Seeds the PRNG with the bytes in *seed*.
129650696a6eSStefan Eßer
129750696a6eSStefan Eßer    If there was no error, **BCL_ERROR_NONE** is returned. Otherwise, this
129850696a6eSStefan Eßer    function can return:
129950696a6eSStefan Eßer
130050696a6eSStefan Eßer    * **BCL_ERROR_INVALID_CONTEXT**
130150696a6eSStefan Eßer
130244d4804dSStefan Eßer**void bcl_rand_reseed(**_void_**)**
130350696a6eSStefan Eßer
130450696a6eSStefan Eßer:   Reseeds the PRNG with the default reseeding behavior. First, it attempts to
130550696a6eSStefan Eßer    read data from **/dev/urandom** and falls back to **libc**'s **rand()**.
130650696a6eSStefan Eßer
130750696a6eSStefan Eßer    This procedure cannot fail.
130850696a6eSStefan Eßer
130944d4804dSStefan Eßer**BclNumber bcl_rand_seed2num(**_void_**)**
131050696a6eSStefan Eßer
131150696a6eSStefan Eßer:   Returns the current seed of the PRNG as a **BclNumber**.
131250696a6eSStefan Eßer
131350696a6eSStefan Eßer    This procedure requires a valid current context.
131450696a6eSStefan Eßer
131550696a6eSStefan Eßer    bcl(3) will encode an error in the return value, if there was one. The error
131650696a6eSStefan Eßer    can be queried with **bcl_err(BclNumber)**. Possible errors include:
131750696a6eSStefan Eßer
131850696a6eSStefan Eßer    * **BCL_ERROR_INVALID_CONTEXT**
131950696a6eSStefan Eßer    * **BCL_ERROR_FATAL_ALLOC_ERR**
132050696a6eSStefan Eßer
132144d4804dSStefan Eßer**BclRandInt bcl_rand_int(**_void_**)**
132250696a6eSStefan Eßer
132350696a6eSStefan Eßer:   Returns a random integer between **0** and **BC_RAND_MAX** (inclusive).
132450696a6eSStefan Eßer
132550696a6eSStefan Eßer    This procedure cannot fail.
132650696a6eSStefan Eßer
132744d4804dSStefan Eßer**BclRandInt bcl_rand_bounded(BclRandInt** _bound_**)**
132850696a6eSStefan Eßer
132950696a6eSStefan Eßer:   Returns a random integer between **0** and *bound* (exclusive). Bias is
133050696a6eSStefan Eßer    removed before returning the integer.
133150696a6eSStefan Eßer
133250696a6eSStefan Eßer    This procedure cannot fail.
133350696a6eSStefan Eßer
133450696a6eSStefan Eßer## Consumption and Propagation
133550696a6eSStefan Eßer
133650696a6eSStefan EßerSome functions are listed as consuming some or all of their arguments. This
133750696a6eSStefan Eßermeans that the arguments are freed, regardless of if there were errors or not.
133850696a6eSStefan Eßer
133950696a6eSStefan EßerThis is to enable compact code like the following:
134050696a6eSStefan Eßer
134150696a6eSStefan Eßer    BclNumber n = bcl_num_add(bcl_num_mul(a, b), bcl_num_div(c, d));
134250696a6eSStefan Eßer
134350696a6eSStefan EßerIf arguments to those functions were not consumed, memory would be leaked until
134450696a6eSStefan Eßerreclaimed with **bcl_ctxt_freeNums(BclContext)**.
134550696a6eSStefan Eßer
134650696a6eSStefan EßerWhen errors occur, they are propagated through. The result should always be
134750696a6eSStefan Eßerchecked with **bcl_err(BclNumber)**, so the example above should properly
134850696a6eSStefan Eßerbe:
134950696a6eSStefan Eßer
135050696a6eSStefan Eßer    BclNumber n = bcl_num_add(bcl_num_mul(a, b), bcl_num_div(c, d));
1351175a4d10SStefan Eßer    if (bcl_err(n) != BCL_ERROR_NONE) {
135250696a6eSStefan Eßer        // Handle the error.
135350696a6eSStefan Eßer    }
135450696a6eSStefan Eßer
135550696a6eSStefan Eßer# ERRORS
135650696a6eSStefan Eßer
135750696a6eSStefan EßerMost functions in bcl(3) return, directly or indirectly, any one of the error
135850696a6eSStefan Eßercodes defined in **BclError**. The complete list of codes is the following:
135950696a6eSStefan Eßer
136050696a6eSStefan Eßer**BCL_ERROR_NONE**
136150696a6eSStefan Eßer
136250696a6eSStefan Eßer:   Success; no error occurred.
136350696a6eSStefan Eßer
136450696a6eSStefan Eßer**BCL_ERROR_INVALID_NUM**
136550696a6eSStefan Eßer
136650696a6eSStefan Eßer:   An invalid **BclNumber** was given as a parameter.
136750696a6eSStefan Eßer
136850696a6eSStefan Eßer**BCL_ERROR_INVALID_CONTEXT**
136950696a6eSStefan Eßer
137050696a6eSStefan Eßer:   An invalid **BclContext** is being used.
137150696a6eSStefan Eßer
137250696a6eSStefan Eßer**BCL_ERROR_MATH_NEGATIVE**
137350696a6eSStefan Eßer
137450696a6eSStefan Eßer:   A negative number was given as an argument to a parameter that cannot accept
137550696a6eSStefan Eßer    negative numbers, such as for square roots.
137650696a6eSStefan Eßer
137750696a6eSStefan Eßer**BCL_ERROR_MATH_NON_INTEGER**
137850696a6eSStefan Eßer
137950696a6eSStefan Eßer:   A non-integer was given as an argument to a parameter that cannot accept
138050696a6eSStefan Eßer    non-integer numbers, such as for the second parameter of **bcl_num_pow()**.
138150696a6eSStefan Eßer
138250696a6eSStefan Eßer**BCL_ERROR_MATH_OVERFLOW**
138350696a6eSStefan Eßer
138450696a6eSStefan Eßer:   A number that would overflow its result was given as an argument, such as
138550696a6eSStefan Eßer    for converting a **BclNumber** to a **BclBigDig**.
138650696a6eSStefan Eßer
138750696a6eSStefan Eßer**BCL_ERROR_MATH_DIVIDE_BY_ZERO**
138850696a6eSStefan Eßer
138950696a6eSStefan Eßer:   A divide by zero occurred.
139050696a6eSStefan Eßer
139150696a6eSStefan Eßer**BCL_ERROR_PARSE_INVALID_STR**
139250696a6eSStefan Eßer
139350696a6eSStefan Eßer:   An invalid number string was passed to a parsing function.
139450696a6eSStefan Eßer
139550696a6eSStefan Eßer    A valid number string can only be one radix (period). In addition, any
139650696a6eSStefan Eßer    lowercase ASCII letters, symbols, or non-ASCII characters are invalid. It is
139750696a6eSStefan Eßer    allowed for the first character to be a dash. In that case, the number is
139850696a6eSStefan Eßer    considered to be negative.
139950696a6eSStefan Eßer
140050696a6eSStefan Eßer    There is one exception to the above: one lowercase **e** is allowed in the
140150696a6eSStefan Eßer    number, after the radix, if it exists. If the letter **e** exists, the
140250696a6eSStefan Eßer    number is considered to be in scientific notation, where the part before the
140350696a6eSStefan Eßer    **e** is the number, and the part after, which must be an integer, is the
140450696a6eSStefan Eßer    exponent. There can be a dash right after the **e** to indicate a negative
140550696a6eSStefan Eßer    exponent.
140650696a6eSStefan Eßer
140750696a6eSStefan Eßer    **WARNING**: Both the number and the exponent in scientific notation are
140850696a6eSStefan Eßer    interpreted according to the current **ibase**, but the number is still
140950696a6eSStefan Eßer    multiplied by **10\^exponent** regardless of the current **ibase**. For
141050696a6eSStefan Eßer    example, if **ibase** is **16** and bcl(3) is given the number string
141150696a6eSStefan Eßer    **FFeA**, the resulting decimal number will be **2550000000000**, and if
141250696a6eSStefan Eßer    bcl(3) is given the number string **10e-4**, the resulting decimal number
141350696a6eSStefan Eßer    will be **0.0016**.
141450696a6eSStefan Eßer
141550696a6eSStefan Eßer**BCL_ERROR_FATAL_ALLOC_ERR**
141650696a6eSStefan Eßer
141750696a6eSStefan Eßer:   bcl(3) failed to allocate memory.
141850696a6eSStefan Eßer
141950696a6eSStefan Eßer    If clients call **bcl_setAbortOnFatalError()** with an **true** argument,
142050696a6eSStefan Eßer    this error will cause bcl(3) to throw a **SIGABRT**. This behavior can also
142150696a6eSStefan Eßer    be turned off later by calling that same function with a **false** argument.
142250696a6eSStefan Eßer    By default, this behavior is off.
142350696a6eSStefan Eßer
142450696a6eSStefan Eßer    It is highly recommended that client libraries do *not* activate this
142550696a6eSStefan Eßer    behavior.
142650696a6eSStefan Eßer
142750696a6eSStefan Eßer**BCL_ERROR_FATAL_UNKNOWN_ERR**
142850696a6eSStefan Eßer
142950696a6eSStefan Eßer:   An unknown error occurred.
143050696a6eSStefan Eßer
143150696a6eSStefan Eßer    If clients call **bcl_setAbortOnFatalError()** with an **true** argument,
143250696a6eSStefan Eßer    this error will cause bcl(3) to throw a **SIGABRT**. This behavior can also
143350696a6eSStefan Eßer    be turned off later by calling that same function with a **false** argument.
143450696a6eSStefan Eßer    By default, this behavior is off.
143550696a6eSStefan Eßer
143650696a6eSStefan Eßer    It is highly recommended that client libraries do *not* activate this
143750696a6eSStefan Eßer    behavior.
143850696a6eSStefan Eßer
143950696a6eSStefan Eßer# ATTRIBUTES
144050696a6eSStefan Eßer
1441d101cdd6SStefan Eßerbcl(3) is *MT-Safe*: it is safe to call any functions from more than one thread.
1442d101cdd6SStefan EßerHowever, is is *not* safe to pass any data between threads except for strings
1443d101cdd6SStefan Eßerreturned by **bcl_string()**.
144450696a6eSStefan Eßer
1445d101cdd6SStefan Eßerbcl(3) is not *async-signal-safe*. It was not possible to make bcl(3) safe with
1446d101cdd6SStefan Eßersignals and also make it safe with multiple threads. If it is necessary to be
1447d101cdd6SStefan Eßerable to interrupt bcl(3), spawn a separate thread to run the calculation.
144850696a6eSStefan Eßer
144950696a6eSStefan Eßer# PERFORMANCE
145050696a6eSStefan Eßer
145150696a6eSStefan EßerMost bc(1) implementations use **char** types to calculate the value of **1**
145250696a6eSStefan Eßerdecimal digit at a time, but that can be slow. bcl(3) does something
145350696a6eSStefan Eßerdifferent.
145450696a6eSStefan Eßer
145550696a6eSStefan EßerIt uses large integers to calculate more than **1** decimal digit at a time. If
145650696a6eSStefan Eßerbuilt in a environment where **BC_LONG_BIT** (see the **LIMITS** section) is
145750696a6eSStefan Eßer**64**, then each integer has **9** decimal digits. If built in an environment
145850696a6eSStefan Eßerwhere **BC_LONG_BIT** is **32** then each integer has **4** decimal digits. This
145950696a6eSStefan Eßervalue (the number of decimal digits per large integer) is called
146050696a6eSStefan Eßer**BC_BASE_DIGS**.
146150696a6eSStefan Eßer
146250696a6eSStefan EßerIn addition, this bcl(3) uses an even larger integer for overflow checking. This
146350696a6eSStefan Eßerinteger type depends on the value of **BC_LONG_BIT**, but is always at least
146450696a6eSStefan Eßertwice as large as the integer type used to store digits.
146550696a6eSStefan Eßer
146650696a6eSStefan Eßer# LIMITS
146750696a6eSStefan Eßer
146850696a6eSStefan EßerThe following are the limits on bcl(3):
146950696a6eSStefan Eßer
147050696a6eSStefan Eßer**BC_LONG_BIT**
147150696a6eSStefan Eßer
147250696a6eSStefan Eßer:   The number of bits in the **long** type in the environment where bcl(3) was
147350696a6eSStefan Eßer    built. This determines how many decimal digits can be stored in a single
147450696a6eSStefan Eßer    large integer (see the **PERFORMANCE** section).
147550696a6eSStefan Eßer
147650696a6eSStefan Eßer**BC_BASE_DIGS**
147750696a6eSStefan Eßer
147850696a6eSStefan Eßer:   The number of decimal digits per large integer (see the **PERFORMANCE**
147950696a6eSStefan Eßer    section). Depends on **BC_LONG_BIT**.
148050696a6eSStefan Eßer
148150696a6eSStefan Eßer**BC_BASE_POW**
148250696a6eSStefan Eßer
148350696a6eSStefan Eßer:   The max decimal number that each large integer can store (see
148450696a6eSStefan Eßer    **BC_BASE_DIGS**) plus **1**. Depends on **BC_BASE_DIGS**.
148550696a6eSStefan Eßer
148650696a6eSStefan Eßer**BC_OVERFLOW_MAX**
148750696a6eSStefan Eßer
148850696a6eSStefan Eßer:   The max number that the overflow type (see the **PERFORMANCE** section) can
148950696a6eSStefan Eßer    hold. Depends on **BC_LONG_BIT**.
149050696a6eSStefan Eßer
149150696a6eSStefan Eßer**BC_BASE_MAX**
149250696a6eSStefan Eßer
149350696a6eSStefan Eßer:   The maximum output base. Set at **BC_BASE_POW**.
149450696a6eSStefan Eßer
149550696a6eSStefan Eßer**BC_SCALE_MAX**
149650696a6eSStefan Eßer
149750696a6eSStefan Eßer:   The maximum **scale**. Set at **BC_OVERFLOW_MAX-1**.
149850696a6eSStefan Eßer
149950696a6eSStefan Eßer**BC_NUM_MAX**
150050696a6eSStefan Eßer
150150696a6eSStefan Eßer:   The maximum length of a number (in decimal digits), which includes digits
150250696a6eSStefan Eßer    after the decimal point. Set at **BC_OVERFLOW_MAX-1**.
150350696a6eSStefan Eßer
150450696a6eSStefan Eßer**BC_RAND_MAX**
150550696a6eSStefan Eßer
150650696a6eSStefan Eßer:   The maximum integer (inclusive) returned by the **bcl_rand_int()** function.
150750696a6eSStefan Eßer    Set at **2\^BC_LONG_BIT-1**.
150850696a6eSStefan Eßer
150950696a6eSStefan EßerExponent
151050696a6eSStefan Eßer
151150696a6eSStefan Eßer:   The maximum allowable exponent (positive or negative). Set at
151250696a6eSStefan Eßer    **BC_OVERFLOW_MAX**.
151350696a6eSStefan Eßer
151450696a6eSStefan EßerThese limits are meant to be effectively non-existent; the limits are so large
151550696a6eSStefan Eßer(at least on 64-bit machines) that there should not be any point at which they
151650696a6eSStefan Eßerbecome a problem. In fact, memory should be exhausted before these limits should
151750696a6eSStefan Eßerbe hit.
151850696a6eSStefan Eßer
151950696a6eSStefan Eßer# SEE ALSO
152050696a6eSStefan Eßer
152150696a6eSStefan Eßerbc(1) and dc(1)
152250696a6eSStefan Eßer
152350696a6eSStefan Eßer# STANDARDS
152450696a6eSStefan Eßer
152578bc019dSStefan Eßerbcl(3) is compliant with the arithmetic defined in the IEEE Std 1003.1-2017
152678bc019dSStefan Eßer(“POSIX.1-2017”) specification at
152778bc019dSStefan Eßerhttps://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html for bc(1).
152850696a6eSStefan Eßer
152950696a6eSStefan EßerNote that the specification explicitly says that bc(1) only accepts numbers that
153050696a6eSStefan Eßeruse a period (**.**) as a radix point, regardless of the value of
153150696a6eSStefan Eßer**LC_NUMERIC**. This is also true of bcl(3).
153250696a6eSStefan Eßer
153350696a6eSStefan Eßer# BUGS
153450696a6eSStefan Eßer
15354fca8e0fSStefan EßerNone are known. Report bugs at https://git.gavinhoward.com/gavin/bc.
153650696a6eSStefan Eßer
153750696a6eSStefan Eßer# AUTHORS
153850696a6eSStefan Eßer
15394fca8e0fSStefan EßerGavin D. Howard <gavin@gavinhoward.com> and contributors.
1540