-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbcalc_ext.bc
11318 lines (10252 loc) · 341 KB
/
bcalc_ext.bc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# bcalc_ext.bc -- bash bc wrapper extensions
# jun/2022 by mountaineerbr
# 8 888888888o ,o888888o. 8 8888888888 `8.`8888. ,8'
# 8 8888 `88. 8888 `88. 8 8888 `8.`8888. ,8'
# 8 8888 `88 ,8 8888 `8. 8 8888 `8.`8888. ,8'
# 8 8888 ,88 88 8888 8 8888 `8.`8888.,8'
# 8 8888. ,88' 88 8888 8 888888888888 `8.`88888'
# 8 8888888888 88 8888 8 8888 .88.`8888.
# 8 8888 `88. 88 8888 8 8888 .8'`8.`8888.
# 8 8888 88 `8 8888 .8' 8 8888 .8' `8.`8888.
# 8 8888 ,88' 8888 ,88' 8 8888 .8' `8.`8888.
# 8 888888888P `8888888P' 8 888888888888 .8' `8.`8888.
#
# 8888888 8888888888 8 8888888888 b. 8
# 8 8888 8 8888 888o. 8
# 8 8888 8 8888 Y88888o. 8
# 8 8888 8 8888 .`Y888888o. 8
# 8 8888 8 888888888888 8o. `Y888888o. 8
# 8 8888 8 8888 8`Y8o. `Y88888o8
# 8 8888 8 8888 8 `Y8o. `Y8888
# 8 8888 8 8888 8 `Y8o. `Y8
# 8 8888 8 8888 8 `Y8o.`
# 8 8888 8 888888888888 8 `Yo
#
# d888888o. 8 8888 ,o888888o. b. 8 d888888o.
# .`8888:' `88. 8 8888 . 8888 `88. 888o. 8 .`8888:' `88.
# 8.`8888. Y8 8 8888 ,8 8888 `8b Y88888o. 8 8.`8888. Y8
# `8.`8888. 8 8888 88 8888 `8b .`Y888888o. 8 `8.`8888.
# `8.`8888. 8 8888 88 8888 88 8o. `Y888888o. 8 `8.`8888.
# `8.`8888. 8 8888 88 8888 88 8`Y8o. `Y88888o8 `8.`8888.
# `8.`8888. 8 8888 88 8888 ,8P 8 `Y8o. `Y8888 `8.`8888.
# 8b `8.`8888. 8 8888 `8 8888 ,8P 8 `Y8o. `Y8 8b `8.`8888.
# `8b. ;8.`8888 8 8888 ` 8888 ,88' 8 `Y8o.` `8b. ;8.`8888
# `Y8888P ,88P' 8 8888 `8888888P' 8 `Yo `Y8888P ,88P'
### MaxScale - Get the maximum scale from your GNU BC
### BC is currently only 32bit in all cases.
### The package manager recognizes that it might have to install 32bit libraries for a 64 bit environment, but continue to be 32 bits libraries.
### That is why your maximum scale is always 2147483647 (though is safer to use 2147483647, because by reason of the minus or the decimal symbol in case of negative or decimal operations by reason of the minus and decimal symbol) 1111111111111111111111111111110 bits (check https://github.com/abueesp/mathstuffinc/blob/master/bitscalc.py).
### If you try to add just 1 bit more, you will get a fatal error 'Out of memory for malloc'.
### Which explain that more memory can't be allocated, even if there is not an overflow because you are running a >32 bits computer.
### If you want to use bc in bash (such as $(echo "scale=2147483646;1/6" | bc) | tee -a division) you will get 'bash: xrealloc: cannot allocate 18446744071562067968 bytes;' (2^31 digits represent A LOT of bytes for a normal 32/64 bits computer memory!) and 'Out of memory malloc' in case you want to assign the value to a variable or simply copy and paste outside bc. However, you can still operate with such a large number inside bc without problems while you don't exceed 2^31 digits. Isn't that awesome?"
#scale=2147483646
/*************************************
bc program: scientific_constants.bc
author: Steffen Brinkmann
e-mail: [email protected]
comments: - published uner the GPL
- contains particle masses, basic constants, such as
speed of light in the vacuum and the gravitational
constant.
- scale is set to 100
From: http://x-bc.sourceforge.net/scientific_constants_bc.html
*************************************/
scale=100
/* -- I -- particle masses: */
mp = 1.6726231*10^(-27) /* proton rest mass in kg */
mn = 1.6749286*10^(-27) /* neutron rest mass in kg */
me = 9.1093897*10^(-31) /* electron rest mass in kg */
mpev = 938.28*10^6 /* proton rest mass in eV */
mnev = 939.57*10^6 /* neutron rest mass in eV */
meev = .511*10^6 /* electron rest mass in eV */
/* -- II -- general constants: */
c = 299792458 /* speed of light in the vacuum in m/s */
h = 6.6260755*10^(-34) /* Planck constant in Js */
hbar = 1.05457266*10^(-34) /* Planck constant divided by 2*pi in Js */
kb = 1.380658*10^(-23) /* boltzmann constant in J/K */
ec = 1.60217733*10^(-19) /* elementary charge in C */
na = 6.0221367*10^23 /* avogadro number in 1/mol */
epsilon0 = 8.854187817*10^(-12) /* dielectric constant in C^2/Jm */
mu0 = 12.566370614 /* permeability of vacuum in T^2*m^3/J */
alpha = .00729735307 /* fine structure constant */
mub = 9.2740154*10^(-24) /* Bohr magneton J/T */
mun = 5.0507866*10^(-27) /* nuclear magneton J/T */
ge = 2.002319304386 /* free electron g factor */
gammae = 1.7608592*10^11 /* free electron gyromagnetic ratio in T/s */
mue = -9.2847701*10^(-24) /* electron magnetic moment */
gammap = 2.67515255*10^8 /* proton gyromagnetic ratio in water in T/s */
mup = 1.41060761*10^(-26) /* proton magnetic moment in J/T */
amu = 1.66057*10^(-27) /* atomic mass unit in kg */
a0 = 5.29177*10^(-11) /* Bohr radius in m */
re = 2.81792*10^(-15) /* electron radius in m */
vmol = 22.41383 /* molar volume in l/mol */
gh = 5.585 /* proton g factor (lande factor) */
grav = 6.673*10^(-11) /* gravitational constant m^3/kg*s^2 */
g = 9.80665 /* acceleration due to gravity on surface of earth in m/s^2 */
lambdac = 2.42631*10^(-12) /* compton wavelength of the electron m */
#!/usr/bin/bc
# bc integer functions I've found useful
# while systems programming in the unix environment.
# License: LGPLv2
# Author:
# http://www.pixelbeat.org/
# Notes:
# I only use bc when python is not available.
# Personally I have this file in ~/bin/bc so
# that I can just invoke bc as normal and have these
# extra functions available.
# Changes:
# V0.1, 11 Apr 2007, Initial release
define min(x,y) {
if (x<y) return x
return y
}
define max(x,y) {
if (x>y) return x
return y
}
define abs(x) {
if (x<0) return -x
return x
}
/* take integer part */
define int(x) {
auto old_scale /* variables global by default */
old_scale=scale /* scale is global */
scale=0; ret=x/1
scale=old_scale
return ret
}
/* round to nearest integer */
define round(x) {
if (x<0) x-=.5 else x+=.5
return int(x)
}
/* smallest integer >= arg */
define ceil(x) {
auto intx
intx=int(x)
if (intx<x) intx+=1
return intx
}
/* largest integer <= arg */
define floor(x) {
return -ceil(-x)
}
/* round x to previous multiple of y */
define round_down(x,y) {
return y*floor(x/y)
}
/* round x to next multiple of y */
define round_up(x,y) {
return y*ceil(x/y)
}
/* round x to nearest multiple of y */
define round_to(x,y) {
return y*round(x/y)
}
/* Greatest Common Divisor or Highest Common Factor of x and y */
/* Note when people say Lowest Common Denominator they usually mean this */
define gcd(x,y) {
if (y==0) return x
return gcd(y,x%y) /* anything that divides into x and y also divides into the remainder of x/y */
}
/* Lowest Common Multiple of x and y */
/* Lowest Common Denominator of fractions is LCM of the denominators */
define lcm(x,y) {
return (x*y/gcd(x,y))
}
/********************************
bc program: extensions.bc
author: Steffen Brinkmann
e-mail: [email protected]
comments: - published under the GPL
- contains functions of trigonometry, exponential functions,
functions of number theory and some mathematical constants
so far.
From: http://x-bc.sourceforge.net/extensions_bc.html
*********************************/
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/***********************************************
--I-- constants
1.- pi : defined as 4*atan(1)
2.- e : defined as e(1)
--II-- trigonometry:
1.- sin(x) : returns sine of x
2.- cos(x) : returns cosine of x
3.- atan(x) : returns arc tangent of x
4.- tan(x) : returns tangent of x
5.- asin(x) : returns arc sine of x
6.- acos(x) : returns arc cosine of x
7.- cot(x) : returns cotangent of x
8.- acot(x) : returns arc cotangent of x
9.- sec(x) : returns secans of x
10.- cosec(x),csc(x) : returns cosecans of x
11.- asec(x) : returns arc secans of x
12.- acosec(x),ascs(x) : returns arc cosecans of x
13.- sinh(x) : returns hyperbolical sine of x
14.- cosh(x) : returns hyperbolical cosine of x
15.- tanh(x) : returns hyperbolical tangent of x
16.- coth(x) : returns hyperbolical cotangent of x
17.- asinh(x) : returns arc hyperbolical sine of x
18.- acosh(x) : returns arc hyperbolical cosine of x
19.- atanh(x) : returns arc hyperbolical tangent of x
20.- acoth(x) : returns arc hyperbolical cotangent of x
21.- sech(x) : returns secans hyperbolicus of x
22.- cosech(x),csch(x) : returns cosecans hyperbolicus of x
23.- asech(x) : returns arc secans hyperbolicus of x
24.- acosech(x),acsch(x) : returns arc cosecans hyperbolicus of x
--II-- exponential functions:
1.- ln(x) : returns natural logarithm of x
2.- log(x) : returns logarithm (base 10) of x
3.- lb(x),ld(x) : returns logarithm (base 2) of x
4.- pow(x,y) : returns x to the power of y
--III-- number theory:
1.- abs(n) : returns absolute value of n
2.- mod(a,b) : returns a modulo b
3.- factorize(n),fac(n) : prints primefactors of n,
returns number of primefactors
returns 0 if n is a prime number
returns -1 if n is +-1 or 0
CAUTION: 13-digit number may need 30 s
4.- factorial(n),f(n) : returns n factorial
5.- gcd(a,b) : returns the greatest common divisor of a and b
6.- lcm(a,b) : returns the least common multiple of a and b
7.- bessel(n,x) : returns the Bessel function order n of x
************************************************/
pi=4*a(1)
e=e(1)
define sin(x)
{
return (s(x))
}
define cos(x)
{
return (c(x))
}
define atan(x)
{
return (a(x))
}
define tan(x)
{
return (s(x)/c(x))
}
define asin(x)
{
if(x==1) return(pi/2)
if(x==-1) return(-pi/2)
return(a(x/sqrt(1-(x^2))))
}
define acos(x)
{
if(x==1) return(0)
if(x==-1) return(pi)
return(pi/2-a(x/sqrt(1-(x^2))))
}
define cot(x)
{
return(c(x)/s(x))
}
define acot(x)
{
return(pi/2-a(x))
}
define sec(x)
{
return(1/c(x))
}
define cosec(x)
{
return(1/s(x))
}
define csc(x)
{
return(1/s(x))
}
define asec(x)
{
return(acos(1/x))
}
define acosec(x)
{
return(asin(1/x))
}
define acsc(x)
{
return(asin(1/x))
}
define sinh(x)
{
return((e(x)-e(-x))/2)
}
define cosh(x)
{
return((e(x)+e(-x))/2)
}
define tanh(x)
{
return((e(x)-e(-x))/e(x)+e(-x))
}
define coth(x)
{
return((e(x)+e(-x))/e(x)-e(-x))
}
define asinh(x)
{
return(l(x + sqrt(x^2 + 1)))
}
define acosh(x)
{
return(l(x + sqrt(x^2 - 1)))
}
define atanh(x)
{
return((l(1 + x) - l(1 - x))/2)
}
define acoth(x)
{
return(atanh(1/x))
}
define sech(x)
{
return(1/cosh(x))
}
define cosech(x)
{
return(1/sinh(x))
}
define csch(x)
{
return(1/sinh(x))
}
define asech(x)
{
return(acosh(1/x))
}
define acosech(x)
{
return(asinh(1/x))
}
define acsch(x)
{
return(asinh(1/x))
}
/************************/
define ln(x)
{
return(l(x))
}
define log(x)
{
return(l(x)/l(10))
}
define lb(x)
{
return(l(x)/l(2))
}
define ld(x)
{
return(lb(x))
}
define pow(x,y)
{
return(e(y*l(x)))
}
/************************/
define abs(n){
if(n>=0) return(n)
return(-n)
}
define mod(a,b){
auto c,tmp_scale
tmp_scale=scale(sqrt(2))
scale=0
c=a%b
scale=tmp_scale
if(a>=0) return(c)
if(c==0) return(0)
return(c+b)
}
define fac(n)
{
auto tmp,i,factors
if(abs(n)<=1)
{
print abs(n),"\nnumber of factors: "
return(0)
}
if(abs(n)==2)
{
print 2,"\nnumber of factors: "
return(1)
}
tmp=n
while(mod(tmp,2)==0)
{
print 2," "
tmp/=2
factors+=1
}
if(prime[0]==2) /*primenumbers.bc is loaded*/
{
i=0
while((prime[i]*prime[i])<=(n+1))
{
if(mod(tmp,prime[i])==0)
{
print prime[i]," "
tmp/=prime[i]
factors+=1
}else{
i+=1
if(i>65535)
{
break
}
}
}
}
if(i>65535)
{
i=prime[65535]
}else
{
i=3
}
while((i*i)<=(n+1))
{
if(mod(tmp,i)==0)
{
print i," "
tmp/=i
factors+=1
}else{
i+=2
}
}
if(tmp!=1)
{
factors+=1
print tmp," " /*BUG: prints zeros after factor*/
}
print "\n"
print "number of factors: "
return(factors)
}
define factorize(n)
{
return (fac(n))
}
define f(n)
{
if (n <= 1) return (1);
return (f(n-1) * n);
}
define factorial(n)
{
return(f(n))
}
define gcd(m,n){
auto a,b,c,tmp_scale
a=abs(m) /* a=r[0] */
if(n==0) return(a)
b=abs(n) /* b=r[1] */
/*tmp_scale=scale(sqrt(2))*/
/*c=a%b /* c=r[2]=r[0] mod(r[1]) */
c=mod(a,b)
while(c>0){
a=b
b=c
/*(c=a%b /* c=r[j]=r[j-2] mod(r[j-1]) */
c=mod(a,b)
}
/*scale=tmp_scale*/
return(b)
}
define lcm(a,b){
auto g
g=gcd(a,b)
if(g==0) return(0)
return(abs(a*b)/g)
}
define bessel(n,x){
return(j(n,x))
}
#!/usr/local/bin/bc -l
### Funcs.BC - a large number of functions for use with GNU BC
## Not to be regarded as suitable for any purpose
## Not guaranteed to return correct answers
scale=50;
define pi() {
auto s;
if(scale==(s=scale(pi_)))return pi_
if(scale<s)return pi_/1
scale+=5;pi_=a(1)*4;scale-=5
return pi_/1
}
e = e(1);
define phi(){return((1+sqrt(5))/2)} ; phi = phi()
define psi(){return((1-sqrt(5))/2)} ; psi = psi()
# Reset base to ten
obase=ibase=A;
## Integer and Rounding
# Round to next integer nearest 0: -1.99 -> 1, 0.99 -> 0
define int(x) { auto os;os=scale;scale=0;x/=1;scale=os;return(x) }
# Round down to integer below x
define floor(x) {
auto os,xx;os=scale;scale=0
xx=x/1;if(xx>x).=xx--
scale=os;return(xx)
}
# Round up to integer above x
define ceil(x) {
auto os,xx;x=-x;os=scale;scale=0
xx=x/1;if(xx>x).=xx--
scale=os;return(-xx)
}
# Fractional part of x: 12.345 -> 0.345
define frac(x) {
auto os,xx;os=scale;scale=0
xx=x/1;if(xx>x).=xx--
scale=os;return(x-xx)
}
# Absolute value of x
define abs(x) { if(x<0)return(-x)else return(x) }
# Sign of x
define sgn(x) { if(x<0)return(-1)else if(x>0)return(1);return(0) }
# Round x up to next multiple of y
define round_up( x,y) { return(y*ceil( x/y )) }
# Round x down to previous multiple of y
define round_down(x,y) { return(y*floor(x/y )) }
# Round x to the nearest multiple of y
define round( x,y) {
auto os,oib;
os=scale;oib=ibase
.=scale++;ibase=A
y*=floor(x/y+.5)
ibase=oib;scale=os
return y
}
# Find the remainder of x/y
define int_remainder(x,y) {
auto os;
os=scale;scale=0
x/=1;y/=1;x%=y
scale=os
return(x)
}
define remainder(x,y) {
os=scale;scale=0
if(x==x/1&&y==y/1){scale=os;return int_remainder(x,y)}
scale=os
return(x-round_down(x,y))
}
# Greatest common divisor of x and y
define int_gcd(x,y) {
auto r,os;
os=scale;scale=0
x/=1;y/=1
while(y>0){r=x%y;x=y;y=r}
scale=os
return(x)
}
define gcd(x,y) {
auto r,os;
os=scale;scale=0
if(x==x/1&&y==y/1){scale=os;return int_gcd(x,y)}
scale=os
while(y>0){r=remainder(x,y);x=y;y=r}
return(x)
}
# Lowest common multiple of x and y
define int_lcm(x,y) {
auto r,m,os;
os=scale;scale=0
x/=1;y/=1
m=x*y
while(y>0){r=x%y;x=y;y=r}
m/=x
scale=os
return(m)
}
define lcm(x,y) { return (x*y/gcd(x,y)) }
# Remove largest possible power of 2 from x
define oddpart(x){
auto os;
os=scale;scale=0;x/=1
if(x==0){scale=os;return 1}
while(!x%2)x/=2
scale=os;return x
}
# Largest power of 2 in x
define evenpart(x) {
auto os;
os=scale;scale=0
x/=oddpart(x/1)
scale=os;return x
}
## Trig / Hyperbolic Trig
# Sine
define sin(x) { return s(x) } # alias for standard library
# Cosine
define c(x) { return s(x+pi()/2) } # as fast or faster than
define cos(x) { return c(x) } # . standard library
# Tangent
define tan(x) { auto c;c=c(x);if(c==0)c=A^-scale;return(s(x)/c) }
# Secant
define sec(x) { auto c;c=c(x);if(c==0)c=A^-scale;return( 1/c) }
# Cosecant
define cosec(x) { auto s;s=s(x);if(s==0)s=A^-scale;return( 1/s) }
# Cotangent
define cotan(x) { auto s;s=s(x);if(s==0)s=A^-scale;return(c(x)/s) }
# Arcsine
define arcsin(x) { if(x==-1||x==1)return(pi()/2*x);return( a(x/sqrt(1-x*x)) ) }
# Arccosine
define arccos(x) { if(x==0)return(0);return pi()/2-arcsin(x) }
# Arctangent (one argument)
define arctan(x) { return a(x) } # alias for standard library
# Arctangent (two arguments)
define arctan2(x,y) {
auto p;
if(x==0&&y==0)return(0)
p=(1-sgn(y))*pi()*(2*(x>=0)-1)/2
if(x==0||y==0)return(p)
return(p+a(x/y))
}
# Arcsecant
define arcsec(x) { return( a(x/sqrt(x*x-1)) ) }
# Arccosecant
define arccosec(x) { return( a(x/sqrt(x*x-1))+pi()*(sgn(x)-1)/2 ) }
# Arccotangent (one argument)
define arccotan(x) { return( a(x)+pi()/2 ) }
# Arccotangent (two arguments)
define arccotan2(x,y) { return( arctan(x,y)+pi()/2 ) }
# Hyperbolic Sine
define sinh(x) { auto t;t=e(x);return((t-1/t)/2) }
# Hyperbolic Cosine
define cosh(x) { auto t;t=e(x);return((t+1/t)/2) }
# Hyperbolic Tangent
define tanh(x) { auto t;t=e(x+x)-1;return(t/(t+2)) }
# Hyperbolic Secant
define sech(x) { auto t;t=e(x);return(2/(t+1/t)) }
# Hyperbolic Cosecant
define cosech(x) { auto t;t=e(x);return(2/(t-1/t)) }
# Hyperbolic Cotangent
define coth(x) { auto t;t=e(x+x)-1;return((t+2)/t) }
# Hyperbolic Arcsine
define arcsinh(x) { return( l(x+sqrt(x*x+1)) ) }
# Hyperbolic Arccosine
define arccosh(x) { return( l(x+sqrt(x*x-1)) ) }
# Hyperbolic Arctangent
define arctanh(x) { return( l((1+x)/(1-x))/2 ) }
# Hyperbolic Arcsecant
define arcsech(x) { return( l((sqrt(1-x*x)+1)/x) ) }
# Hyperbolic Arccosecant
define arccosech(x) { return( l((sqrt(1+x*x)*sgn(x)+1)/x) ) }
# Hyperbolic Arccotangent
define arccoth(x) { return( l((x+1)/(x-1))/2 ) }
# Length of the diagonal vector (0,0)-(x,y) [pythagoras]
define pyth(x,y) { return(sqrt(x*x+y*y)) }
define pyth3(x,y,z) { return(sqrt(x*x+y*y+z*z)) }
# Gudermannian Function
define gudermann(x) { return 2*(a(e(x))-a(1)) }
# Inverse Gudermannian Function
define arcgudermann(x) {
return arctanh(s(x))
}
# Bessel function
define besselj(n,x) { return j(n,x) } # alias for standard library
## Exponential / Logs
# Exponential e^x
define exp(x) { return e(x) } # alias for standard library
# Natural Logarithm (base e)
define ln(x) {
auto os,len,ln;
if(x< 0){print "ln error: logarithm of a negative number\n";return 0}
if(x==0)print "ln error: logarithm of zero; negative infinity\n"
len=length(x)-scale(x)-1
if(len<A)return l(x);
os=scale;scale+=length(len)+1
ln=l(x/A^len)+len*l(A)
scale=os
return ln/1
} # speed improvement on standard library
# workhorse function for pow and log - new, less clever version
# Helps determine whether a fractional power is legitimate for a negative number
# . expects to be fed a positive value
# . returns -odd for even/odd; odd2 for odd1/odd2;
# even for odd/even; -2 for irrational
# . note that the return value is the denominator of the fraction if the
# fraction is rational, and the sign of the return value states whether
# the numerator is odd (positive) or even (negative)
# . since even/even is not possible, -2 is used to signify irrational
define id_frac2_(y){
auto os,oib,es,eps,lim,max,p,max2,i,cf[],f[],n,d,t;
os=scale
if(cf_max){
# cf.bc is present!
.=cf_new(cf[],y);if(scale(cf[0]))return -2;
.=frac_from_cf(f[],cf[],1)
d=f[0];scale=0;if(f[1]%2==0)d=-d;scale=os
return d
}
oib=ibase;ibase=A
scale=0
es=3*os/4
scale=os
eps=A^-es
y+=eps/A
scale=es
y/=1
scale=0
if(y<0)y=-y
d=y-(n=y/1)
if(d<eps){t=2*(n%2)-1;scale=os;ibase=oib;return t}#integers are x/1
t=y/2;t=y-t-t
# Find numerator and denominator of fraction, if any
lim=A*A;max2=A^5*(max=A^int(os/2));p=1
i=0;y=t
while(1) {
scale=es;y=1/y;scale=0
y-=(t=cf[++i]=y/1);p*=1+t
if(i>lim||(max<p&&p<max2)){cf[i=1]=-2;break}#escape if number seems irrational
if((p>max2||3*length(t)>es+es)&&i>1){cf[i--]=0;break}#cheat: assume rational
if(y==0)break;#completely rational
}
n=1;d=cf[i]
if(i==0){print "id_frac2_: something is wrong; y=",y,", d=",d,"\n"}
if(d!=-2&&i)while(--i){d=n+cf[i]*(t=d);n=t}
if(d<A^os){d*=2*(n%2)-1}else{d=-2}
scale=os;ibase=oib
return d;
}
# raise x to integer power y faster than bc's x^y
# . it seems bc (at time of writing) uses
# . an O(n) repeated multiplication algorithm
# . for the ^ operator, which is inefficient given
# . that there is a simple O(log n) alternative:
define fastintpow__(x,y) {
auto r,hy;
if(y==0)return(1)
if(y==1)return(x)
r=fastintpow__(x,hy=y/2)
r*=r;if(hy+hy<y)r*=x
return( r )
}
define fastintpow_(x,y) {
auto ix,os;
if(y<0)return fastintpow_(1/x,-y)
if(y==0)return(1)
if(y==1)return(x)
if(x==1)return(1)
os=scale;scale=0
if(x==-1){y%=2;y+=y;scale=os;return 1-y}
# bc is still faster for integers
if(x==(ix=x/1)){scale=os;return ix^y}
# ...and small no. of d.p.s, but not for values <= 2
if(scale(x)<3&&x>2){scale=os;return x^y}
scale=os;x/=1;scale=0
x=fastintpow__(x,y);
scale=os;return x;
}
# Raise x to a fractional power faster than e^(y*l(x))
define fastfracpow_(x,y) {
auto f,yy,inv;
inv=0;if(y<0){y=-y;inv=1}
y-=int(y)
if(y==0)return 1;
if((yy=y*2^C)!=int(yy)){x=l(x);if(inv)x=-x;return e(y/1*x)}
# faster using square roots for rational binary fractions
# where denominator <= 8192
x=sqrt(x)
for(f=1;y&&x!=1;x=sqrt(x))if(y+=y>=1){.=y--;f*=x}
if(inv)f=1/f;
return f;
}
# Find the yth root of x where y is integer
define fastintroot_(x,y) {
auto os,d,r,ys,eps;
os=scale;scale=0;y/=1;scale=os
if(y<0){x=1/x;y=-y}
if(y==1){return x}
if(y>=x-1){return fastfracpow_(x,1/y)}
if(y*int((d=2^F)/y)==d){
r=1;while(r+=r<=y)x=sqrt(x)
return x
}
scale=length(y)-scale(y);if(scale<5)scale=5;r=e(ln(x)/y)
scale=os+5;if(scale<5)scale=5
d=1;eps=A^(3-scale)
ys=y-1
while(d>eps){
d=r;r=(ys*r+x/fastintpow_(r,ys))/y
d-=r;if(d<0)d=-d
}
scale=os
return r/1
}
# Raise x to the y-th power
define pow(x,y) {
auto os,p,ix,iy,fy,dn,s;
if(y==0) return 1
if(x==0) return 0
if(0<x&&x<1){x=1/x;y=-y}
os=scale;scale=0
ix=x/1;iy=y/1;fy=y-iy;dn=0
scale=os;#scale=length(x/1)
if(y!=iy&&x<0){
dn=id_frac2_(y)# -ve implies even numerator
scale=0;if(dn%2){# odd denominator
scale=os
if(dn<0)return pow(-x,y) # even/odd
/*else*/return -pow(-x,y) # odd/odd
}
print "pow error: "
if(dn>0) print "even root"
if(dn<0) print "irrational power"
print " of a negative number\n"
scale=os;return 0
}
if(y==iy) {
if(x==ix){p=fastintpow_(ix,iy);if(iy>0){scale=0;p/=1};scale=os;return p/1}
scale+=scale;p=fastintpow_(x,iy);scale=os;return p/1
}
if((dn=id_frac2_(y))!=-2){ #accurate rational roots (sometimes slower)
if(dn<0)dn=-dn
s=1;if(y<0){y=-y;s=-1}
p=y*dn+1/2;scale=0;p/=1;scale=os
if(p<A^3)x=fastintpow_(x,p)
x=fastintroot_(x,dn)
if(p>=A^3)x=fastintpow_(x,p)
if(s<0)x=1/x
return x
}
p=fastintpow_(ix,iy)*fastfracpow_(x,fy);
scale=os+os
if(ix)p*=fastintpow_(x/ix,iy)
scale=os
return p/1
#The above is usually faster and more accurate than
# return( e(y*l(x)) );
}
# y-th root of x [ x^(1/y) ]
define root(x,y) {
return pow(x,1/y)
}
# Specific cube root function
# = stripped down version of fastintroot_(x,3)
define cbrt(x) {
auto os,d,r,eps;
if(x<0)return -cbrt(-x)
if(x==0)return 0
os=scale;scale=0;eps=A^(scale/3)
if(x<eps){scale=os;return 1/cbrt(1/x)}
scale=5;r=e(ln(x)/3)
scale=os+5;if(scale<5)scale=5
d=1;eps=A^(3-scale)
while(d>eps){
d=r;r=(r+r+x/(r*r))/3
d-=r;if(d<0)d=-d
}
scale=os
return r/1
}
# Logarithm of x in given base: log(2, 32) = 5 because 2^5 = 32
# tries to return a real answer where possible when given negative numbers