contrib/bc: import version 6.5.0

This release that fixes an infinite loop bug in the (non-standard)
extended math library functions root() and cbrt(), fixes a bug with
BC_LINE_LENGTH=0, and adds the fib() function to the extended math
library to calculate Fibonacci numbers.

(cherry picked from commit 438a1101dc1f687928cdbe02cd7817a88a24f42f)

MFC after:      3 days
This commit is contained in:
Stefan Eßer 2023-04-30 19:53:26 +02:00
parent 6a23843a4a
commit 8c48f4c537
13 changed files with 473 additions and 8 deletions

View File

@ -1,5 +1,11 @@
# News # News
## 6.5.0
This is a production release that fixes an infinite loop bug in `root()` and
`cbrt()`, fixes a bug with `BC_LINE_LENGTH=0`, and adds the `fib()` function to
the extended math library to calculate Fibonacci numbers.
## 6.4.0 ## 6.4.0
This is a production release that fixes a `read()`/`?` bug and adds features to This is a production release that fixes a `read()`/`?` bug and adds features to

View File

@ -93,6 +93,18 @@ define comb(n,r){
scale=s scale=s
return f return f
} }
define fib(n){
auto i,t,p,r
if(!n)return 0
n=abs(n)$
t=1
for (i=1;i<n;++i){
r=p
p=t
t+=r
}
return t
}
define log(x,b){ define log(x,b){
auto p,s auto p,s
s=scale s=scale
@ -106,7 +118,7 @@ define log(x,b){
define l2(x){return log(x,2)} define l2(x){return log(x,2)}
define l10(x){return log(x,A)} define l10(x){return log(x,A)}
define root(x,n){ define root(x,n){
auto s,m,r,q,p auto s,t,m,r,q,p
if(n<0)sqrt(n) if(n<0)sqrt(n)
n=n$ n=n$
if(n==0)x/n if(n==0)x/n
@ -114,13 +126,17 @@ define root(x,n){
if(n==2)return sqrt(x) if(n==2)return sqrt(x)
s=scale s=scale
scale=0 scale=0
if(x<0&&n%2==0)sqrt(x) if(x<0&&n%2==0){
scale=s+2 scale=s
sqrt(x)
}
scale=s+scale(x)+5
t=s+5
m=(x<0) m=(x<0)
x=abs(x) x=abs(x)
p=n-1 p=n-1
q=A^ceil((length(x$)/n)$,0) q=A^ceil((length(x$)/n)$,0)
while(r!=q){ while(r@t!=q@t){
r=q r=q
q=(p*r+x/r^p)/n q=(p*r+x/r^p)/n
} }

View File

@ -37,6 +37,6 @@
#define BC_VERSION_H #define BC_VERSION_H
/// The current version. /// The current version.
#define VERSION 6.4.0 #define VERSION 6.5.0
#endif // BC_VERSION_H #endif // BC_VERSION_H

View File

@ -622,8 +622,12 @@ bc_vm_envLen(const char* var)
if (num) if (num)
{ {
// Parse it and clamp it if needed. // Parse it and clamp it if needed.
len = (size_t) atoi(lenv) - 1; len = (size_t) strtol(lenv, NULL, 10);
if (len == 1 || len >= UINT16_MAX) len = BC_NUM_PRINT_WIDTH; if (len != 0)
{
len -= 1;
if (len < 2 || len >= UINT16_MAX) len = BC_NUM_PRINT_WIDTH;
}
} }
// Set the default. // Set the default.
else len = BC_NUM_PRINT_WIDTH; else len = BC_NUM_PRINT_WIDTH;

View File

@ -34,6 +34,7 @@ arctangent
sine sine
cosine cosine
bessel bessel
fib
arrays arrays
misc misc
misc1 misc1

View File

@ -0,0 +1,31 @@
fib(0)
fib(1)
fib(2)
fib(3)
fib(4)
fib(5)
fib(6)
fib(7)
fib(8)
fib(9)
fib(10)
fib(11)
fib(12)
fib(13)
fib(14)
fib(15)
fib(16)
fib(17)
fib(18)
fib(19)
fib(20)
fib(21)
fib(22)
fib(23)
fib(24)
fib(25)
fib(26)
fib(27)
fib(28)
fib(29)
fib(30)

View File

@ -0,0 +1,31 @@
0
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
6765
10946
17711
28657
46368
75025
121393
196418
317811
514229
832040

View File

@ -4,6 +4,7 @@ subtract.bc
add.bc add.bc
print.bc print.bc
parse.bc parse.bc
root.bc
array.bc array.bc
array2.bc array2.bc
atan.bc atan.bc

View File

@ -0,0 +1,100 @@
.464158883361277889241007635091
.215443469003188372175929356651
.100000000000000000000000000000
.046415888336127788924100763509
.021544346900318837217592935665
.010000000000000000000000000000
.004641588833612778892410076350
.002154434690031883721759293566
.001000000000000000000000000000
.000464158883361277889241007635
.000215443469003188372175929356
.000100000000000000000000000000
.000046415888336127788924100763
.000021544346900318837217592935
.000010000000000000000000000000
.000004641588833612778892410076
.000002154434690031883721759293
.000001000000000000000000000000
.000000464158883361277889241007
.000000215443469003188372175929
.000000100000000000000000000000
.000000046415888336127788924100
.000000021544346900318837217592
.000000010000000000000000000000
.000000004641588833612778892410
.000000002154434690031883721759
.000000001000000000000000000000
.000000000464158883361277889241
.000000000215443469003188372175
.000000000100000000000000000000
.000000000046415888336127788924
.000000000021544346900318837217
.000000000010000000000000000000
.000000000004641588833612778892
.000000000002154434690031883721
.000000000001000000000000000000
.000000000000464158883361277889
.000000000000215443469003188372
.000000000000100000000000000000
.000000000000046415888336127788
.000000000000021544346900318837
.000000000000010000000000000000
.000000000000004641588833612778
.000000000000002154434690031883
.000000000000001000000000000000
.000000000000000464158883361277
.000000000000000215443469003188
.000000000000000100000000000000
.000000000000000046415888336127
.000000000000000021544346900318
.000000000000000010000000000000
.000000000000000004641588833612
.000000000000000002154434690031
.000000000000000001000000000000
.000000000000000000464158883361
.000000000000000000215443469003
.000000000000000000100000000000
.000000000000000000046415888336
.000000000000000000021544346900
.000000000000000000010000000000
.000000000000000000004641588833
.000000000000000000002154434690
.000000000000000000001000000000
.000000000000000000000464158883
.000000000000000000000215443469
.000000000000000000000100000000
.000000000000000000000046415888
.000000000000000000000021544346
.000000000000000000000010000000
.000000000000000000000004641588
.000000000000000000000002154434
.000000000000000000000001000000
.000000000000000000000000464158
.000000000000000000000000215443
.000000000000000000000000100000
.000000000000000000000000046415
.000000000000000000000000021544
.000000000000000000000000010000
.000000000000000000000000004641
.000000000000000000000000002154
.000000000000000000000000001000
.000000000000000000000000000464
.000000000000000000000000000215
.000000000000000000000000000100
.000000000000000000000000000046
.000000000000000000000000000021
.000000000000000000000000000010
.000000000000000000000000000004
.000000000000000000000000000002
.000000000000000000000000000001
0
0
0
0
0
0
0
0
0
0

View File

@ -0,0 +1,19 @@
scale = 30
s = 1 >> 1
for (i = 0; i < 100; ++i)
{
cbrt(s)
s >>= 1
}
s = 1 >> 1
for (i = 0; i < 155; ++i)
{
root(s, 5)
s >>= 1
}
halt

View File

@ -0,0 +1,255 @@
.464158883361277889241007635091
.215443469003188372175929356651
.100000000000000000000000000000
.046415888336127788924100763509
.021544346900318837217592935665
.010000000000000000000000000000
.004641588833612778892410076350
.002154434690031883721759293566
.001000000000000000000000000000
.000464158883361277889241007635
.000215443469003188372175929356
.000100000000000000000000000000
.000046415888336127788924100763
.000021544346900318837217592935
.000010000000000000000000000000
.000004641588833612778892410076
.000002154434690031883721759293
.000001000000000000000000000000
.000000464158883361277889241007
.000000215443469003188372175929
.000000100000000000000000000000
.000000046415888336127788924100
.000000021544346900318837217592
.000000010000000000000000000000
.000000004641588833612778892410
.000000002154434690031883721759
.000000001000000000000000000000
.000000000464158883361277889241
.000000000215443469003188372175
.000000000100000000000000000000
.000000000046415888336127788924
.000000000021544346900318837217
.000000000010000000000000000000
.000000000004641588833612778892
.000000000002154434690031883721
.000000000001000000000000000000
.000000000000464158883361277889
.000000000000215443469003188372
.000000000000100000000000000000
.000000000000046415888336127788
.000000000000021544346900318837
.000000000000010000000000000000
.000000000000004641588833612778
.000000000000002154434690031883
.000000000000001000000000000000
.000000000000000464158883361277
.000000000000000215443469003188
.000000000000000100000000000000
.000000000000000046415888336127
.000000000000000021544346900318
.000000000000000010000000000000
.000000000000000004641588833612
.000000000000000002154434690031
.000000000000000001000000000000
.000000000000000000464158883361
.000000000000000000215443469003
.000000000000000000100000000000
.000000000000000000046415888336
.000000000000000000021544346900
.000000000000000000010000000000
.000000000000000000004641588833
.000000000000000000002154434690
.000000000000000000001000000000
.000000000000000000000464158883
.000000000000000000000215443469
.000000000000000000000100000000
.000000000000000000000046415888
.000000000000000000000021544346
.000000000000000000000010000000
.000000000000000000000004641588
.000000000000000000000002154434
.000000000000000000000001000000
.000000000000000000000000464158
.000000000000000000000000215443
.000000000000000000000000100000
.000000000000000000000000046415
.000000000000000000000000021544
.000000000000000000000000010000
.000000000000000000000000004641
.000000000000000000000000002154
.000000000000000000000000001000
.000000000000000000000000000464
.000000000000000000000000000215
.000000000000000000000000000100
.000000000000000000000000000046
.000000000000000000000000000021
.000000000000000000000000000010
.000000000000000000000000000004
.000000000000000000000000000002
.000000000000000000000000000001
0
0
0
0
0
0
0
0
0
0
.630957344480193249434360136622
.398107170553497250770252305087
.251188643150958011108503206779
.158489319246111348520210137339
.100000000000000000000000000000
.063095734448019324943436013662
.039810717055349725077025230508
.025118864315095801110850320677
.015848931924611134852021013733
.010000000000000000000000000000
.006309573444801932494343601366
.003981071705534972507702523050
.002511886431509580111085032067
.001584893192461113485202101373
.001000000000000000000000000000
.000630957344480193249434360136
.000398107170553497250770252305
.000251188643150958011108503206
.000158489319246111348520210137
.000100000000000000000000000000
.000063095734448019324943436013
.000039810717055349725077025230
.000025118864315095801110850320
.000015848931924611134852021013
.000010000000000000000000000000
.000006309573444801932494343601
.000003981071705534972507702523
.000002511886431509580111085032
.000001584893192461113485202101
.000001000000000000000000000000
.000000630957344480193249434360
.000000398107170553497250770252
.000000251188643150958011108503
.000000158489319246111348520210
.000000100000000000000000000000
.000000063095734448019324943436
.000000039810717055349725077025
.000000025118864315095801110850
.000000015848931924611134852021
.000000010000000000000000000000
.000000006309573444801932494343
.000000003981071705534972507702
.000000002511886431509580111085
.000000001584893192461113485202
.000000001000000000000000000000
.000000000630957344480193249434
.000000000398107170553497250770
.000000000251188643150958011108
.000000000158489319246111348520
.000000000100000000000000000000
.000000000063095734448019324943
.000000000039810717055349725077
.000000000025118864315095801110
.000000000015848931924611134852
.000000000010000000000000000000
.000000000006309573444801932494
.000000000003981071705534972507
.000000000002511886431509580111
.000000000001584893192461113485
.000000000001000000000000000000
.000000000000630957344480193249
.000000000000398107170553497250
.000000000000251188643150958011
.000000000000158489319246111348
.000000000000100000000000000000
.000000000000063095734448019324
.000000000000039810717055349725
.000000000000025118864315095801
.000000000000015848931924611134
.000000000000010000000000000000
.000000000000006309573444801932
.000000000000003981071705534972
.000000000000002511886431509580
.000000000000001584893192461113
.000000000000001000000000000000
.000000000000000630957344480193
.000000000000000398107170553497
.000000000000000251188643150958
.000000000000000158489319246111
.000000000000000100000000000000
.000000000000000063095734448019
.000000000000000039810717055349
.000000000000000025118864315095
.000000000000000015848931924611
.000000000000000010000000000000
.000000000000000006309573444801
.000000000000000003981071705534
.000000000000000002511886431509
.000000000000000001584893192461
.000000000000000001000000000000
.000000000000000000630957344480
.000000000000000000398107170553
.000000000000000000251188643150
.000000000000000000158489319246
.000000000000000000100000000000
.000000000000000000063095734448
.000000000000000000039810717055
.000000000000000000025118864315
.000000000000000000015848931924
.000000000000000000010000000000
.000000000000000000006309573444
.000000000000000000003981071705
.000000000000000000002511886431
.000000000000000000001584893192
.000000000000000000001000000000
.000000000000000000000630957344
.000000000000000000000398107170
.000000000000000000000251188643
.000000000000000000000158489319
.000000000000000000000100000000
.000000000000000000000063095734
.000000000000000000000039810717
.000000000000000000000025118864
.000000000000000000000015848931
.000000000000000000000010000000
.000000000000000000000006309573
.000000000000000000000003981071
.000000000000000000000002511886
.000000000000000000000001584893
.000000000000000000000001000000
.000000000000000000000000630957
.000000000000000000000000398107
.000000000000000000000000251188
.000000000000000000000000158489
.000000000000000000000000100000
.000000000000000000000000063095
.000000000000000000000000039810
.000000000000000000000000025118
.000000000000000000000000015848
.000000000000000000000000010000
.000000000000000000000000006309
.000000000000000000000000003981
.000000000000000000000000002511
.000000000000000000000000001584
.000000000000000000000000001000
.000000000000000000000000000630
.000000000000000000000000000398
.000000000000000000000000000251
.000000000000000000000000000158
.000000000000000000000000000100
.000000000000000000000000000063
.000000000000000000000000000039
.000000000000000000000000000025
.000000000000000000000000000015
.000000000000000000000000000010
.000000000000000000000000000006
.000000000000000000000000000003
.000000000000000000000000000002
.000000000000000000000000000001
.000000000000000000000000000001
0
0
0
0
0

View File

@ -1,5 +1,6 @@
engineering engineering
lib2 lib2
fib
places places
rand rand
scientific scientific

View File

@ -132,7 +132,7 @@ fi
# Skip the tests that require extra math if we don't have it. # Skip the tests that require extra math if we don't have it.
if [ "$run_extra_tests" -eq 0 ]; then if [ "$run_extra_tests" -eq 0 ]; then
if [ "$f" = "rand.bc" ]; then if [ "$f" = "rand.bc" ] || [ "$f" = "root.bc" ]; then
printf 'Skipping %s script: %s\n' "$d" "$f" printf 'Skipping %s script: %s\n' "$d" "$f"
exit 0 exit 0
fi fi