#cmdline "-O 2 -v" '#cmdline "-asm intel -t 16384 -w all -arch native -gen gcc -Wc -O2 -v" '#cmdline "-asm att -t 16384 -w all -arch native -gen clang -Wc -O2 -v" declare sub init_constants const NUMBER_OF_DIGITS = 10000 '136-8 '32786 '35660 '10000 '24 '1000 '1008 '7056 '1008 '120 '1024 '120 Const NUM_DIGITS = (NUMBER_OF_DIGITS \ 8 - ((NUMBER_OF_DIGITS Mod 8) <> 0)) * 8 const bias=1073741824 const NUM_DWORDS=NUM_DIGITS\8-1 const Infinity = 1.0 / 0.0 const DIVZ_ERR = 1 const EXPO_ERR = 2 const EXPU_ERR = 3 /' my FreeBasic decfloat library is big-endian and uses the following structure const NUMBER_OF_DIGITS = 100 'user defined Const NUM_DIGITS = (NUMBER_OF_DIGITS \ 8 - ((NUMBER_OF_DIGITS Mod 8) <> 0)) * 8 const BIAS=1073741824 const NUM_DWORDS=NUM_DIGITS\8 type decfloat_struct as long sign as long exponent 'offset by BIAS as long mantissa(0 to NUM_DWORDS) end type the mantissa is always kept left-justified, the exponent is offset by BIAS can you adapt the following code to my library using 8 digit limbs instead of strings and 1 digit? Declare Function karatsuba (Byref x As Const String, Byref y As Const String) As String Declare Function padleft (Byref s As Const String, Byval l As Long) As String Declare Function addstrings (Byref a As Const String, Byref b As Const String) As String Declare Function substrings (Byref a As Const String, Byref b As Const String) As String Declare Function shiftleft (Byref s As Const String, Byval n As Long) As String Declare Function stripzeros (Byref s As Const String) As String ' ========================================================================= ' example usage ' ========================================================================= Cls Dim As String num1, num2, result 'num1 = "12345678" 'num2 = "87654321" Input "num1 ", num1 Input "num2 ", num2 Print "multiplicand: "; num1 Print "multiplier: "; num2 Print "--------------------------------------------------" result = karatsuba(num1, num2) Print "product: "; result End ' ========================================================================= ' karatsuba multiplication algorithm ' ========================================================================= Function karatsuba (Byref x As Const String, Byref y As Const String) As String Dim As String xclean, yclean xclean = stripzeros(x) yclean = stripzeros(y) ' base cases If xclean = "0" Or yclean = "0" Then karatsuba = "0" Exit Function End If If Len(xclean) = 1 And Len(yclean) = 1 Then karatsuba = Ltrim(Str(Clng(xclean) * Clng(yclean))) Exit Function End If ' equalize lengths by padding with leading zeros Dim maxlen As Long maxlen = Len(xclean) If Len(yclean) > maxlen Then maxlen = Len(yclean) ' round up to the nearest even length for symmetric splitting If (maxlen Mod 2) <> 0 Then maxlen = maxlen + 1 Dim As String xpad, ypad xpad = padleft(xclean, maxlen) ypad = padleft(yclean, maxlen) ' midpoint for split Dim As Long m, m2 m = maxlen \ 2 m2 = maxlen - m ' split strings into high and low parts Dim As String xh, xl, yh, yl xh = Left(xpad, m) xl = Right(xpad, m2) yh = Left(ypad, m) yl = Right(ypad, m2) ' recursive steps Dim As String z2, z0, z1 z2 = karatsuba(xh, yh) z0 = karatsuba(xl, yl) ' z1 components: (xh + xl) * (yh + yl) - z2 - z0 Dim As String sumx, sumy, p3 sumx = addstrings(xh, xl) sumy = addstrings(yh, yl) p3 = karatsuba(sumx, sumy) z1 = substrings(p3, z2) z1 = substrings(z1, z0) ' shift results (multiply by base powers) Dim As String term1, term2 term1 = shiftleft(z2, m2 * 2) term2 = shiftleft(z1, m2) ' combine final terms Dim As String finalresult finalresult = addstrings(term1, term2) finalresult = addstrings(finalresult, z0) Return stripzeros(finalresult) End Function ' ========================================================================= ' big integer helper functions ' ========================================================================= Function addstrings (Byref a As Const String, Byref b As Const String) As String Dim As Long maxlen maxlen = Len(a) If Len(b) > maxlen Then maxlen = Len(b) Dim As String apad, bpad apad = padleft(a, maxlen) bpad = padleft(b, maxlen) Dim res As String, carry As Long res = Space(maxlen) carry = 0 Dim As Long i, digitsum For i = maxlen To 1 Step -1 digitsum = Clng(Mid(apad, i, 1)) + Clng(Mid(bpad, i, 1)) + carry carry = digitsum \ 10 Mid(res, i, 1) = Right(Str(digitsum Mod 10), 1) Next i If carry > 0 Then res = "1" + res End If Return res End Function Function substrings (Byref a As Const String, Byref b As Const String) As String ' assumes a$ >= b$ Dim As Long maxlen maxlen = Len(a) Dim As String bpad bpad = padleft(b, maxlen) Dim res As String, borrow As Long res = Space(maxlen) borrow = 0 Dim As Long i, diff For i = maxlen To 1 Step -1 diff = Clng(Mid(a, i, 1)) - Clng(Mid(bpad, i, 1)) - borrow If diff < 0 Then diff = diff + 10 borrow = 1 Else borrow = 0 End If Mid(res, i, 1) = Right(Str(diff), 1) Next i Return stripzeros(res) End Function Function shiftleft (Byref s As Const String, Byval n As Long) As String If s = "0" Or n <= 0 Then Return s Else Return s + String(n, "0") End If End Function Function padleft (Byref s As Const String, Byval l As Long) As String Dim diff As Long diff = l - Len(s) If diff > 0 Then Return String(diff, "0") + s Else Return s End If End Function Function stripzeros (Byref s As Const String) As String Dim As Long i For i = 1 To Len(s) If Mid(s, i, 1) <> "0" Then Return Mid(s, i) End If Next i Return "0" End Function '/ dim shared as const longint p10_constants(...)= {_ 1ll, _ 10ll, _ 100ll, _ 1000ll, _ 10000ll, _ 100000ll, _ 1000000ll, _ 10000000ll, _ 100000000ll, _ 1000000000ll, _ 10000000000ll, _ 100000000000ll, _ 1000000000000ll, _ 10000000000000ll, _ 100000000000000ll, _ 1000000000000000ll, _ 10000000000000000ll, _ 100000000000000000ll, _ 1000000000000000000ll} type decfloat_struct as long sign as long exponent as long mantissa(0 to NUM_DWORDS) 'function ToString(places:Int32 = num_digits): ansistring; 'function ToStringExp(places:Int32 = num_digits): ansistring; end type type decfloat_structc as decfloat_struct re, im 'function ToString(places:Int32 = num_digits): ansistring; 'function ToStringExp(places:Int32 = num_digits): ansistring; end type Type decfloat Declare Constructor ( ) Declare Constructor ( Byval rhs As Long ) Declare Constructor ( Byval rhs As Longint ) Declare Constructor ( Byval rhs As Integer ) Declare Constructor ( Byval rhs As Double ) Declare Constructor ( Byref rhs As String ) Declare Constructor ( Byref rhs As decfloat ) Declare Destructor ( ) Declare Operator Let ( Byval rhs As Long ) Declare Operator Let ( Byval rhs As Longint ) Declare Operator Let ( Byval rhs As Integer ) Declare Operator Let ( Byval rhs As Double ) Declare Operator Let ( Byref rhs As String ) Declare Operator Let ( Byref rhs As decfloat ) Declare Operator Cast ( ) As String Declare Operator Cast ( ) As Long Declare Operator Cast ( ) As Double '---------------------------------------------- Declare Operator += (Byref rhs As decfloat) Declare Operator += (Byval rhs As Longint) Declare Operator += (Byval rhs As Double) Declare Operator += (Byref rhs As String) Declare Operator -= (Byref rhs As decfloat) Declare Operator -= (Byval rhs As Longint) Declare Operator -= (Byval rhs As Double) Declare Operator -= (Byref rhs As String) Declare Operator *= (Byref rhs As decfloat) Declare Operator *= (Byval rhs As Longint) Declare Operator *= (Byval rhs As Double) Declare Operator *= (Byref rhs As String) Declare Operator /= (Byref rhs As decfloat) Declare Operator /= (Byval rhs As Longint) Declare Operator /= (Byval rhs As Double) Declare Operator /= (Byval rhs As Single) Declare Operator /= (Byref rhs As String) ' For Next Implicit step = +1 Declare Operator For ( ) Declare Operator Step( ) Declare Operator Next( Byref end_cond As decfloat ) As Integer ' For Next Exlicit step Declare Operator For ( Byref stp As decfloat ) Declare Operator Step( Byref stp As decfloat ) Declare Operator Next( Byref end_cond As decfloat, Byref step_var As decfloat ) As Integer '---------------------------------------------- Declare Function toString( byval places as long=NUM_DIGITS ) As String Declare Function toString_exp( byval places as long=NUM_DIGITS ) As String Declare Function toLong ( ) As Long Declare Function toDouble ( ) As Double Declare Function toLongint ( ) As Longint dec_num As decfloat_struct End Type dim shared as decfloat fppi, fpln2, fpexp1, fpPihalf, fpPi2, Pi3o2, fpdtor, fpPIo6, fpSQR3, fpTMS3,fpconst1olog10, sqrt2pi dim shared as decfloat ExpAn(29), SinAn(27), CosAn(27), TanAn(13), TanBn(13), AtanAn(17), AtanBn(17), gamma_coeff(70) init_constants function log10_32(byval value as ulong) as long if (value >= 1000000000) then return 9 if (value >= 100000000) then return 8 if (value >= 10000000) then return 7 if (value >= 1000000) then return 6 if (value >= 100000) then return 5 if (value >= 10000) then return 4 if (value >= 1000) then return 3 if (value >= 100) then return 2 if (value >= 10) then return 1 if (value >= 1) then return 0 end function function log10_64(byval value as ulongint) as long if (value >= 10000000000000000000) then return 19 if (value >= 1000000000000000000) then return 18 if (value >= 100000000000000000) then return 17 if (value >= 10000000000000000) then return 16 if (value >= 1000000000000000) then return 15 if (value >= 100000000000000) then return 14 if (value >= 10000000000000) then return 13 if (value >= 1000000000000) then return 12 if (value >= 100000000000) then return 11 if (value >= 10000000000) then return 10 if (value >= 1000000000) then return 9 if (value >= 100000000) then return 8 if (value >= 10000000) then return 7 if (value >= 1000000) then return 6 if (value >= 100000) then return 5 if (value >= 10000) then return 4 if (value >= 1000) then return 3 if (value >= 100) then return 2 if (value >= 10) then return 1 if (value >= 1) then return 0 end function Function str2fp(Byref value_in As String ) As decfloat_struct Dim As Integer j,s,d,e,ep,ex,es,i,f,fp,fln Dim As String c,f1,f2,f3, ts, value Dim As Ulong ulng Dim n As decfloat_struct j=1 s=1 d=0 e=0 ep=0 ex=0 es=1 i=0 f=0 fp=0 f1="" f2="" f3="" value=Ucase(value_in) value=ltrim(value, "0") if value="" then value="0" fln=Len(value) While j<=fln 'c=Mid(value,j,1) c=chr(value[j-1]) If ep=1 Then If c=" " Then j=j+1 Continue While Endif If c="-" Then es=-es c="" Endif If c="+" Then j=j+1 Continue While Endif If (c="0") And (f3="") Then j=j+1 Continue While Endif If (c>"/") And (c<":") Then 'c is digit between 0 and 9 f3=f3+c ex=10*ex+(Asc(c)-48) j=j+1 Continue While Endif Endif If c=" " Then j=j+1 Continue While Endif If c="-" Then s=-s j=j+1 Continue While Endif If c="+" Then j=j+1 Continue While Endif If c="." Then If d=1 Then j=j+1 Continue While Endif d=1 Endif If (c>"/") And (c<":") Then 'c is digit between 0 and 9 If ((c="0") And (i=0)) Then If d=0 Then j=j+1 Continue While Endif If (d=1) And (f=0) Then e=e-1 j=j+1 Continue While Endif Endif If d=0 Then f1=f1+c i=i+1 Else If (c>"0") Then fp=1 Endif f2=f2+c f=f+1 Endif Endif If c="E" or c="D" Then ep=1 Endif j=j+1 Wend If fp=0 Then f=0 f2="" Endif If s=-1 Then s=-1 Else s=1 n.sign=s ex=es*ex-1+i+e f1=f1+f2 f1=Mid(f1,1,1)+Right(f1,Len(f1)-1) fln=Len(f1) If Len(f1)>(NUM_DIGITS+1+8) Then f1=Mid(f1,1,(NUM_DIGITS+1+8)) Endif While Len(f1)<(NUM_DIGITS+1+8) f1=f1+"0" Wend j=1 For i=0 To NUM_DWORDS ts=Mid(f1,j,8) ulng=ValUInt(ts) n.mantissa(i)=ulng if ulng<>0 then fp=1 j+=8 Next if fp then n.exponent=(ex+BIAS+1) Else n.exponent=0 return n End Function function fp2str_exp (byref n as decfloat_struct, byval places_in as long=num_digits) as String dim as long places, i, ex dim as string v, f, ts places=places_in If (places>num_digits) Then places=num_digits places=8*(1+NUM_DWORDS) If n.exponent > 0 Then ex = (n.exponent and &h7fffffff) - bias - 1 Else ex = 0 end if If n.sign<1 Then v = "-" Else v = " " end if ts=Str(n.mantissa(0)) ts=trim(ts) while Len(ts) < 8 ts = ts + "0" wEnd v = v + chr(ts[0])+"."+mid(ts, 2) For i = 1 To NUM_DWORDS ts=Str(n.mantissa(i)) ts=trim(ts) while Len(ts) < 8 ts = "0" + ts wEnd v = v + ts next v=left(v, places + 2) v=rtrim(v, "0") f=Str(Abs(ex)) f=trim(f) If ex < 0 Then v = v + "e-" Else v = v + "e+" end if v = v + f return v end function /' function fpfix(byref num as decfloat_struct, byval dwords_in as long = NUM_DWORDS) as decfloat_struct dim as long dwords, ex, ex2, j, k dim as decfloat_struct ip dwords = dwords_in if (dwords > NUM_DWORDS) then dwords = NUM_DWORDS for j=0 to NUM_DWORDS ip.mantissa(j)=0 next ex = (num.exponent and &h7FFFFFFF) - bias if (ex < 1) then for j=0 to NUM_DWORDS ip.mantissa(j)=0 next ip.exponent=0 ip.sign=1 return ip end if if (ex >= (8 * dwords)) then return num ex2 = ex \ 8 k = ex2 j = ex mod 8 j+=1 while (ex2 > 0) ex2 -= 1 ip.mantissa(ex2) = num.mantissa(ex2) wend if (j = 1) then ip.mantissa(k) = 10000000 * (num.mantissa(k) \ 10000000) elseif (j = 2) then ip.mantissa(k) = 1000000 * (num.mantissa(k) \ 1000000) elseif (j = 3) then ip.mantissa(k) = 100000 * (num.mantissa(k) \ 100000) elseif (j = 4) then ip.mantissa(k) = 10000 * (num.mantissa(k) \ 10000) elseif (j = 5) then ip.mantissa(k) = 1000 * (num.mantissa(k) \ 1000) elseif (j = 6) then ip.mantissa(k) = 100 * (num.mantissa(k) \ 100) elseif (j = 7) then ip.mantissa(k) = 10 * (num.mantissa(k) \ 10) elseif (j = 8) then ip.mantissa(k) = num.mantissa(k) end if ip.exponent = ex + bias ip.sign = num.sign return ip end function '/ 'integer part of num function fpfix( byref num as decfloat_struct ) as decfloat_struct dim as decfloat_struct ip dim as long ex, ex2, j, k ex=(num.exponent And &h7FFFFFFF)-BIAS if ex<1 then return ip end if if ex>=(NUM_DIGITS) then return num end if ex2=ex\8 k=ex2 j=ex mod 8 while ex2>0 ex2-=1 ip.mantissa(ex2)=num.mantissa(ex2) wend if j=1 then ip.mantissa(k)=10000000*(num.mantissa(k)\10000000) elseif j=2 then ip.mantissa(k)=1000000*(num.mantissa(k)\1000000) elseif j=3 then ip.mantissa(k)=100000*(num.mantissa(k)\100000) elseif j=4 then ip.mantissa(k)=10000*(num.mantissa(k)\10000) elseif j=5 then ip.mantissa(k)=1000*(num.mantissa(k)\1000) elseif j=6 then ip.mantissa(k)=100*(num.mantissa(k)\100) elseif j=7 then ip.mantissa(k)=10*(num.mantissa(k)\10) elseif j=8 then ip.mantissa(k)=num.mantissa(k) end if ip.exponent=ex+BIAS ip.sign=num.sign return ip end function function fpfix_is_odd(byref num as decfloat_struct) as long dim as long ex, ex2, j, k ex = (num.exponent and &h7fffffff) - bias if (ex < 1) then return 0 if (ex >= num_digits) then return -1 ex-=1 ex2 = ex \ 8 k = ex2 j = ex mod 8 j +=1 if (j = 1) then return ((num.mantissa(k) \ 10000000) and 1) if (j = 2) then return ((num.mantissa(k) \ 1000000) and 1) if (j = 3) then return ((num.mantissa(k) \ 100000) and 1) if (j = 4) then return ((num.mantissa(k) \ 10000) and 1) if (j = 5) then return ((num.mantissa(k) \ 1000) and 1) if (j = 6) then return ((num.mantissa(k) \ 100) and 1) if (j = 7) then return ((num.mantissa(k) \ 10) and 1) if (j = 8) then return (num.mantissa(k) and 1) return 0 end function function ipower(byval x as double, byval e as long) as double dim as double z, y dim as long n ' take x to an integer power y = x n = abs(e) z = 1.0 while (n > 0) while ((n and 1) = 0) n = n \ 2 y = y * y wend n = n - 1 z = y * z wend if (e < 0) then z = 1.0 / z return z end function function fp2dbl(byref n as decfloat_struct) as double dim as double dbl, tp dim as long ex, sign if (n.sign < 0) then sign = -1 else sign = 1 end if if (n.exponent > 0) then ex = (n.exponent and &h7fffffff) - bias - 1 else ex = 0 end if if (ex > 308) then return infinity if (ex < (-308)) then return 0 dbl = cdbl(n.mantissa(0)) + (cdbl(n.mantissa(1)) / 100000000.0) dbl = dbl / 10000000.0 tp = ipower(10.0, ex) return dbl * tp * sign end function function si2fp( byval m as longint, byval dwords_in as long=NUM_DWORDS) as decfloat_struct dim as long dwords, ex, i dim as longint mm dim as decfloat_struct result dwords=dwords_in if (dwords > NUM_DWORDS) then dwords = NUM_DWORDS mm = abs(m) for i = 0 to dwords result.mantissa(i) = 0 next if (m = 0) then result.exponent = 0 result.sign = 1 return result end if ex = log10_64(mm) result.exponent = bias + ex + 1 if (ex > 15) then ex = ex-7 result.mantissa(0) = (mm \ p10_constants(ex)) mm = mm mod p10_constants(ex) ex = ex-8 result.mantissa(1) = (mm \ p10_constants(ex)) mm = mm mod p10_constants(ex) result.mantissa(2) = (mm * p10_constants(8 - ex)) elseif (ex >= 8) then ex = ex-7 result.mantissa(0) = (mm \ p10_constants(ex)) mm = mm mod p10_constants(ex) result.mantissa(1) = (mm * p10_constants(8 - ex)) elseif (ex < 8) then result.mantissa(0) = (mm * p10_constants(7 - ex)) end if result.sign = 1 if (m < 0) then result.sign = -1 return result end function sub rshift_1(byref mantissa as decfloat_struct, byval dwords_in as long = NUM_DWORDS) dim as long dwords, i, v1, v2 dwords = dwords_in if (dwords > NUM_DWORDS) then dwords = NUM_DWORDS for i = dwords to 1 step -1 v1 = mantissa.mantissa(i) \ 10 v2 = mantissa.mantissa(i - 1) mod 10 v2 = v2 * 10000000 + v1 mantissa.mantissa(i) = v2 next mantissa.mantissa(0) = mantissa.mantissa(0) \ 10 end sub sub lshift_1( byref mantissa as decfloat_struct, byval dwords_in as long = NUM_DWORDS) dim as long dwords, i, v1, v2 dwords = dwords_in if (dwords > NUM_DWORDS) then dwords = NUM_DWORDS for i = 0 to (dwords - 1) v1 = mantissa.mantissa(i) mod 10000000 v2 = mantissa.mantissa(i + 1) \ 10000000 mantissa.mantissa(i) = v1 * 10 + v2 mantissa.mantissa(i + 1) = mantissa.mantissa(i + 1) mod 10000000 next mantissa.mantissa(dwords) = 10 * (mantissa.mantissa(dwords) mod 10000000) end sub sub rshift_2( byref mantissa as decfloat_struct, byval dwords_in as long = NUM_DWORDS) dim as long dwords, i, v1, v2 dwords = dwords_in if (dwords > NUM_DWORDS) then dwords = NUM_DWORDS for i = dwords to 1 step -1 v1 = mantissa.mantissa(i) \ 100 v2 = mantissa.mantissa(i - 1) mod 100 v2 = v2 * 1000000 + v1 mantissa.mantissa(i) = v2 next mantissa.mantissa(0) = mantissa.mantissa(0) \ 100 end sub sub lshift_2( byref mantissa as decfloat_struct, byval dwords_in as long = NUM_DWORDS) dim as long dwords, i, v1, v2 dwords = dwords_in if (dwords > NUM_DWORDS) then dwords = NUM_DWORDS for i = 0 to (dwords - 1) v1 = mantissa.mantissa(i) mod 1000000 v2 = mantissa.mantissa(i + 1) \ 1000000 mantissa.mantissa(i) = v1 * 100 + v2 mantissa.mantissa(i + 1) = mantissa.mantissa(i + 1) mod 1000000 next mantissa.mantissa(dwords) = 100 * (mantissa.mantissa(dwords) mod 1000000) end sub sub rshift_3( byref mantissa as decfloat_struct, byval dwords_in as long = NUM_DWORDS) dim as long dwords, i, v1, v2 dwords = dwords_in if (dwords > NUM_DWORDS) then dwords = NUM_DWORDS for i = dwords to 1 step -1 v1 = mantissa.mantissa(i) \ 1000 v2 = mantissa.mantissa(i - 1) mod 1000 v2 = v2 * 100000 + v1 mantissa.mantissa(i) = v2 next mantissa.mantissa(0) = mantissa.mantissa(0) \ 1000 end sub sub lshift_3( byref mantissa as decfloat_struct, byval dwords_in as long = NUM_DWORDS) dim as long dwords, i, v1, v2 dwords = dwords_in if (dwords > NUM_DWORDS) then dwords = NUM_DWORDS for i = 0 to (dwords - 1) v1 = mantissa.mantissa(i) mod 100000 v2 = mantissa.mantissa(i + 1) \ 100000 mantissa.mantissa(i) = v1 * 1000 + v2 mantissa.mantissa(i + 1) = mantissa.mantissa(i + 1) mod 100000 next mantissa.mantissa(dwords) = 1000 * (mantissa.mantissa(dwords) mod 100000) end sub sub rshift_4( byref mantissa as decfloat_struct, byval dwords_in as long = NUM_DWORDS) dim as long dwords, i, v1, v2 dwords = dwords_in if (dwords > NUM_DWORDS) then dwords = NUM_DWORDS for i = dwords to 1 step -1 v1 = mantissa.mantissa(i) \ 10000 v2 = mantissa.mantissa(i - 1) mod 10000 v2 = v2 * 10000 + v1 mantissa.mantissa(i) = v2 next mantissa.mantissa(0) = mantissa.mantissa(0) \ 10000 end sub sub lshift_4( byref mantissa as decfloat_struct, byval dwords_in as long = NUM_DWORDS) dim as long dwords, i, v1, v2 dwords = dwords_in if (dwords > NUM_DWORDS) then dwords = NUM_DWORDS for i = 0 to (dwords - 1) v1 = mantissa.mantissa(i) mod 10000 v2 = mantissa.mantissa(i + 1) \ 10000 mantissa.mantissa(i) = v1 * 10000 + v2 mantissa.mantissa(i + 1) = mantissa.mantissa(i + 1) mod 10000 next mantissa.mantissa(dwords) = 10000 * (mantissa.mantissa(dwords) mod 10000) end sub sub rshift_5( byref mantissa as decfloat_struct, byval dwords_in as long = NUM_DWORDS) dim as long dwords, i, v1, v2 dwords = dwords_in if (dwords > NUM_DWORDS) then dwords = NUM_DWORDS for i = dwords to 1 step -1 v1 = mantissa.mantissa(i) \ 100000 v2 = mantissa.mantissa(i - 1) mod 100000 v2 = v2 * 1000 + v1 mantissa.mantissa(i) = v2 next mantissa.mantissa(0) = mantissa.mantissa(0) \ 100000 end sub sub lshift_5( byref mantissa as decfloat_struct, byval dwords_in as long = NUM_DWORDS) dim as long dwords, i, v1, v2 dwords = dwords_in if (dwords > NUM_DWORDS) then dwords = NUM_DWORDS for i = 0 to (dwords - 1) v1 = mantissa.mantissa(i) mod 1000 v2 = mantissa.mantissa(i + 1) \ 1000 mantissa.mantissa(i) = v1 * 100000 + v2 mantissa.mantissa(i + 1) = mantissa.mantissa(i + 1) mod 1000 next mantissa.mantissa(dwords) = 100000 * (mantissa.mantissa(dwords) mod 1000) end sub sub rshift_6( byref mantissa as decfloat_struct, byval dwords_in as long = NUM_DWORDS) dim as long dwords, i, v1, v2 dwords = dwords_in if (dwords > NUM_DWORDS) then dwords = NUM_DWORDS for i = dwords to 1 step -1 v1 = mantissa.mantissa(i) \ 1000000 v2 = mantissa.mantissa(i - 1) mod 1000000 v2 = v2 * 100 + v1 mantissa.mantissa(i) = v2 next mantissa.mantissa(0) = mantissa.mantissa(0) \ 1000000 end sub sub lshift_6( byref mantissa as decfloat_struct, byval dwords_in as long = NUM_DWORDS) dim as long dwords, i, v1, v2 dwords = dwords_in if (dwords > NUM_DWORDS) then dwords = NUM_DWORDS for i = 0 to (dwords - 1) v1 = mantissa.mantissa(i) mod 100 v2 = mantissa.mantissa(i + 1) \ 100 mantissa.mantissa(i) = v1 * 1000000 + v2 mantissa.mantissa(i + 1) = mantissa.mantissa(i + 1) mod 100 next mantissa.mantissa(dwords) = 1000000 * (mantissa.mantissa(dwords) mod 100) end sub sub rshift_7( byref mantissa as decfloat_struct, byval dwords_in as long = NUM_DWORDS) dim as long dwords, i, v1, v2 dwords = dwords_in if (dwords > NUM_DWORDS) then dwords = NUM_DWORDS for i = dwords to 1 step -1 v1 = mantissa.mantissa(i) \ 10000000 v2 = mantissa.mantissa(i - 1) mod 10000000 v2 = v2 * 10 + v1 mantissa.mantissa(i) = v2 next mantissa.mantissa(0) = mantissa.mantissa(0) \ 10000000 end sub sub lshift_7( byref mantissa as decfloat_struct, byval dwords_in as long = NUM_DWORDS) dim as long dwords, i, v1, v2 dwords = dwords_in if (dwords > NUM_DWORDS) then dwords = NUM_DWORDS for i = 0 to (dwords - 1) v1 = mantissa.mantissa(i) mod 10 v2 = mantissa.mantissa(i + 1) \ 10 mantissa.mantissa(i) = v1 * 10000000 + v2 mantissa.mantissa(i + 1) = mantissa.mantissa(i + 1) mod 10 next mantissa.mantissa(dwords) = 10000000 * (mantissa.mantissa(dwords) mod 10) end sub sub rshift_8( byref mantissa as decfloat_struct, byval dwords_in as long = NUM_DWORDS) dim as long dwords, i dwords = dwords_in if (dwords > NUM_DWORDS) then dwords = NUM_DWORDS for i = dwords to 1 step -1 mantissa.mantissa(i) = mantissa.mantissa(i - 1) next mantissa.mantissa(0) = 0 end sub sub lshift_8( byref mantissa as decfloat_struct, byval dwords_in as long = NUM_DWORDS) dim as long dwords, i dwords = dwords_in if (dwords > NUM_DWORDS) then dwords = NUM_DWORDS for i = 0 to (dwords - 1) mantissa.mantissa(i) = mantissa.mantissa(i + 1) next mantissa.mantissa(dwords) = 0 end sub Function fpcmp(Byref x As decfloat_struct, Byref y As decfloat_struct, Byval dwords As Long=NUM_DWORDS) As Long If dwords>NUM_DWORDS Then dwords=NUM_DWORDS Dim As Long i Dim As Longint c If x.sign < y.sign Then return -1 end if If x.sign > y.sign Then return 1 end if If x.exponenty.exponent Then if x.sign=1 then Return 1 else return -1 end if end if For i=0 To dwords c=clngint(x.mantissa(i))-clngint(y.mantissa(i)) If c<>0 Then Exit For Next if c=0 then return 0 If c<0 Then if x.sign=1 then Return -1 else return 1 end if end if If c>0 Then if x.sign=1 then Return 1 else return -1 end if end if End Function function fpcmp_abs(byref x as decfloat_struct, byref y as decfloat_struct, byval dwords_in as long = NUM_DWORDS) as long dim as long dwords, i dim as longint c dwords = dwords_in if (dwords > NUM_DWORDS) then dwords = NUM_DWORDS if (x.exponent < y.exponent) then return -1 if (x.exponent > y.exponent) then return 1 for i = 0 to dwords c = clngint(x.mantissa(i)) - clngint(y.mantissa(i)) if (c <> 0) then exit for next if (c = 0) then return 0 if (c < 0) then return -1 if (c > 0) then return 1 return 0 end function function norm_fac1(byref fac1 as decfloat_struct, byval dwords_in as long = NUM_DWORDS) as long dim as long dwords, i, f dwords = dwords_in f=0 if (dwords > NUM_DWORDS) then dwords = NUM_DWORDS ' normalize the number in fac1 ' all routines exit through this one. 'see if the mantissa is all zeros. 'if so, set the exponent and sign equal to 0. for i = 0 to dwords if (fac1.mantissa(i) > 0) then f = 1 next if (f = 0) then fac1.exponent = 0 fac1.sign = 0 return 0 'if the highmost digit in fac1_man is nonzero, 'shift the mantissa right 1 digit and 'increment the exponent elseif (fac1.mantissa(0) > 99999999) then rshift_1(fac1, dwords) fac1.exponent+=1 else 'now shift fac1_man 1 to the left until a 'nonzero digit appears in the }-to-highest 'digit of fac1_man. decrement exponent for 'each shift. while (fac1.mantissa(0) = 0) lshift_8(fac1, dwords) fac1.exponent-= 8 if (fac1.exponent = 0) then return EXPU_ERR end if wend if (fac1.mantissa(0) < 10) then lshift_7(fac1, dwords) fac1.exponent-= 7 elseif (fac1.mantissa(0) < 100) then lshift_6(fac1, dwords) fac1.exponent-= 6 elseif (fac1.mantissa(0) < 1000) then lshift_5(fac1, dwords) fac1.exponent-= 5 elseif (fac1.mantissa(0) < 10000) then lshift_4(fac1, dwords) fac1.exponent-= 4 elseif (fac1.mantissa(0) < 100000) then lshift_3(fac1, dwords) fac1.exponent-= 3 elseif (fac1.mantissa(0) < 1000000) then lshift_2(fac1, dwords) fac1.exponent-= 2 elseif (fac1.mantissa(0) < 10000000) then lshift_1(fac1, dwords) fac1.exponent-=1 end if end if ' check for overflow/underflow if (fac1.exponent < 0) then return EXPO_ERR end if return 0 end function sub fpadd_aux(byref fac1 as decfloat_struct, byref fac2 as decfloat_struct, byval dwords_in as long = NUM_DWORDS) dim as long dwords, i, v, c dwords = dwords_in if (dwords > NUM_DWORDS) then dwords = NUM_DWORDS c = 0 for i = dwords to 1 step -1 v = fac2.mantissa(i) + fac1.mantissa(i) + c if (v > 99999999) then v = v - 100000000 c = 1 else c = 0 end if fac1.mantissa(i) = v next v = fac1.mantissa(0) + fac2.mantissa(0) + c fac1.mantissa(0) = v i = norm_fac1(fac1, dwords) end sub sub fpsub_aux(byref fac1 as decfloat_struct, byref fac2 as decfloat_struct, byval dwords_in as long = NUM_DWORDS) dim as long dwords, i, v, c dwords = dwords_in if (dwords > NUM_DWORDS) then dwords = NUM_DWORDS c = 0 for i = dwords to 1 step -1 v = fac1.mantissa(i) - fac2.mantissa(i) - c if (v < 0) then v = v + 100000000 c = 1 else c = 0 end if fac1.mantissa(i) = v next v = fac1.mantissa(0) - fac2.mantissa(0) - c fac1.mantissa(0) = v i = norm_fac1(fac1, dwords) end sub function fpadd(byref x as decfloat_struct, byref y as decfloat_struct, byval dwords_in as long = NUM_DWORDS) as decfloat_struct dim as long dwords, i, t, c dim as decfloat_struct fac1, fac2 dwords = dwords_in if (dwords > NUM_DWORDS) then dwords = NUM_DWORDS c = fpcmp_abs(x, y, dwords) if (c < 0) then fac1 = y fac2 = x else fac1 = x fac2 = y end if 't = fac1.exponent - fac2.exponent t = ((fac1.exponent and &h7fffffff) - bias - 1) - ((fac2.exponent and &h7fffffff) - bias - 1) if (t < (num_digits+8)) then 'the difference between the two 'exponents indicate how many times 'we have to multiply the mantissa 'of fac2 by 10 (i.e., shift it right 1 place). 'if we have to shift more times than 'we have dwords, the result is already in fac1. t = fac1.exponent - fac2.exponent if ((t > 0) and (t < (num_digits+8))) then 'shift i = t \ 8 while (i > 0) rshift_8(fac2, dwords) t-= 8 i-=1 wend if (t = 7) then rshift_7(fac2, dwords) elseif (t = 6) then rshift_6(fac2, dwords) elseif (t = 5) then rshift_5(fac2, dwords) elseif (t = 4) then rshift_4(fac2, dwords) elseif (t = 3) then rshift_3(fac2, dwords) elseif (t = 2) then rshift_2(fac2, dwords) elseif (t = 1) then rshift_1(fac2, dwords) end if end if 'see if the signs of the two numbers 'are the same. if so, add if not, subtract. if (fac1.sign = fac2.sign) then 'add fpadd_aux(fac1, fac2, dwords) else fpsub_aux(fac1, fac2, dwords) end if end if return fac1 end function function fpsub(byref x as decfloat_struct, byref y as decfloat_struct, byval dwords_in as long = NUM_DWORDS) as decfloat_struct dim as long dwords dim as decfloat_struct fac1, fac2 dwords = dwords_in if (dwords > NUM_DWORDS) then dwords = NUM_DWORDS fac1 = x fac2 = y fac2.sign = -fac2.sign return fpadd(fac1, fac2, dwords) end function sub LSHIFT_a (mantissa() as ulong, byval n as long, byval k as long) dim as ulong v1, v2, c1, c2 dim as long i c1 = p10_constants(k) c2 = p10_constants(8 - k) For i = 0 To n v1 = mantissa(i) mod c2 v2 = mantissa(i + 1) \ c2 mantissa(i) = (v1 * c1 + v2) mantissa(i + 1) = mantissa(i + 1) mod c2 next mantissa(n+1) = c1 * (mantissa(n+1) mod c2) End sub function fpmul(byref x as decfloat_struct, byref y as decfloat_struct, byval dwords_in as long = NUM_DWORDS) as decfloat_struct dim as long dwords, i, j, ex, den, num dim as longint digit, carry, prod dim as decfloat_struct fac1, fac2, result dim as ulong fac3(0 to 2 * NUM_DWORDS + 2) dwords = dwords_in if (dwords > NUM_DWORDS) then dwords = NUM_DWORDS result=si2fp(0) ex = 0 carry = 0 fac1 = x fac2 = y 'check exponents. if either is zero, 'the result is zero if ((fac1.sign = 0) or (fac2.sign = 0)) then 'result is zero...clear fac1. return result else den = dwords while (fac2.mantissa(den) = 0) den-=1 wend num = dwords while (fac1.mantissa(num) = 0) num-=1 wend if (num < den) then fac1=y fac2=x i = den den = num num = i end if for j = den to 0 step -1 carry = 0 digit = clngint(fac2.mantissa(j)) for i = num to 0 step -1 prod = clngint(fac3(i + j + 1)) + digit * clngint(fac1.mantissa(i)) + carry carry = prod \ 100000000 fac3(i + j + 1) = culng(prod mod 100000000) next fac3(j) = culng(carry) next for i = 0 to dwords fac1.mantissa(i) = fac3(i) next end if j = 0 If carry=0 Then j = 8 For i = 0 To dwords+2 fac3(i) = fac3(i+1) next End if if fac3(0) < 10 then j = 7 elseif fac3(0) < 100 then j = 6 elseif fac3(0) < 1000 then j = 5 elseif fac3(0) < 10000 then j = 4 elseif fac3(0) < 100000 then j = 3 elseif fac3(0) < 1000000 then j = 2 elseif fac3(0) < 10000000 then j = 1 end if If j>0 Then LSHIFT_a(fac3(), dwords+j, j) For i = 0 To dwords result.mantissa(i) = fac3(i) next /' now determine exponent of result. '/ /' as you do...watch for overflow. '/ ex = fac2.exponent-bias+fac1.exponent result.exponent = ex-j /' determine the sign of the product '/ result.sign = fac1.sign * fac2.sign norm_fac1(result, dwords) return result end function function fpmul_si(byref x as decfloat_struct, byval y as long, byval dwords_in as long = NUM_DWORDS) as decfloat_struct dim as long dwords, i dim as longint carry, digit, prod, value dim as decfloat_struct fac1, fac2, result dwords = dwords_in if (dwords > NUM_DWORDS) then dwords = NUM_DWORDS fac1 = x if (y < 0) then digit = -y else digit = y end if if (digit > 99999999) then fac2=si2fp(y, dwords) result=fpmul(fac1, fac2, dwords) return result end if 'check exponents. if either is zero, 'the result is zero if ((fac1.exponent = 0) or (y = 0)) then 'result is zero...clear fac1. result=si2fp(0, dwords) return result end if if (digit = 1) and (y < 0)then fac1.sign = -fac1.sign return fac1 end if carry = 0 for i = dwords to 0 step -1 prod = digit * fac1.mantissa(i) + carry value = (prod mod 100000000ll) fac1.mantissa(i) = clng(value) carry = prod \ 100000000ll next if (carry < 10) then rshift_1(fac1, dwords) fac1.exponent += 1 fac1.mantissa(0) += clng(carry) * 10000000 elseif (carry < 100) then rshift_2(fac1, dwords) fac1.exponent += 2 fac1.mantissa(0) += clng(carry) * 1000000 elseif (carry < 1000) then rshift_3(fac1, dwords) fac1.exponent += 3 fac1.mantissa(0) += clng(carry) * 100000 elseif (carry < 10000) then rshift_4(fac1, dwords) fac1.exponent += 4 fac1.mantissa(0) += clng(carry) * 10000 elseif (carry < 100000) then rshift_5(fac1, dwords) fac1.exponent += 5 fac1.mantissa(0) += clng(carry) * 1000 elseif (carry < 1000000) then rshift_6(fac1, dwords) fac1.exponent += 6 fac1.mantissa(0) += clng(carry) * 100 elseif (carry < 10000000) then rshift_7(fac1, dwords) fac1.exponent += 7 fac1.mantissa(0) += clng(carry) * 10 elseif (carry < 100000000) then rshift_8(fac1, dwords) fac1.exponent += 8 fac1.mantissa(0) += clng(carry) end if i = norm_fac1(fac1, dwords) if (y < 0) then fac1.sign = -fac1.sign end if return fac1 end function Sub LSHIFT_da (mantissa() As Double, n As Long, k as long) Dim As Unsigned Long v1, v2, c1, c2 Dim As Long i c1 = p10_constants(k) c2 = p10_constants(4 - k) For i = 2 To n-1 v1 = mantissa(i) Mod c2 v2 = mantissa(i + 1) \ c2 mantissa(i) = v1 * c1 + v2 mantissa(i + 1) = fix(mantissa(i + 1)) Mod c2 Next mantissa(n) = c1 * (mantissa(n) Mod c2) End Sub function realw(w() as double, byval j as long, byval ubw as long) as double dim as double wx wx = ((w(j - 1) * 10000 + w(j)) * 10000 + w(j + 1)) * 10000 if (ubw >= (j + 2)) then wx = wx + w(j + 2) end if return wx end function sub subtract(w() as double, byval q as long, d() as double, byval ka as long, byval kb as long) dim as long j for j = ka to kb w(j) = w(j) - q * d(j - ka + 2) next end sub sub normalize(w() as double, byval ka as long, byval q as long) w(ka) = w(ka) + w(ka - 1) * 10000 w(ka - 1) = q end sub sub finalnorm(w() as double, byval kb as long) const b = 10000 dim as long carry, j for j = kb to 3 step -1 if w(j) < 0 then carry = clng((-w(j) - 1) / b) + 1 else if w(j) >= b then carry = -clng(w(j) / b) else carry = 0 end if end if w(j) = w(j) + carry * b w(j - 1) = w(j - 1) - carry next end sub #Define min(a,b) Iif(((a)<(b)),(a),(b)) function fpdiv(byref x as decfloat_struct, byref y as decfloat_struct, byval dwords_in as long = NUM_DWORDS) as decfloat_struct const b = 10000 dim as long dwords, i, ubn, ubw dim as long j, last, laststep, q, t, stp, carry dim as double xd, xn, rund dim as decfloat_struct fac1, fac2 dwords = dwords_in if (dwords > NUM_DWORDS) then dwords = NUM_DWORDS ubn = 2 * dwords + 3+1 ubw = ubn + 4 fac1 = x fac2 = y if (fac2.exponent = 0) then ' if fac2 = 0, return ' a divide-by-zero error and ' bail out. for i = 0 to dwords fac1.mantissa(i) = 99999999 next fac1.exponent = 99999 + bias + 1 'er=DIVZ_ERR return fac1 end if if (fac1.exponent = 0) then 'fact1=0, just return return fac1 end if dim as double w(ubw + 1) dim as double resul(ubn + 1) dim as double n(ubn + 1) dim as double d(ubn + 1) 'double w(ubw) for j = 0 to dwords n(2 * j + 2) = fac1.mantissa(j) \ 10000 n(2 * j + 3) = fac1.mantissa(j) mod 10000 d(2 * j + 2) = fac2.mantissa(j) \ 10000 d(2 * j + 3) = fac2.mantissa(j) mod 10000 next n(1) = (fac1.exponent and &h7fffffff) - bias - 1 d(1) = (fac2.exponent and &h7fffffff) - bias - 1 for j = ubn to ubw w(j) = 0 next t = ubn - 1 w(1) = n(1) - d(1) + 1 w(2) = 0 for j = 2 to ubn w(j + 1) = n(j) next xd = (d(2) * b + d(3)) * b + d(4) + d(5) / b laststep = t + 2 for stp = 1 to laststep xn = ((w((stp + 2) - 1) * 10000 + w((stp + 2))) * 10000 + w((stp + 2) + 1)) * 10000 if (ubw >= ((stp + 2) + 2)) then xn = xn + w((stp + 2) + 2) end if q = xn / xd last = min(stp + t + 1, ubw) 'subtract(w, q, d, (stp + 2), last) for j = (stp + 2) to last w(j) = w(j) - q * d(j - (stp + 2) + 2) next 'normalize(w, (stp + 2), q) w((stp + 2)) = w((stp + 2)) + w((stp + 2) - 1) * 10000 w((stp + 2) - 1) = q next 'finalnorm(w, (laststep + 1)) for j = (laststep + 1) to 3 step -1 if w(j) < 0 then carry = ((-w(j) - 1) / b) + 1 else if w(j) >= b then carry = -(w(j) / b) else carry = 0 end if end if w(j) = w(j) + carry * b w(j - 1) = w(j - 1) - carry next If (w(2) <> 0) Then laststep-=1 end if rund = w(laststep + 1) / b w(laststep) = w(laststep) If (rund >= 0.5) Then w(laststep)+=1 end if if (w(2) = 0) then for j = 1 to (t + 1) resul(j) = w(j + 1) next else for j = 1 to (t + 1) resul(j) = w(j) next end if ' left-shift out leading 0"s if any to preserve accuracy #if 1 j = 0 if (resul(2) < 10) then j = 3 elseif (resul(2) < 100) then j = 2 elseif (resul(2) < 1000) then j = 1 end if if (j > 0) then LSHIFT_da(resul(), 2 * dwords + 4, j) end if #endif resul(1) = w(1) If w(2) = 0 Then resul(1)-=1 end if for j = 0 to dwords fac1.mantissa(j) = (resul(2 * j + 2) * 10000 + resul(2 * j + 3)) next j = norm_fac1(fac1, dwords) fac1.exponent = (resul(1) + bias) fac1.sign = fac1.sign * fac2.sign return fac1 end function function fp2str_fix (byref n as decfloat_struct, byval places_in as long=num_digits) as String dim as long places, i, ex dim as string v, ts, s, result places=places_in If (places>(num_digits)) Then places=num_digits if n.exponent > 0 then ex = (n.exponent and &h7fffffff) - bias - 1 else ex = 0 end if if n.sign<0 then s = "-" else s = " " end if ts = trim(Str(n.mantissa(0))) if len(ts) < 8 then ts = ts + string(8 - len(ts), "0") v = ts for i = 1 to NUM_DWORDS ts = trim(Str(n.mantissa(i))) if len(ts) < 8 then ts = string(8 - len(ts), "0") + ts v = v + ts next if places < num_digits then v = left(v, places) if ex = 0 then v = left(v, 1) + "." + right(v, len(v)-1) elseif ex < 0 then v = "." + string(abs(ex) - 1, "0") + v elseif ex > 0 then v = left(v, ex + 1) + "." + right(v, len(v)-1-ex) end if result = rtrim(s + v,"0") result = rtrim(result,".") return result end function function fp2str(byref n as decfloat_struct, byval places_in as long=NUM_DIGITS - 2) as String dim as long dwords, ex, e, ee, sign, places dim as decfloat_struct z places=places_in if places>num_digits-1 then places=num_digits-2 else places+=1 end if dwords=NUM_DWORDS if n.exponent <> 0 then ex = (n.exponent and &h7fffffff) - bias - 1 else ex = 0 end if if n.exponent=0 then return " 0" e=ex ee=0 z=si2fp (5, dwords) sign=n.sign if sign=0 then sign=1 if (ex > (-5)) and (e < (places-2)) then if e<0 then ee=1 z.exponent = (-(places-e-ee) + bias + 1) if sign=1 then z=fpadd(z, n, dwords) else z=fpsub(n, z, dwords) end if return fp2str_fix(z, places-ee) else z.exponent = (-(places-e-1) + bias + 1) if sign=1 then z=fpadd(z, n, dwords) else z=fpsub(n, z, dwords) end if return fp2str_exp(z, places-1) end if end function function fpdiv_si(byref num as decfloat_struct, byval den as long, byval dwords_in as long = NUM_DWORDS) as decfloat_struct dim as long dwords, i dim as decfloat_struct fac1, fac2, result dim as ulongint carry, remder dim as longint divisor, quotient dwords = dwords_in if (dwords > NUM_DWORDS) then dwords = NUM_DWORDS remder=0 if (den < 0) then divisor = -den else divisor = den end if fac1 = num if (divisor = 0) then 'print "error: divisor = 0" for i = 0 to dwords result.mantissa(i) = 99999999 next result.exponent = 99999999 result.sign = num.sign return result end if if (divisor > 99999999) then fac2 = si2fp(den, dwords) result = fpdiv(fac1, fac2, dwords) return result end if for i = 0 to (dwords) quotient = fac1.mantissa(i) + remder * 100000000 remder = quotient mod divisor fac1.mantissa(i) = quotient \ divisor next quotient = remder * 100000000 quotient = quotient \ divisor carry = fac1.mantissa(0) if (carry = 0) then lshift_8(fac1, dwords) fac1.exponent -= 8 fac1.mantissa(dwords) += clng(quotient) elseif (carry < 10) then lshift_7(fac1, dwords) fac1.exponent -= 7 fac1.mantissa(dwords) += clng(quotient \ 10) elseif (carry < 100) then lshift_6(fac1, dwords) fac1.exponent -= 6 fac1.mantissa(dwords) += clng(quotient \ 100) elseif (carry < 1000) then lshift_5(fac1, dwords) fac1.exponent -= 5 fac1.mantissa(dwords) += clng(quotient \ 1000) elseif (carry < 10000) then lshift_4(fac1, dwords) fac1.exponent -= 4 fac1.mantissa(dwords) += clng(quotient \ 10000) elseif (carry < 100000) then lshift_3(fac1, dwords) fac1.exponent -= 3 fac1.mantissa(dwords) += clng(quotient \ 100000) elseif (carry < 1000000) then lshift_2(fac1, dwords) fac1.exponent -= 2 fac1.mantissa(dwords) += clng(quotient \ 1000000) elseif (carry < 10000000) then lshift_1(fac1, dwords) fac1.exponent -= 1 fac1.mantissa(dwords) += clng(quotient \ 10000000) end if i = norm_fac1(fac1, dwords) if (den < 0) then fac1.sign = -fac1.sign 'i=norm_fac1(fac1, dwords) return fac1 end function function fpfrac(byref num as decfloat_struct, byval dwords_in as long = NUM_DWORDS) as decfloat_struct dim as long dwords dim as decfloat_struct n dwords = dwords_in if (dwords > NUM_DWORDS) then dwords = NUM_DWORDS n=fpfix( num) return fpsub(num, n, dwords) end function Function fpabs (byref n as decfloat_struct) as decfloat_struct dim as decfloat_struct x x=n x.sign=1 Return x End function Function fpneg (byref n as decfloat_struct) as decfloat_struct dim as decfloat_struct x x=n x.sign =-x.sign Return x End function Function log2_64(byval value as ulongint) as long 'https:'stackoverflow.com/questions/11376288/fast-computing-of-log2-for-64-bit-integers static as long tab64(...) = {63, 0, 58, 1, 59, 47, 53, 2, 60, 39, 48, 27, 54, 33, 42, _ 3, 61, 51, 37, 40, 49, 18, 28, 20, 55, 30, 34, 11, 43, 14, 22, 4, 62, 57, 46, 52, 38, 26, 32, _ 41, 50, 36, 17, 19, 29, 10, 13, 21, 56, 45, 25, 31, 35, 16, 9, 12, 44, 24, 15, 8, 23, 7, 6, 5} value = value Or culngint(value Shr 1) value = value Or culngint(value Shr 2) value = value Or culngint(value Shr 4) value = value Or culngint(value Shr 8) value = value Or culngint(value Shr 16) value = value Or culngint(value Shr 32) return tab64((culngint((value - (value Shr 1)) * &h07EDD5E59A4E28C2ull) Shr 58)) End function function fpsqr(byref num as decfloat_struct, byval dwords_in as long = NUM_DWORDS) as decfloat_struct dim as long dwords, ex, k, prec ', l dim as decfloat_struct r, r2, tmp, n, result dim as double x, y r=si2fp(0) r2=si2fp(0) tmp=si2fp(0) n=si2fp(0) dwords = dwords_in if (dwords > NUM_DWORDS) then dwords = NUM_DWORDS '''l = round(log2((dwords + 1) * 8 * double(0.0625))) + 1 'l = log(((num_digits+8)*0.0625)*1.5) 'l=estimated number of iterations needed 'first estimate is accurate to about 16 digits 'l is approximatly = to log2((NUM_DIGITS+9)/16) 'NUM_DIGITS+9 because decfloat_struct has an extra 9 guard digits n = num tmp=si2fp(0, dwords) if ((fpcmp(n, tmp, dwords) = 0) or (fpcmp(n, tmp, dwords) < 0)) then result=si2fp(0, dwords) return result end if result=si2fp(1, dwords) 'if (fpcmp(n, result, dwords) = 0) then return result '===================================================================== 'hack to bypass the limitation of double exponent range 'in case the number is larger than what a double can handle 'for example, if the number is 2e500 'we separate the exponent and mantissa in this case 2 'if the exponent is odd then multiply the mantissa by 10 'take the square root and assign it to decfloat_struct 'divide the exponent in half for square root 'in this case 1.414213562373095e250 if (n.exponent > 0) then ex = (n.exponent and &h7fffffff) - bias - 1 else ex = 0 end if x = n.mantissa(0) + (n.mantissa(1) / 100000000) x = x / 10000000 if (abs(ex) and 1) then x = x * 10 ex -= 1 end if x = sqr(x) 'approximation y = x * 10000000 r=si2fp(0, dwords) r.mantissa(0) = fix(y) r.mantissa(1) = (y - r.mantissa(0)) * 100000000 r.exponent = ex \ 2 + bias + 1 r.sign=1 'if len(v)>1 and k=0 then r.exponent+=1 '===================================================================== 'newton-raphson method prec = 3 /' for k = 1 to (l + 3) prec = 2 * prec - 1 tmp=fpdiv(n, r, prec) r2=fpadd(r, tmp, prec) r=fpdiv_si(r2, 2, prec) next '/ If prec > dwords Then prec = dwords Do tmp = fpdiv(num, r, prec) r2 = fpadd(r, tmp, prec) r = fpdiv_si(r2, 2, prec) If prec >= dwords Then Exit Do prec = 2 * prec - 1 If prec > dwords Then prec = dwords Loop return r end function function getExponent(byref n as decfloat_struct) as long dim as long ex If n.exponent<>0 Then ex=(n.exponent And &h7FFFFFFF)-BIAS-1 Else ex=0 end if return ex end function Sub MyFrExp( _ ByVal x As Double, _ ByRef y As Double, _ ByRef e As Long _ ) Dim xUInt As ULongInt Dim ExpBin As Long ' transfer Double to ULongInt xUInt = Cast(ULongInt Ptr, @x)[0] ' get biased exponent ExpBin = (xUInt Shr 52) And &h7ff ' sort out zero, de-normal, NaN and Inf cases If (ExpBin <> 0) And (ExpBin <> &h7ff) Then xUInt = (xUInt And &h800fffffffffffffULL) Or &h3fe0000000000000ULL y = Cast(Double Ptr, @xUInt)[0] e = ExpBin - &h3fe Else If (ExpBin = 0) And ((xUInt And &hfffffffffffffULL) = 0) Then e = 0 y = Cast(Double Ptr, @xUInt)[0] Else ' none of the remaining values - de-normal, NaN or Inf - can begin ' 'normalised' in a meaningful way. Add whatever you like in error ' handling here End If End If End Sub sub pi_chudnovsky_binary_split(byval a as ulongint, byval b as ulongint, byref pab as decfloat_struct, byref qab as decfloat_struct, byref tb as decfloat_struct, byval dwords_in as long = NUM_DWORDS) dim as long dwords dim as decfloat_struct c545140134, c13591409, temp, c3_over_24 dim as decfloat_struct pam, qam, tam, pmb, qmb, tmb, result dim as ulongint m, t dwords = dwords_in if (dwords > NUM_DWORDS) then dwords = NUM_DWORDS c545140134=si2fp( 545140134, dwords) c13591409=si2fp(13591409, dwords) c3_over_24=si2fp(0, dwords) c3_over_24.exponent = 1073741841 c3_over_24.mantissa(0) = 10939058 c3_over_24.mantissa(1) = 86003200 if ((b - a) = 1) then if (a = 0) then pab=si2fp(1, dwords) qab=si2fp(1, dwords) else t = 6 * a pab=si2fp(t - 1, dwords) pab=fpmul_si( pab, a + a - 1, dwords) pab=fpmul_si(pab, t - 5, dwords) temp=si2fp(a, dwords) qab=fpmul(temp, temp, dwords) qab=fpmul(qab, temp, dwords) qab=fpmul(qab, c3_over_24, dwords) end if tb=c545140134 tb=fpmul_si(tb, a, dwords) tb=fpadd(tb, c13591409, dwords) tb=fpmul(tb, pab, dwords) if ((a mod 2) <> 0) then tb.sign = -tb.sign end if else m = (a + b) \ 2 pi_chudnovsky_binary_split(a, m, pam, qam, tam, dwords) pi_chudnovsky_binary_split(m, b, pmb, qmb, tmb, dwords) pab=fpmul(pam, pmb, dwords) qab=fpmul(qam, qmb, dwords) temp=fpmul(qmb, tam, dwords) tb=fpmul(pam, tmb, dwords) tb=fpadd(tb, temp, dwords) end if end sub function pi_chudnovsky_bs(byval digits_in as long = num_digits) as decfloat_struct dim as long dwords, digits dim as decfloat_struct p, q, sqrtc, t, result dim as ulongint n dim as double digits_per_term dwords = (digits_in \ 8 - ((digits_in Mod 8) <> 0)) if (dwords > NUM_DWORDS) then dwords = NUM_DWORDS digits=8*dwords digits_per_term = 14.18164746272548 n = (digits / digits_per_term + 1) pi_chudnovsky_binary_split(0, n, p, q, t, dwords) sqrtc=si2fp(10005, dwords) sqrtc=fpsqr( sqrtc, dwords) result=fpmul_si(sqrtc, 426880, dwords) result=fpmul(result, q, dwords) return fpdiv(result, t, dwords) end function Sub binarysplittingRamanujanPI (byval a As ulongint, byval b As ulongint, byref p As decfloat_struct, byref q As decfloat_struct, byref r As decfloat_struct) Dim As decfloat_struct pp, qq, rr, one, three, bb Dim As ulongint n1, n2, n3, md one = si2fp (1, NUM_DWORDS) three=si2fp (3, NUM_DWORDS) If (b - a) = 1 Then If b <= 832256 Then n1 = (2 * b) - 1 n2 = (4 * b) - 3 n3 = (4 * b) - 1 r=si2fp (n1, NUM_DWORDS) r=fpmul_si (r, n2, NUM_DWORDS) r=fpmul_si (r, n3, NUM_DWORDS) Else bb=si2fp (b, NUM_DWORDS) r=fpmul_si (bb, 4, NUM_DWORDS) r=fpsub (r, three, NUM_DWORDS) p=fpmul_si (bb, 4, NUM_DWORDS) p=fpsub (p, one, NUM_DWORDS) r=fpmul (r, p, NUM_DWORDS) p=fpmul_si (bb, 2, NUM_DWORDS) p=fpsub (p, one, NUM_DWORDS) r=fpmul (r, p, NUM_DWORDS) End If p=si2fp (b, NUM_DWORDS) p=fpmul_si (p, 26390, NUM_DWORDS) one=si2fp (1103, NUM_DWORDS) p=fpadd (p, one, NUM_DWORDS) p=fpmul (p, r, NUM_DWORDS) q=si2fp (b, NUM_DWORDS) one=fpmul (q, q, NUM_DWORDS) q=fpmul (q, one, NUM_DWORDS) one=si2fp (3073907232, NUM_DWORDS) q=fpmul (q, one, NUM_DWORDS) Exit Sub End If md = (a + b) / 2 binarysplittingRamanujanPI a, md, p, q, r binarysplittingRamanujanPI md, b, pp, qq, rr pp=fpmul (pp, r, NUM_DWORDS) p=fpmul (p, qq, NUM_DWORDS) p=fpadd (p, pp, NUM_DWORDS) q=fpmul (q, qq, NUM_DWORDS) r=fpmul (r, rr, NUM_DWORDS) End Sub function Pi_Ramanujan (byval digits As Long=num_digits) As decfloat_struct Dim As Long prec prec=(digits \ 8 - ((digits Mod 8) <> 0)) If prec > NUM_DWORDS Then prec = NUM_DWORDS Dim As ulongint k Dim As decfloat_struct p, q, r, pi1 k = -Int(-(prec *8* Log(10) / Log(96059301))) binarysplittingRamanujanPI 0, k, p, q, r r=si2fp (8, prec) r=fpsqr (r, prec) pi1=fpmul_si (q, 1103, prec) pi1=fpadd (pi1, p, prec) pi1=fpmul (pi1, r, prec) q=fpmul_si (q, 9801, prec) return fpdiv (q, pi1, prec) End function function fpipow(byref x as decfloat_struct, byval e as longint, byval dwords_in as long = NUM_DWORDS) as decfloat_struct dim as long dwords dim as decfloat_struct one, y, r dim as longint n dwords = dwords_in if (dwords > NUM_DWORDS) then dwords = NUM_DWORDS 'take x to an long power y = x if (e < 0) then n = -e else n = e end if r=si2fp(1, dwords) one = r while (n > 0) while ((n and 1) = 0) n=n shr 1 y=fpmul(y, y, dwords) wend n -= 1 r=fpmul(y, r, dwords) wend if (e < 0) then r=fpdiv(one, r, dwords) return r end function function dbl2fp(byval x as double, byval dwords_in as long = NUM_DWORDS) as decfloat_struct dim as long dwords, n dim as decfloat_struct fp dim as string s dwords = dwords_in if (dwords > NUM_DWORDS) then dwords = NUM_DWORDS s=str(x) fp=str2fp(s) return fp end function function fp2i(byref x as decfloat_struct) as long dim as long e, ex, n dim as decfloat_struct tmp tmp=si2fp(2147483647, 2) if (fpcmp_abs(x, tmp, 3)>0) then return 2147483647 if (x.exponent <> 0 ) then ex=(x.exponent and &h7fffffff)-bias-1 else ex=0 end if e=abs(ex) tmp=si2fp(5, 3) tmp.exponent-=1 tmp=fpadd(tmp, x, 3) tmp=fpfix(tmp) if (e>7) then n=tmp.mantissa(0)*clng(p10_constants(e-7))+(tmp.mantissa(1) \ clng(p10_constants(15-e))) else n=tmp.mantissa(0) \ clng(p10_constants(7-e)) end if if (x.sign<0) then n=-n return n end function function fp2ui(byref x as decfloat_struct) as ulong dim as long e, ex dim as ulong m dim as decfloat_struct tmp tmp=si2fp(0) if (fpcmp(x, tmp, 3)<0) then return 0 tmp=si2fp(4294967295, 2) if (fpcmp(x, tmp, 3)>0) then return 4294967295 if (x.exponent <> 0 ) then ex=(x.exponent and &h7fffffff)-bias-1 else ex=0 end if e=abs(ex) tmp=si2fp(5, 3) tmp.exponent-=1 tmp=fpadd(tmp, x, 3) tmp=fpfix(tmp) if (e>7) then m=tmp.mantissa(0)*culng(p10_constants(e-7))+(tmp.mantissa(1) \ culng(p10_constants(15-e))) else m=tmp.mantissa(0) \ culng(p10_constants(7-e)) end if return m end function function fp2ui64(byref x as decfloat_struct) as ulongint dim as long e, ex dim as ulongint m dim as decfloat_struct tmp tmp=si2fp(0) if (fpcmp(x, tmp, 3)<0) then return 0 tmp.mantissa(0)=18446744 tmp.mantissa(1)=7370955 #if (NUM_DWORDS>1) tmp.mantissa(2)=16150000 #endif tmp.exponent=19+bias+1 if (fpcmp(x, tmp, 3)>0) then return 18446744073709551615 if (x.exponent <> 0 ) then ex=(x.exponent and &h7fffffff)-bias-1 else ex=0 end if e=abs(ex) tmp=si2fp(5, 3) tmp.exponent-=1 tmp=fpadd(tmp, x, 3) tmp=fpfix(tmp) if (e>15) then m=(culngint(tmp.mantissa(0))*100000000+culngint(tmp.mantissa(1)))*culngint(p10_constants(e-15)) #if (NUM_DWORDS>1) m=m+(culngint(tmp.mantissa(2)) \ culngint(p10_constants(23-e))) #endif elseif (e>7) then m=culngint(tmp.mantissa(0))*culngint(p10_constants(e-7))+culngint(tmp.mantissa(1)) \ culngint(p10_constants(15-e)) else m=culngint(tmp.mantissa(0)) \ culngint(p10_constants(7-e)) end if return m end function function ui2fp(byval m as ulongint, byval dwords_in as long=NUM_DWORDS) as decfloat_struct dim as long dwords, ex, i dim as ulongint mm dim as decfloat_struct result dwords=dwords_in if (dwords > NUM_DWORDS) then dwords = NUM_DWORDS mm=m for i = 0 to dwords result.mantissa(i) = 0 next if (mm = 0) then result.exponent = 0 result.sign = 0 return result end if ex = clng(log10_64(mm)) result.exponent = bias + ex + 1 if (ex > 15) then ex -= 7 result.mantissa(0) = (mm \ culngint(p10_constants(ex))) mm = mm mod culngint(p10_constants(ex)) ex -= 8 result.mantissa(1) = (mm \ culngint(p10_constants(ex))) mm = mm mod culngint(p10_constants(ex)) #if (NUM_DWORDS>1) result.mantissa(2) = (mm * culngint(p10_constants(8 - ex))) #endif elseif (ex >= 8) then ex -= 7 result.mantissa(0) = (mm \ culngint(p10_constants(ex))) mm = mm mod culngint(p10_constants(ex)) result.mantissa(1) = (mm * culngint(p10_constants(8 - ex))) elseif (ex < 8) then result.mantissa(0) = (mm * culngint(p10_constants(7 - ex))) end if result.sign=1 return result end function '================================================================================ sub init_constants fppi ="3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230665" fpln2 ="0.693147180559945309417232121458176568075500134360255254120680009493393621969694715605863326996418687542001481020570685734" fpPihalf ="1.57079632679489661923132169163975144209858469968755291048747229615390820314310449931401741267105853399107404325664115332" fpPi2 ="6.28318530717958647692528676655900576839433879875021164194988918461563281257241799725606965068423413596429617302656461329" Pi3o2 ="4.71238898038468985769396507491925432629575409906265873146241688846172460942931349794205223801317560197322212976992345997" fpdtor ="57.2957795130823208767981548141051703324054724665643215491602438612028471483215526324409689958511109441862233816328648933" fpPIo6 ="0.523598775598298873077107230546583814032861566562517636829157432051302734381034833104672470890352844663691347752213717775" fpexp1 ="2.71828182845904523536028747135266249775724709369995957496696762772407663035354759457138217852516642742746639193200305992" fpSQR3 ="1.73205080756887729352744634150587236694280525381038062805580697945193301690880003708114618675724857567562614141540670303" fpTMS3 ="0.267949192431122706472553658494127633057194746189619371944193020548066983091199962918853813242751424324373858584593296970" fpconst1olog10 = "0.434294481903251827651128918916605082294397005803666566114453783165864649208870774729224949338431748318706106744766303734" sqrt2pi ="2.50662827463100050241576528481104525300698674060993831662992357634229365460784197494659583837805726611600997266520387964" ExpAn(0 )="1" ExpAn(1 )="1" ExpAn(2 )=".4999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999961117" ExpAn(3 )=".1666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666682274920798" ExpAn(4 )=".4166666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666656237937087521742e-1" ExpAn(5 )=".8333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333336109858401870340372214e-2" ExpAn(6 )=".1388888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888849541814925118828401547483e-2" ExpAn(7 )=".1984126984126984126984126984126984126984126984126984126984126984126984126984126984126984130422306090789583780127756903696e-3" ExpAn(8 )=".2480158730158730158730158730158730158730158730158730158730158730158730158730158730158527719926815220970311353352747237453e-4" ExpAn(9 )=".2755731922398589065255731922398589065255731922398589065255731922398589065255731930918232062923990455497298592143515572556e-5" ExpAn(10)="2.755731922398589065255731922398589065255731922398589065255731922398589065255464747980554762647870930856211515270205401544e-7" ExpAn(11)="2.505210838544171877505210838544171877505210838544171877505210838544171883942503104765038135831690471509942149683169281806e-8" ExpAn(12)="2.087675698786809897921009032120143231254342365453476564587675698786687950644468674395972397984097675001411197745417420120e-9" ExpAn(13)="1.605904383682161459939237717015494793272571050348828126605904385530932804486554378333580307327616965087649588241294437707e-10" ExpAn(14)="1.147074559772972471385169797868210566623265035963448661861337535947250011108157872615077642873308889963236482635784501917e-11" ExpAn(15)="7.647163731819816475901131985788070444155100239756324414701611322414003023692019578041035553431994070562963779375081323500e-13" ExpAn(16)="4.779477332387385297438207491117544027596937649847683660680040771280131023431956696921916454602481126183843122803218681768e-14" ExpAn(17)="2.811457254345520763198945583010320016233492735336697288433230879881820679396670870157985820716416476158391526893143801392e-15" ExpAn(18)="1.561920696858622646221636435005733342351939645813606542549314908096564720237811669508408661284759225102971491283431774169e-16" ExpAn(19)="8.220635246624329716955981236872280749257488474137131373628376772891351998996219279905975426051288935571998026974127402877e-18" ExpAn(20)="4.110317623312164858477990618436140226774668352324392838507599172248134501951148261499233151088650330658930366144982890114e-19" ExpAn(21)="1.957294106339126123084757437351038307875435281983196685229297808699188981919675216235246017887364292706391796947744780314e-20" ExpAn(22)="8.896791392450573286748897428750955438706642667917828842333556544468870256427171836955060794219076581434090099154180442146e-22" ExpAn(23)="3.868170170630684037716943332051792267914055843146803217720980926981325427853199378982863163800592759273641779196986658577e-23" ExpAn(24)="1.611737571096118348990423155090252114353420717174424065972942724765570934620950842475710488491806909237741908534621554323e-24" ExpAn(25)="6.446950284384474261215566725913820377060917375552685540909431055221031259301197056072844720586689400619096431266967511872e-26" ExpAn(26)="2.479596263223796307329068401901725171947153210311180885258730280671330490066264466268514383358223686335828222660311613097e-27" ExpAn(27)="9.183689872498303981159758954165118026225938797807512835800094213864564847614858793637580280302699853578561887658434686523e-29" ExpAn(28)="3.279883894958889552315828253154834591621809795045356912645897347000800202326008290146291586770204651546506708773479046509e-30" ExpAn(29)="1.133059920109944927680124570888332047735661955149099575740337478861696056000411548468552425884248499208974706593635700258e-31" SinAn(0 )=" 1" SinAn(1 )="-.1666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666667" SinAn(2 )=" 0.8333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333279212e-2" SinAn(3 )="-0.1984126984126984126984126984126984126984126984126984126984126984126984126984126984126984126984126984126984126982433896575e-3" SinAn(4 )=" 0.2755731922398589065255731922398589065255731922398589065255731922398589065255731922398589065255731922398588854267302280114e-5" SinAn(5 )="-2.505210838544171877505210838544171877505210838544171877505210838544171877505210838544171877505210838530183525102281872036e-8" SinAn(6 )=" 1.605904383682161459939237717015494793272571050348828126605904383682161459939237717015494793272570478899560176957900899055e-10" SinAn(7 )="-7.647163731819816475901131985788070444155100239756324412409068493724578380663036747692832348759845399758952890977219451819e-13" SinAn(8 )=" 2.811457254345520763198945583010320016233492735204531033973922240339918522302587039592949983656288236096991572798353404429e-15" SinAn(9 )="-8.220635246624329716955981236872280749220738991826114134426673217368182813750254501282932812625865093677900129104893022087e-18" SinAn(10)=" 1.957294106339126123084757437350543035528747379006217651053969813659091146131007924227814530027780015004704319803628533090e-20" SinAn(11)="-3.868170170630684037716911931522812323179342646257347136470296074425081316020177067024180381917727311715967852141276687320e-23" SinAn(12)=" 6.446950284384473396194853219204687205298904410428911894117160124041771003315498998424892554556514868753554717270880231719e-26" SinAn(13)="-9.183689863795546148425716836473913397861687194343179336349230944156930883813989757955420086367311595472516516606695216646e-29" SinAn(14)=" 1.130996288644771693155876457693831699244050147086598440437089185378504444537667421461339268039813465383736351215788317094e-31" SinAn(15)="-1.216125041553517949629974685692292149724785104394191871123690783858902702294154335864001544573758661804026757574604681630e-34" SinAn(16)=" 1.151633562077195028058688149329822111481804076130853593278395607329703346123533334829303960108921916804307185639841508400e-37" SinAn(17)="-9.677592958631890992089816380922874886401714922870148240171862811359755951307521667789138924460875715116687265009445862424e-41" SinAn(18)=" 7.265460179153071315382745030722879043845074820056419546652747679140688983246811832397785025098568588693836714487118849181e-44" SinAn(19)="-4.902469756513543397694159939759027693884738786766986140072510393249036879117208769456062133961457305387147546755715140448e-47" SinAn(20)=" 2.989310827142404510789121914487196895250435035852581208087748506017153947026219677265592492857774377893956810230876215587e-50" SinAn(21)="-1.655210867742195188698295633529998794205639418794038238482326894919308751192975210215829948037719333806029759755204477685e-53" SinAn(22)=" 8.359650847182803983324707309584274871276699260984835936899625000371354694581599398024791895451345602396316035385899204916e-57" SinAn(23)="-3.866628513960593886687018723432760536142067197177119255550473824296536250864584987338178782994373762444894646597253671335e-60" SinAn(24)=" 1.643974708316578165753096998071834619387381987427181986831853060309738539071237192697810917906011640073144073758415384568e-63" SinAn(25)="-6.446959640417502506574940218671581716059501575605026719186956142991749800283761962313332229181514372515405884485314280220e-67" SinAn(26)=" 2.339245024809057101509430254450653434406763802770121901556920104908315214521802202800467707268715409836900842311263798593e-70" SinAn(27)="-7.873662767408033237104715044279536302464693946411142264171854725558111825094008652889107261678391239091667352381956720052e-74" CosAn(0 )=" 1" CosAn(1 )="-.4999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999998" CosAn(2 )=" 0.4166666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666358179e-1" CosAn(3 )="-0.1388888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888879238396735e-2" CosAn(4 )=" 0.2480158730158730158730158730158730158730158730158730158730158730158730158730158730158730158730158730158728956109394384839e-4" CosAn(5 )="-2.755731922398589065255731922398589065255731922398589065255731922398589065255731922398589065255731922318856334880261329190e-7" CosAn(6 )=" 2.087675698786809897921009032120143231254342365453476564587675698786809897921009032120143231254339108228427773419937616167e-9" CosAn(7 )="-1.147074559772972471385169797868210566623265035963448661861360274058686757099455512153924852247970758219054712343792757239e-11" CosAn(8 )=" 4.779477332387385297438207491117544027596937649847702757755667808577861487914397967308002629213745178553680706353541248821e-14" CosAn(9 )="-1.561920696858622646221636435005733342351940408446961685541067911299954734612548355072437755692273604973649725635502967434e-16" CosAn(10)=" 4.110317623312164858477990618436140374610369495913057067213336608684091406875098452638993320257094514742313839927607687363e-19" CosAn(11)="-8.896791392450573286748897442502468343312488086391898413881680971177687025335898058192655583656200380069678179230444364416e-22" CosAn(12)=" 1.611737571096118349048713304801171801324726102607227973529290031010432769966707602068406109735238457743463373219533069529e-24" CosAn(13)="-2.479596263224797460074943545847956617422655542472658420814292354390913470598967773752511124469587104213908514661480424156e-27" CosAn(14)=" 3.279889237069837910152041727312111927807745426551135477267535617181425764054737359826908206988289590207052410472319852534e-30" CosAn(15)="-3.769987628815905643852921525646105664146833823621994799666934647894667871966899088259890393267273632803594440081924266747e-33" CosAn(16)=" 3.800390754854743592593670892788412967889953451231793047196293556820611905607898240926556998848297827030496581302056391501e-36" CosAn(17)="-3.387157535521161847231435733323006210240600222432722888493262155788841506001880616991389472908848344264689633667032058484e-39" CosAn(18)=" 2.688220266286636386691615661367465246222666382680169264073961839046109137184090238124895849393440568343183343719485751789e-42" CosAn(19)="-1.911963205040281925100722376506020800431902436767308636904950148591146416236324911674190572879286154612802592684014545068e-45" CosAn(20)=" 1.225617439128385849423539984939748308926012259566910947344257983889265501632267993233100960196951217880379290994645607972e-48" CosAn(21)="-7.117406731291439311402671223921614402729862859367610203444065496732280795437718794404210294346024278110454738146353988205e-52" CosAn(22)=" 3.761842881232261792496116115827989461015360574924432625719339470150810065988215576091256416241353464931603013254374266155e-55" CosAn(23)="-1.817315401561479126728681670125120852122694991947008296325526449071490287849414489083759760690370469069640947128627904686e-58" CosAn(24)=" 8.055476070751232318024955533528421673808787372586925612557073064044620102955700861554546676421709091468446959175669691028e-62" CosAn(25)="-3.287949416610546260516738925030594762925254904806493652992790686660080043991607540853961931008212443883568692525858419746e-65" CosAn(26)=" 1.239799858039287348293423440377113558596512343002249774862514519764550391177038648829840163407963762516301653541420071379e-68" CosAn(27)="-4.330462859679986215651472642018104241395619306229983704837663013135815728557692331079445456608889720715959871686942332402e-72" TanAn(0 )=" 1" TanAn(1 )=" .3381427705179460999629525894050280822954486647553971682079876024433697492874791033170221562212468975062406115683192381044" TanAn(2 )="-0.3062428339002068769423683982622332380130801870288921183226233006602426697482875288391504447911043916462139376086790752665e-1" TanAn(3 )=" 0.9042904373192817809461509864554329990346257454762909939807351254789341782380937158627014736230783578935709131165513825091e-3" TanAn(4 )="-0.1248162389242236321507034717370135978087901280334640973978202944323693698272586037417900172640170788972355765032249365154e-4" TanAn(5 )=" 9.382666676659686111759300177016711476220525885864071218056099266289677554013190395641663943210656754864359435838077250815e-8" TanAn(6 )="-4.154830382587421625555884721140299171994200421655214523454689846997830830688611816071823382499004556234840057763308651476e-10" TanAn(7 )=" 1.127291252446310564056149161894679967545289331127038621020183176394284926149781920172650755942902232518865810148184192097e-12" TanAn(8 )="-1.897065833342428458007165576099870588973603114171111780860060574781105174178604145366747763095885186821982837743514307644e-15" TanAn(9 )=" 1.955701281411211124319964810651999909188007741454823894358017792419401108810771994567106767626818775957475280176331369291e-18" TanAn(10)="-1.184495385066249724094932815570454376455545541269946222702317757244824431499818179633590595724860491201220421871306954059e-21" TanAn(11)=" 3.862612442075900614153708885285083282790536006297571390876565467690051192180336524685864763587718514110265779860148445000e-25" TanAn(12)="-5.675345309650311747076712234207291584532838319062994255634388860520334037426530041694213043122582035391058409137580080113e-29" TanAn(13)=" 2.421777590733945174014124534836524060994840103652711097679794213002578878046923252851031663140820081826974420708559192114e-33" TanBn(0)=" 1.014428311553838299888857768215084246886345994266191504623962807330109247862437309951066468663740692518721834704957714321" TanBn(1)="-.4976441747915973830382536267647036701584624538151442373463721131301165000694611826321717209028275945013529151645869882212" TanBn(2)=" 0.3752976697702297857613436023994841280269950730986508621946850087027523862286604687504603118882320837119072461555229279962e-1" TanBn(3)="-0.1033475020395003481216814115633338801172383042553750936785329235973085473685656938806396844750076808075363947714552115664e-2" TanBn(4)=" 0.1374306796194102907051690866850061168741875384185675714860259757155480244747260788318904854762308420677413242899661499384e-4" TanBn(5)="-1.009158191033954660379219499458767107145913870065621348164719832082487599004287984989439827729651978441731336064117102589e-7" TanBn(6)=" 4.396391513900540701891677594491471000148727869579547496422491387353798834802869161309032806428232678239045782003418637429e-10" TanBn(7)="-1.178353572419216021223749319030337334598203022510948634592184189366946813108267873270696185060424926376142304454231100360e-12" TanBn(8)=" 1.963950166936637800783318296814876527995655198903967727310065294702778853933731822993802198997370708647248667556976443678e-15" TanBn(9)="-2.008558700443658833248576785612030560742521583004083748406281295013766265603137025843334433193720058464017617265148826816e-18" TanBn(10)=" 1.208185663158194155040917259536457054577143107556095533819566691169299149394568438707159138451275833971003732029353358744e-21" TanBn(11)="-3.915817567409817779692227286414907648224624046391218252191164899878287565557886519047861053514748441113046555337656457560e-25" TanBn(12)=" 5.721115474304089868623242072305878930385251575994336385012885462407892119456919225709905590167119009099550401348999128480e-29" TanBn(13)="-2.428201638420431044400256983329689684084429328920471750251184027472108208392163686563993340151274701918733503821381890167e-33" AtanAn(0 )="-.3243573609350119752652601107872512026906964993525644843517358185107429279106553646348543679189866793884014390061310059727" AtanAn(1 )="-2.676753561499818417369589325143647999626060804204368623546019782427145605177468443375607145111622694647532125335377588914" AtanAn(2 )="-10.05881814998381194911459688725715958337892260775662309967662436784536153748154101409725386715699988178263559445915690000" AtanAn(3 )="-22.78474352147614089479614292724552820410774513683108653587968503074936743340195590446512580613961256662036930780409066748" AtanAn(4 )="-34.70911639508491280093025628624537456079269395405203181965570240610134841117272120729461909415743966520941370281305472391" AtanAn(5 )="-37.56452773937188945785119977268423999488138707628935287818627915731884318221800913028363109808370107923947024824222396634" AtanAn(6 )="-29.74698706206586995159635175538703290300019427568988453913951085260723450952169474535906234710225237428281809559864024303" AtanAn(7 )="-17.49032871799773592278525069783761658296173552727478833210589994310350740929615494057904237362138524728246982191136845821" AtanAn(8 )="-7.668182882161233744481429438758393096998418766889453899451992821302196170702149997637453443508128316661769481678660059704" AtanAn(9 )="-2.495819665531749920564507810968804118398476234664143394243067919927006470883714059103174055714163210961016325766780654265" AtanAn(10)="-.5952869989214454629507890718139232270150340709006978606884935234379028947471499960853791983887401224317574575797042754544" AtanAn(11)="-.1016945299818784214597005029026200688855819622685494188133664779668236366310996268882195535536948298788026143220245174766" AtanAn(12)="-0.1200293890845295511295631899977280449132916143603249724649981918355391794348665153298665449676395807287417652707028979937e-1" AtanAn(13)="-0.9257025834124583009787974321918213260200106509575705541083236866680203178206578460154905837838366720784896416897491058386e-3" AtanAn(14)="-0.4264235318707588038657265165391421895277200588531782192713832293208664869836151221034308914032723986535877392824619362044e-4" AtanAn(15)="-9.997830249328112187200980972250152567988866722464794139649414323169887965466018182754938120213459504731921892166473201199e-7" AtanAn(16)="-8.318240086712091447468980342748459984395594095686465797307287276809472882787091491890065214436737121786875149047705881167e-9" AtanAn(17)="-8.582902977025633486917907333913029153697360107259664168089673919018171439896246977607757221522810086216287935790957246925e-13" AtanBn(0 )=" .9730720828050359257957803323617536080720894980576934530552074555322287837319660939045631037569600381652043170183930179240" AtanBn(1 )=" 8.614103934182476807586236174847996163721436111447721942471183820600774085771584986469559297589044106841718966217166242054" AtanBn(2 )=" 34.92788591783019224941162650995381061633873399097091955606035162923987958516530285107154156412858609309642202739850981990" AtanBn(3 )=" 85.94356064712620280604942072356126668607941397095078070742506596518815551388502550875763131156252856074662173393018105384" AtanBn(4 )=" 143.3303762914964532386596204272411866216468310113833555110317930017155580756064654976409727600112988620305548166723734397" AtanBn(5 )=" 171.3767370923092582060063752401871757452702780811947529259410538994539842352612515856422151367014007208990619872685859038" AtanBn(6 )=" 151.5550215192955251144091083637620628309464516429413473467589618483704306930229660444053028209100948495939958496629290753" AtanBn(7 )=" 100.8036444010777783589190396917733079374511481424110079366366289799498319924402078217180779636533721127982704687332606567" AtanBn(8 )=" 50.78410483773071154446562331245417152903980121434687579543114363655531703578096258763251087937517832262992979730434013434" AtanBn(9 )=" 19.36575215409280218790582186052845678927666883611325406207814525824810221004047649786282006273520283653291981543543829583" AtanBn(10)=" 5.546370753420526857852973961516670559765151100835101463124482042211839664188506057334269370124446337468916543572596778346" AtanBn(11)=" 1.174766008822037318301586917183550625981565505546409191247746989165725765480838860294353029301221469715991540749484076104" AtanBn(12)=" .1795230905259450803744736237903138655683146462304723605418046432204412771406209153923371275316415523059902216263105419957" AtanBn(13)=" 0.1906489129426966155026362060372065786450413664990764388492386982955037746726696986932827189290176565241940564920353945428e-1" AtanBn(14)=" 0.1328978068773886988023740857711094903819229232541417385681636034436953828433541051227008762386850959785180429600477501971e-2" AtanBn(15)=" 0.5551526200481775009634326009639727590415696743444694333105614507291915152775290501860977250536420230712942717364187933090e-4" AtanBn(16)=" 0.1182044649569231362247721613842563009741568930824602421778317172716074555023282056559670323245988066200054397936453863934e-5" AtanBn(17)=" 8.891587756167457579712850942853443604539069793706607489434939272033484962670729752101466502560723117722018940395407407502e-9" gamma_coeff(0 )=" 1" gamma_coeff(1 )=" 0.83333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333e-1" gamma_coeff(2 )=" 0.34722222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222e-2" gamma_coeff(3 )="-0.26813271604938271604938271604938271604938271604938271604938271604938271604938271604938271604938271604938271604938271604938e-2" gamma_coeff(4 )="-0.22947209362139917695473251028806584362139917695473251028806584362139917695473251028806584362139917695473251028806584362140e-3" gamma_coeff(5 )=" 0.78403922172006662747403488144228884969625710366451107191847932588673329414070154810895551636292377033117773858514599255340e-3" gamma_coeff(6 )=" 0.69728137583658577742939882857578330829359635943998083915779389030417837002199142116837590088618895203257343175038648289677e-4" gamma_coeff(7 )="-0.59216643735369388286483622560440118739158519679781682525166750132456579644508313918464810097183211037806373883191441490481e-3" gamma_coeff(8 )="-0.51717909082605921933705784300205882281785345342729887768753795274142782144611134094444565409357361803635237562992478401715e-4" gamma_coeff(9 )=" 0.83949872067208727999335751676498344519818211159300762850357255004243098876369549877992019264521762447214190681480559378576e-3" gamma_coeff(10)=" 0.72048954160200105590857193022501505206345173797547895250448981047217056213620003660397876984747130525979246452719250532850e-4" gamma_coeff(11)="-0.19144384985654775265008988583285225448768935789535038961194854860782566236766635843324103571780575033455947262532968727474e-2" gamma_coeff(12)="-0.16251626278391581689863512398027099810587259193225188703848805674978617230296350226132035595691209719113427339128034359684e-3" gamma_coeff(13)=" 0.64033628338080697948236380902657958304018940939628646763014956654075946278083048102235925093393311306056457008671397843041e-2" gamma_coeff(14)=" 0.54016476789260451518046750857024173554725441597921717504212201410757237708941782157619300841296442640519795332917700920730e-3" gamma_coeff(15)="-0.29527880945699120505440651054693824446565482825437969056208248980919249456618949230994786051870802178758918239054758211378e-1" gamma_coeff(16)="-0.24817436002649977309156583687434643239751680472290206759453220106206065427888466515725528426253371204671940872710018902278e-2" gamma_coeff(17)=" .17954011706123485610769940772222633053091282338692173187581192489531068395002944612338031928482709435369210062683038731014" gamma_coeff(18)=" 0.15056113040026424412384221877131127260259815455414920738292925331668273325761970295978299164655095143657415393567717958179e-1" gamma_coeff(19)="-1.3918010932653374813991477635422731493580456177264624679865925370475240492000148358261164828693684708993752557588272078452" gamma_coeff(20)="-.11654627659946320085073403690714796967893733438370661851220722611792145300338240888041062245062161668262042489245857786210" gamma_coeff(21)=" 13.397985455142589217626930432019671950420585565033163574185888348949147269953472142131426106968906594216702950631633361942" gamma_coeff(22)=" 1.1208044642899116068626394001399239410087445805897861376438469597990390290051015909765568838952952854146649726070808629087" gamma_coeff(23)="-156.80141270402272637282369844604118986429592535258608066515346152119917884790801835969326071638250947656247340370182809561" gamma_coeff(24)="-13.107863022633865659027505322267172656213954267262501047270600394871882309341341415448461852229787523817062657197734965881" gamma_coeff(25)=" 2192.5555360905234329690129668354049891217444393378924336055247865120374843324794053890788559730346654173520587944954557589" gamma_coeff(26)=" 183.19073348452433808866211206047526830490081016659783259037716120869556287843205198506093733025025821194362260469593985867" gamma_coeff(27)="-36101.119293222075951913791014310212311727440812019422740892542767701830912510375493644009068353029597425304947942086275396" gamma_coeff(28)="-3015.0773126223058542158273842951345851261670776557135575828020272318124806795532509855689541910437899442660821021210630806" gamma_coeff(29)=" 6.9134637614187812160020149423620785956471176792003329425882749848074393412661212151831682657550316878709244909804613012204e5" gamma_coeff(30)=" 57721.336363040722716587219971632365575408399654732472871054287310320067918234257444294258370298804998604268424119035310980" gamma_coeff(31)="-1.5235812145715274206827871380493290594176042259953992430095143570468064128662819995262084599443572596795109240915620089454e7" gamma_coeff(32)="-1.2717356392530083922281796470202717699206099207789756160053458316574353536314825132238436065671265924909887806021666353638e6" gamma_coeff(33)=" 3.8284767958047026326148095596422761054926528044089913941223269548642599041669331116354385484190504755863126029970445400483e8" gamma_coeff(34)=" 3.1949820414797702114003599308321389892267817499372662345413540208091880032662279194455894686512023420916265333468518532896e7" gamma_coeff(35)="-1.0880932972261803653900943755516898415525401592137919224133379631891499676570809916142114000435372956217462147082294102977e10" gamma_coeff(36)="-9.0789457815842143542782849439145304696751702576405517222401379095391685218860256867746870320057180102734945572960693142142e8" gamma_coeff(37)=" 3.4728240915261627452391458785846610877573104221908401062920456617950718313542999419226039041917210120852039282315845346515e11" gamma_coeff(38)=" 2.8972845800344443358195428420923468756182495767466134372344468374142731359389788783009715531865814145011698957372536637746e10" gamma_coeff(39)="-1.2368393644340795958485556319730867373617562771928750212987248595076777021441927184539601550820318790306599441793834524643e13" gamma_coeff(40)="-1.0317402311560912496533010452313425369881672373003112860986491522622366449769218400118656645849781497797389188718357969523e12" gamma_coeff(41)=" 4.8874503430548611050097905076881910501241986297138164619038288317998106556924888536940100040279205095859010911272890524065e14" gamma_coeff(42)=" 4.0765784796595956563906357771127383210565012653600147172302789279217989397411126676982914924458423587783210777079185865561e13" gamma_coeff(43)="-2.1318633916848950926837958328825205818109207072461415185004302245823195255073913210013645783088816402212228545417258553457e16" gamma_coeff(44)="-1.7780150113801061259438348913569719499497232521657542591515615410288509303727028646224407649116628879211738315069508083155e15" gamma_coeff(45)=" 1.0217011545437860040705412147561322108846328827421361479151756159183991257188936131517542985585064757638476664561892291754e18" gamma_coeff(46)=" 8.5205499037065271170733831707514450664289262187023063115704092959935683819588325195168265644368460153231166950012670146023e16" gamma_coeff(47)="-5.3571919415238751901229578056195904606696698044375684856686347421862090432964263039734339598653872305742326652178758272054e19" gamma_coeff(48)="-4.4673794115194825043333967084410812950556692431620390864619774960489695619649437536391305518242705824371500772856428831348e18" gamma_coeff(49)=" 3.0613920017780595831101122254899530282344837789052148529223703405386801629294243514670590958089593924567269775592993744124e21" gamma_coeff(50)=" 2.5527598951784639815515328050901967015961663419051428842220526536388328472973435586478773866666846212386855086568416206576e20" gamma_coeff(51)="-1.8998853141744738343043487731073337052963136248489483458262535084025864901924028024787348143986082994068103633084004055068e23" gamma_coeff(52)="-1.5841516244210818232564196309184938397057935968927171985334436984355215072470321502236293506524065892817602230277047370057e22" gamma_coeff(53)=" 1.2762713608136791580121506730066531447768284512163810207207998823233837599777189431827576563919027630581803346455284368930e25" gamma_coeff(54)=" 1.0641263857907083996475558194050352326514699064289678063912834516148380131343069889034870422777331453279689671747577550080e24" gamma_coeff(55)="-9.2524035652525618489655202440055752328273415513337834083007184473636773380468122479988477408926656377041009358434014732808e26" gamma_coeff(56)="-7.7141433409315116107723422753883643327592685951880378705123195797178662304337423407719015935104615867132195630820821438104e25" gamma_coeff(57)=" 7.2185010215448900537758859174772201018442113074357809539654218880607720751519829429884771470931246886050104162968158369695e28" gamma_coeff(58)=" 6.0181766092095530249735938486541267552599243407823189597498640654024991783107436196446596699374056092955337536738049297252e27" gamma_coeff(59)="-6.0449325392064802223440741260096658097141459167690841362940303287562731468784663671134779303239491282225619842322808901897e30" gamma_coeff(60)="-5.0395957726719553163570471067586462375556351302162378502340655827511137236434886566698159351274583922591503432362623379784e29" gamma_coeff(61)=" 5.4204604030647370891020087275420976244117773016337928275704374344292153911052390507995350979568801600124467164272784373726e32" gamma_coeff(62)=" 4.5188520201630485982445453263664662024234913153396800249890069797085951137731834379609808285831417421418808328658013121853e31" gamma_coeff(63)="-5.1927694583610497405174097989870293832717880352911189997065716442540624121501279854180420472179410750327520056722928672872e34" gamma_coeff(64)="-4.3289230884022849626769692787317202844371166753551055694484370234124287241091114764729540390019396975396004273457914998754e33" gamma_coeff(65)=" 5.3034784192698647855876977267437467952627779613687741542814850294028251141484514185644106203211113687474773907276248473428e36" gamma_coeff(66)=" 4.4211124009235185291087374861005054476214706951151642508201057717164488590778965302309461848957709272063800610420138543230e35" gamma_coeff(67)="-5.7631410737985622064911719073658764222561715718530107586959704411660163837815401953038326466414956458518154942160979955823e38" gamma_coeff(68)="-4.8041973120285675857509678577328135847588766536883735138830885472298655185655300183633149382013863366386421022410771375840e37" gamma_coeff(69)=" 6.6509554773158623375713460250622668189334599120816536950953650461077794412817302715112414880741676341671609696868651710015e40" gamma_coeff(70)=" 5.5441792879474355683658022488972041164491986014694134166315493438102217852266073598885253805315241750049825788615940096210e39" 'gamma_coeff(71)="-8.1371472826051415580682782408640902142946939799263244642783963640432034744223806865935733232392200305958251758814652397466e42" 'gamma_coeff(72)="-6.7829365747536009579309695613724306080832372118583906141510523340174216324694637862680047150596778634433368655936847947202e41" 'gamma_coeff(73)=" 1.0536684252467374376755737322957155752558489790329632803500414098275347754091742953011263833241736200589747217919030019552e45" 'gamma_coeff(74)=" 8.7829929411759515692627631725705381613671036314107883753182934617425104040916288972270128677386741226098810936583948862827e43" 'gamma_coeff(75)="-1.4417814545857186280873231904504176164364254384313794206053940069195711200471533653733660223772344263736491864237886546153e47" 'gamma_coeff(76)="-1.2017982225440671737827328699520953817086620777992292179772891252312402309044039597511807225003887434683193153721141003238e46" 'gamma_coeff(77)=" 2.0816855648453860228762450466808477096675037014521280261779800735956311138303315627058370782029466303414722835445723179117e49" end sub '================================================================================ Function decfloat.toString( byval places as long=NUM_DIGITS ) As String Function = fp2str( this.dec_num, places ) End Function /' Function decfloat.toString_exp( byval places as long=NUM_DIGITS ) As String Function = fpfix_exp( this.dec_num, places ) End Function Function decfloat.toString_fix( byval places as long=NUM_DIGITS ) As String Function = fpfix_rndu( this.dec_num, places ) End Function '/ Function decfloat.toLong ( ) As Long dim as double x x=fp2dbl(this.dec_num ) return clng(x) End Function Function decfloat.toLongint ( ) As Longint dim as double x x=fp2dbl(this.dec_num ) return CLngInt(x) End Function Function decfloat.toDouble ( ) As Double Function = fp2dbl(this.dec_num) End Function Constructor decfloat ( ) this.dec_num=si2fp(0, NUM_DWORDS) End Constructor Constructor decfloat ( Byval rhs As Long ) this.dec_num=si2fp( rhs, NUM_DWORDS ) End Constructor Constructor decfloat ( Byref rhs As String ) this.dec_num=str2fp( rhs ) End Constructor Constructor decfloat ( Byref rhs As decfloat) this.dec_num.sign=rhs.dec_num.sign this.dec_num.exponent=rhs.dec_num.exponent for i as long=0 to NUM_DWORDS this.dec_num.mantissa(i)=rhs.dec_num.mantissa(i) next End Constructor Constructor decfloat ( Byval rhs As Longint ) this.dec_num=si2fp( rhs, NUM_DWORDS ) End Constructor Constructor decfloat ( Byval rhs As Integer ) this.dec_num=si2fp( rhs, NUM_DWORDS ) End Constructor Constructor decfloat ( Byval rhs As Double ) this.dec_num=str2fp( str( rhs ) ) End Constructor Destructor decfloat ( ) End Destructor Operator decfloat.let ( Byref rhs As decfloat ) this.dec_num.sign=rhs.dec_num.sign this.dec_num.exponent=rhs.dec_num.exponent for i as long=0 to NUM_DWORDS this.dec_num.mantissa(i)=rhs.dec_num.mantissa(i) next End Operator Operator decfloat.let ( Byval rhs As Long ) this.dec_num=si2fp( rhs, NUM_DWORDS ) End Operator Operator decfloat.let ( Byref rhs As String ) this.dec_num=str2fp( rhs ) End Operator Operator decfloat.Let ( Byval rhs As Longint ) this.dec_num=si2fp( rhs, NUM_DWORDS ) End Operator Operator decfloat.Let ( Byval rhs As Integer ) this.dec_num=si2fp( rhs, NUM_DWORDS ) End Operator Operator decfloat.Let ( Byval rhs As Double ) this.dec_num=str2fp( str(rhs) ) End Operator Operator decfloat.cast ( ) As String Operator = fp2str(this.dec_num) End Operator Operator decfloat.cast ( ) As Long dim as double x x=fp2dbl(this.dec_num ) Operator = clng(x) End Operator Operator decfloat.cast ( ) As Double Operator = fp2dbl(this.dec_num) End Operator '============================================================================ '' For Next for decfloat type '' '' implicit step versions '' '' In this example, we interpret implicit step '' to mean 1 Operator decfloat.for ( ) End Operator Operator decfloat.step ( ) this.dec_num = fpadd(this.dec_num,si2fp(1, NUM_DWORDS), NUM_DWORDS) End Operator Operator decfloat.next ( Byref end_cond As decfloat ) As Integer Return fpcmp(This.dec_num, end_cond.dec_num, NUM_DWORDS)<=0 End Operator '' explicit step versions '' Operator decfloat.for ( Byref step_var As decfloat ) End Operator Operator decfloat.step ( Byref step_var As decfloat ) this.dec_num = fpadd(this.dec_num, step_var.dec_num, NUM_DWORDS) End Operator Operator decfloat.next ( Byref end_cond As decfloat, Byref step_var As decfloat ) As Integer If fpcmp(step_var.dec_num, si2fp(0, NUM_DWORDS), NUM_DWORDS) < 0 Then Return fpcmp(This.dec_num, end_cond.dec_num, NUM_DWORDS) >= 0 Else Return fpcmp(This.dec_num, end_cond.dec_num, NUM_DWORDS) <= 0 End If End Operator '============================================================================ Operator + ( Byref lhs As decfloat, Byval rhs As decfloat ) As decfloat Dim As decfloat result result.dec_num = fpadd(lhs.dec_num, rhs.dec_num, NUM_DWORDS) Operator = result End Operator Operator + ( Byref lhs As decfloat, Byval rhs As Double ) As decfloat Dim As decfloat result result.dec_num=str2fp( str(rhs) ) result.dec_num = fpadd(lhs.dec_num, result.dec_num, NUM_DWORDS) Operator = result End Operator Operator + ( Byval lhs As Double, Byref rhs As decfloat ) As decfloat Dim As decfloat result result.dec_num=str2fp( str(lhs) ) result.dec_num = fpadd( result.dec_num, rhs.dec_num, NUM_DWORDS) Operator = result End Operator Operator + ( Byref lhs As decfloat, Byval rhs As Longint ) As decfloat Dim As decfloat result result.dec_num=si2fp(rhs, NUM_DWORDS) result.dec_num = fpadd(lhs.dec_num, result.dec_num, NUM_DWORDS) Operator = result End Operator Operator + ( Byval lhs As Longint, byref rhs As decfloat ) As decfloat Dim As decfloat result result.dec_num=si2fp(lhs, NUM_DWORDS) result.dec_num = fpadd(result.dec_num, rhs.dec_num, NUM_DWORDS) Operator = result End Operator Operator + (Byref lhs As decfloat, byref rhs as String) As decfloat dim as decfloat result result.dec_num =str2fp(rhs) result.dec_num = fpadd(lhs.dec_num, result.dec_num, NUM_DWORDS) Operator = result End Operator Operator - ( Byref rhs As decfloat ) As decfloat Dim As decfloat result result.dec_num = fpneg(rhs.dec_num) Operator = result End Operator Operator - ( Byref lhs As decfloat, Byval rhs As decfloat ) As decfloat Dim As decfloat result result.dec_num = fpsub(lhs.dec_num, rhs.dec_num, NUM_DWORDS) Operator = result End Operator Operator - ( Byref lhs As decfloat, Byval rhs As Double ) As decfloat Dim As decfloat result result.dec_num=str2fp( str(rhs) ) result.dec_num = fpsub(lhs.dec_num, result.dec_num, NUM_DWORDS) Operator = result End Operator Operator - ( Byval lhs As Double, Byref rhs As decfloat ) As decfloat Dim As decfloat result result.dec_num=str2fp( str(lhs) ) result.dec_num = fpsub( result.dec_num, rhs.dec_num, NUM_DWORDS) Operator = result End Operator Operator - ( Byref lhs As decfloat, Byval rhs As Longint ) As decfloat Dim As decfloat result result.dec_num=si2fp(rhs, NUM_DWORDS) result.dec_num = fpsub(lhs.dec_num, result.dec_num, NUM_DWORDS) Operator = result End Operator Operator - ( Byval lhs As Longint, byref rhs As decfloat ) As decfloat Dim As decfloat result result.dec_num=si2fp(lhs, NUM_DWORDS) result.dec_num = fpsub(result.dec_num, rhs.dec_num, NUM_DWORDS) Operator = result End Operator Operator - (Byref lhs As decfloat, byref rhs as String) As decfloat dim as decfloat result result.dec_num =str2fp(rhs) result.dec_num = fpsub(lhs.dec_num, result.dec_num, NUM_DWORDS) Operator = result End Operator Operator * ( Byref lhs As decfloat, Byval rhs As decfloat ) As decfloat Dim As decfloat result result.dec_num = fpmul(lhs.dec_num, rhs.dec_num, NUM_DWORDS) Operator = result End Operator Operator * ( Byref lhs As decfloat, Byval rhs As Double ) As decfloat Dim As decfloat result result.dec_num=str2fp( str(rhs) ) result.dec_num = fpmul(lhs.dec_num, result.dec_num, NUM_DWORDS) Operator = result End Operator Operator * ( Byval lhs As Double, Byref rhs As decfloat ) As decfloat Dim As decfloat result result.dec_num=str2fp( str(lhs) ) result.dec_num = fpmul( result.dec_num, rhs.dec_num, NUM_DWORDS) Operator = result End Operator Operator * ( Byref lhs As decfloat, Byval rhs As Longint ) As decfloat Dim As decfloat result if abs(rhs)<100000000 then result.dec_num=fpmul_si(lhs.dec_num, rhs) else result.dec_num=si2fp(rhs, NUM_DWORDS) result.dec_num = fpmul(lhs.dec_num, result.dec_num, NUM_DWORDS) end if Operator = result End Operator Operator * ( Byval lhs As Longint, byref rhs As decfloat ) As decfloat Dim As decfloat result result.dec_num=si2fp(lhs, NUM_DWORDS) result.dec_num = fpmul(result.dec_num, rhs.dec_num, NUM_DWORDS) Operator = result End Operator Operator * (Byref lhs As decfloat, byref rhs as String) As decfloat dim as decfloat result result.dec_num =str2fp(rhs) result.dec_num = fpmul(lhs.dec_num, result.dec_num, NUM_DWORDS) Operator = result End Operator Operator / ( Byref lhs As decfloat, Byval rhs As decfloat ) As decfloat Dim As decfloat result result.dec_num = fpdiv(lhs.dec_num, rhs.dec_num, NUM_DWORDS) Operator = result End Operator Operator / ( Byref lhs As decfloat, Byval rhs As Double ) As decfloat Dim As decfloat result result.dec_num=str2fp( str(rhs) ) result.dec_num = fpdiv(lhs.dec_num, result.dec_num, NUM_DWORDS) Operator = result End Operator Operator / ( Byval lhs As Double, Byref rhs As decfloat ) As decfloat Dim As decfloat result result.dec_num=str2fp( str(lhs) ) result.dec_num = fpdiv( result.dec_num, rhs.dec_num, NUM_DWORDS) Operator = result End Operator Operator / ( Byref lhs As decfloat, Byval rhs As Longint ) As decfloat Dim As decfloat result result.dec_num=si2fp(rhs, NUM_DWORDS) result.dec_num = fpdiv(lhs.dec_num, result.dec_num, NUM_DWORDS) Operator = result End Operator Operator / ( Byval lhs As Longint, byref rhs As decfloat ) As decfloat Dim As decfloat result result.dec_num=si2fp(lhs, NUM_DWORDS) result.dec_num = fpdiv(result.dec_num, rhs.dec_num, NUM_DWORDS) Operator = result End Operator Operator / (Byref lhs As decfloat, byref rhs as String) As decfloat dim as decfloat result result.dec_num =str2fp(rhs) result.dec_num = fpdiv(lhs.dec_num, result.dec_num, NUM_DWORDS) Operator = result End Operator declare Function fplogTaylor(x As decfloat_struct, byval dwords as long=NUM_DWORDS) As decfloat_struct declare Function fplog (x As decfloat_struct, byval dwords as long=NUM_DWORDS) As decfloat_struct declare Function fpexp_cf(byref x as decfloat_struct, ByVal n As Integer, byval dwords as long=NUM_DWORDS) As decfloat_struct declare Function fplog_r (x As decfloat_struct, byval dwords as long=NUM_DWORDS) As decfloat_struct /' Operator ^ ( Byref lhs As decfloat, Byval rhs As Longint ) As decfloat Dim As decfloat lhs2 lhs2.dec_num=fpipow(lhs.dec_num, rhs, NUM_DWORDS) Operator = lhs2 end operator Operator ^ ( Byval lhs As Longint, Byref rhs As decfloat ) As decfloat Dim As decfloat lhs2 lhs2.dec_num=str2fp(str(lhs)) lhs2.dec_num=fplog(lhs2.dec_num, NUM_DWORDS) lhs2.dec_num=fpmul(lhs2.dec_num, rhs.dec_num, NUM_DWORDS) lhs2.dec_num=fpexp(lhs2.dec_num, NUM_DWORDS) Operator = lhs2 End Operator Operator ^ ( Byref lhs As decfloat, Byref rhs As decfloat ) As decfloat Dim As decfloat lhs2 lhs2.dec_num=fplog(lhs.dec_num, NUM_DWORDS) lhs2.dec_num=fpmul(lhs2.dec_num, rhs.dec_num, NUM_DWORDS) lhs2.dec_num=fpexp(lhs2.dec_num, NUM_DWORDS) Operator = lhs2 End Operator Operator ^ (Byref lhs As decfloat, byref rhs as String) As decfloat dim as decfloat lhs2, rhs2 rhs2.dec_num =str2fp(rhs) lhs2.dec_num=fplog(lhs.dec_num, NUM_DWORDS) lhs2.dec_num=fpmul(lhs2.dec_num, rhs2.dec_num, NUM_DWORDS) lhs2.dec_num=fpexp(lhs2.dec_num, NUM_DWORDS) Operator = lhs2 End Operator '/ Operator decfloat.+= (Byref rhs As decfloat) this.dec_num = fpadd(this.dec_num, rhs.dec_num, NUM_DWORDS) End Operator Operator decfloat.+= (Byval rhs As Longint) Dim As decfloat result result.dec_num=si2fp(rhs, NUM_DWORDS) this.dec_num = fpadd(this.dec_num, result.dec_num, NUM_DWORDS) End Operator Operator decfloat.+= (Byval rhs As Double) Dim As decfloat result result.dec_num=str2fp( str(rhs) ) this.dec_num = fpadd(this.dec_num, result.dec_num, NUM_DWORDS) End Operator Operator decfloat.+= (Byref rhs As String) Dim As decfloat result result.dec_num=str2fp(rhs) this.dec_num = fpadd(this.dec_num, result.dec_num, NUM_DWORDS) End Operator Operator decfloat.-= (Byref rhs As decfloat) this.dec_num = fpsub(this.dec_num, rhs.dec_num, NUM_DWORDS) End Operator Operator decfloat.-= (Byval rhs As Longint) Dim As decfloat result result.dec_num=si2fp(rhs, NUM_DWORDS) this.dec_num = fpsub(this.dec_num, result.dec_num, NUM_DWORDS) End Operator Operator decfloat.-= (Byval rhs As Double) Dim As decfloat result result.dec_num=str2fp( str(rhs) ) this.dec_num = fpsub(this.dec_num, result.dec_num, NUM_DWORDS) End Operator Operator decfloat.-= (Byref rhs As String) Dim As decfloat result result.dec_num=str2fp(rhs) this.dec_num = fpsub(this.dec_num, result.dec_num, NUM_DWORDS) End Operator Operator decfloat.*= (Byref rhs As decfloat) this.dec_num = fpmul(this.dec_num, rhs.dec_num, NUM_DWORDS) End Operator Operator decfloat.*= (Byval rhs As Longint) Dim As decfloat result result.dec_num=si2fp(rhs, NUM_DWORDS) this.dec_num = fpmul(this.dec_num, result.dec_num, NUM_DWORDS) End Operator Operator decfloat.*= (Byval rhs As Double) Dim As decfloat result result.dec_num=str2fp( str(rhs) ) this.dec_num = fpmul(this.dec_num, result.dec_num, NUM_DWORDS) End Operator Operator decfloat.*= (Byref rhs As String) Dim As decfloat result result.dec_num=str2fp(rhs) this.dec_num = fpmul(this.dec_num, result.dec_num, NUM_DWORDS) End Operator Operator decfloat./= (Byref rhs As decfloat) this.dec_num = fpdiv(this.dec_num, rhs.dec_num, NUM_DWORDS) End Operator Operator decfloat./= (Byval rhs As Longint) Dim As decfloat result result.dec_num=si2fp(rhs, NUM_DWORDS) this.dec_num = fpdiv(this.dec_num, result.dec_num, NUM_DWORDS) End Operator Operator decfloat./= (Byval rhs As Double) Dim As decfloat result result.dec_num=str2fp( str(rhs) ) this.dec_num = fpdiv(this.dec_num, result.dec_num, NUM_DWORDS) End Operator Operator decfloat./= (Byref rhs As String) Dim As decfloat result result.dec_num=str2fp(rhs) this.dec_num = fpdiv(this.dec_num, result.dec_num, NUM_DWORDS) End Operator /' Operator decfloat.^= ( Byref rhs As decfloat) this.dec_num=__bid32_pow(this.dec_num, rhs.dec_num, 0, @flag) End Operator Operator decfloat.^= (Byval rhs As Double) Dim As decfloat result result.dec_num=__binary64_to_bid32(rhs, 0, @flag) this.dec_num=__bid32_pow(this.dec_num, result.dec_num, 0, @flag) End Operator Operator decfloat.^= (Byval rhs As Longint) Dim As decfloat result result.dec_num=__binary64_to_bid32(rhs, 0, @flag) this.dec_num=__bid32_pow(this.dec_num, result.dec_num, 0, @flag) End Operator Operator decfloat.^= (Byref rhs As String) Dim As decfloat result result.dec_num=__bid_strtod32(rhs, 0) this.dec_num = __bid32_pow(this.dec_num, result.dec_num, 0, @flag) End Operator '/ Operator = ( Byref lhs As decfloat, Byref rhs As decfloat ) As Long Operator = fpcmp(lhs.dec_num, rhs.dec_num, NUM_DWORDS)=0 End Operator Operator < ( Byref lhs As decfloat, Byref rhs As decfloat ) As Long Operator = fpcmp(lhs.dec_num, rhs.dec_num, NUM_DWORDS)<0 End Operator Operator > ( Byref lhs As decfloat, Byref rhs As decfloat ) As Long Operator = fpcmp(lhs.dec_num, rhs.dec_num, NUM_DWORDS)>0 End Operator Operator <= ( Byref lhs As decfloat, Byref rhs As decfloat ) As Long Operator = fpcmp(lhs.dec_num, rhs.dec_num, NUM_DWORDS)<=0 End Operator Operator >= ( Byref lhs As decfloat, Byref rhs As decfloat ) As Long Operator = fpcmp(lhs.dec_num, rhs.dec_num, NUM_DWORDS)>=0 End Operator Operator <> ( Byref lhs As decfloat, Byref rhs As decfloat ) As Long Operator = fpcmp(lhs.dec_num, rhs.dec_num, NUM_DWORDS)<>0 End Operator '===================================================================== Operator Abs(Byref rhs As decfloat) As decfloat Dim As decfloat result result.dec_num=fpabs(rhs.dec_num) Operator = result End Operator Operator Fix (Byref x As decfloat) As decfloat Dim As decfloat result result.dec_num =fpfix(x.dec_num) Operator = result End Operator Operator Frac(Byref x As decfloat) As decfloat Dim As decfloat result result.dec_num=fpfrac(x.dec_num, NUM_DWORDS) Operator = result End Operator Operator Sqr(Byref x As decfloat) As decfloat Dim As decfloat result result.dec_num=fpsqr(x.dec_num, NUM_DWORDS) Return result End Operator function agm(byref a_in as decfloat, byref b_in as decfloat, byval digits as long=num_digits) as decfloat dim as decfloat a, b, c, d, eps dim as string s a=a_in b=b_in s=str(digits-5) s=trim(s) eps="1e-"+s do c = (a + b)/2 d = sqr(a*b) a = c b = d loop until abs(a-b) NUM_DWORDS) then dwords = NUM_DWORDS temp=0 fac=1 if (x2 = temp) then return fac end if fac=abs(x2) fac=fix(fac) x=abs(x2)-fac if x>0 then x.dec_num=fpdiv_si(x.dec_num, 256, dwords) accum=ExpAn(29) For i as long = 28 To 0 Step -1 accum = accum * x + ExpAn(i) Next i for i=1 to 8 accum=accum * accum next else accum=1 end if if fac>0 then n=fp2ui(fac.dec_num) fac.dec_num=fpipow(fpexp1.dec_num, n, dwords) accum=accum*fac end if if x2<0 then accum=ExpAn(0)/accum return accum end function function fpSin(byref x as decfloat) as decfloat dim as decfloat sn,x2 dim as long sign sign=1 if x<0 then sign=-1 x=-x end if if x>fpPi2 then x=frac(x/fpPi2) x=x*fpPi2 end if if x>fpPihalf then if xfpPi2 then x=frac(x/fpPi2) x=x*fpPi2 end if if x>fpPihalf then if xfpPi2 then x=frac(x/fpPi2) x=x*fpPi2 end if while x > fpPihalf x = x - fpPi wend while x <= -fpPihalf x = x + fpPi wend x=x/27 x2=x*x num = TanAn(13) den = TanBn(13) For i = 12 To 1 Step -1 num = num * x2 + TanAn(i) den = den * x2 + TanBn(i) Next i den = den * x2 + TanBn(0) tn=x+x2*x*num/den if sign<0 then tn=-tn end if tn=(tn*(3-tn*tn))/(TanAn(0)-3*tn*tn) tn=(tn*(3-tn*tn))/(TanAn(0)-3*tn*tn) tn=(tn*(3-tn*tn))/(TanAn(0)-3*tn*tn) return tn end function function fpAtan(byref x1 as decfloat) as decfloat dim as decfloat tn, x, x2, eps, num, den dim as long i, sign, n, r if x1=0 then return x1 x=x1 sign=1 r=0 eps="0.0061361" ' approximately Pi/512 if x<0 then sign=-1 x=-x end if if x>1 then x=1/x r=1 ' set reciprocal flag end if n=0 while x>eps ' divide argument in half x=x/(1+sqr(1+x*x)) n+=1 wend x2=x*x num = AtanAn(17) den = AtanBn(17) For i = 16 To 0 Step -1 num = num * x2 + AtanAn(i) den = den * x2 + AtanBn(i) Next i tn=x+x2*x*num/den if n>0 then ' if the argument was halved then restore for i=1 to n tn=tn+tn next end if if r=1 then tn=fpPihalf-tn end if if sign<0 then tn=-tn end if return tn end function function fpgamma(byref x as decfloat) as decfloat dim as decfloat z, result dim as long k, n, m result=0 if (x=1) then result=1 return result end if if (x<0) then n=fp2i(x.dec_num) if (x<>n) then result=fppi/(fpgamma(1-x)*sin(fppi*x)) else result="1e99999999" end if else n=abs(fp2i(x.dec_num)) m=0 if n<169 then m=169-n end if x=x+m z=1/x result = gamma_coeff(70) For i as long = 69 To 0 Step -1 result = result * z + gamma_coeff(i) Next i result=result*x result=sqrt2pi*z*sqr(z)/(fpexp(log_agm(z)*x)*fpexp(x))*result if m>0 then for k=1 to m z=x-k result=result/z next end if end if return result end function Function mpexp1 (x1 as decfloat) as decfloat dim as decfloat sn, sd, y, x, xx, x3, x5, t dim as longint digits, sign, i, k, n, m dim as double e, lt, eps n=0 x=x1 eps=0.5 '0.00390625 ' 1/256 e=2.718281828459045 lt=2.302585092994046 digits=NUM_DIGITS m=-int(-((digits*lt/(Log(digits*lt/(e*eps))-log(Log(digits*lt/(e*eps))))-3))) Do While Abs(x)>eps x=x/5 n=n+1 Loop ''' y=mp_tanh_half(x) 'If y = Tan(x/2) Then ' ' 2*x*y 'sinh(x) = ------- ' y^2-x^2 ' convert To sinh ''' sn=(2*x*y)/(y*y-x*x) /' t = x y = t*t sn = 0 sd = 1 i = 0 DO UNTIL sn=sn-t i = i+2 k = i*i+i t = t*y sn = sn*k+t sd = sd*k LOOP sn = x + sn/sd '/ t = x y = t*t sn = 0 sd = 1 for i=2 to m step 2 k = i*i+i t = t*y sn = sn*k+t sd = sd*k next sn = x + sn/sd ' sinh(5*x) For i=1 To n xx=sn*sn y=xx*sn x5=5*sn+20*y y=y*xx sn=x5+y*16 Next i sn=sn+Sqr(1+sn*sn) 'convert sinh To Exp return sn End Function Function mpexp2 (x1 as decfloat) as decfloat dim as decfloat sn, y, x, t, hi, a0, b0, a1, b1, z, xx, x5 dim as longint digits, sign, i, k, n, m dim as double e, lt, eps n=0 x=x1 ' eps=3.0e-15 ' eps=NUMBER_OF_DIGITS 'eps=1/exp((-3.86482456973614 + eps * 0.0371341339417906)/(1 + eps * -1.29416227479631e-008)) 'eps=1/exp((-4.124234442965103+2.807538848667194E-02*eps)/(1-1.558046246939480E-08*eps)) ' eps=1/exp((-4.342414715178671+2.257180749332072E-02*eps)/(1-1.007581641313990E-08*eps)) 'eps=1/exp((-4.516480386261090+1.887046248085925E-02*eps)/(1-1.496897466020367E-08*eps)) /' eps=0.008 e=2.718281828459045 lt=2.302585092994046 digits=NUM_DIGITS m=-int(-((digits*lt/(Log(digits*lt/(e*eps))-log(Log(digits*lt/(e*eps))))-3))) '/ /' eps=1e-5 Do While Abs(x)>eps x=x/5 n=n+1 Loop '/ ''' y=mp_tanh_half(x) 'If y = Tan(x/2) Then ' ' 2*x*y 'sinh(x) = ------- ' y^2-x^2 ' convert To sinh ''' sn=(2*x*y)/(y*y-x*x) /' t = x y = t*t sn = 0 sd = 1 i = 0 DO UNTIL sn=sn-t i = i+2 k = i*i+i t = t*y sn = sn*k+t sd = sd*k LOOP sn = x + sn/sd '/ eps=.5 Do While Abs(x)>eps x=x/5 n=n+1 Loop t = 0.5*x y = t*t i = 3 x = Abs(x) hi = "1e"+trim(str(NUM_DIGITS\2)) '"1e500" hi= hi * x ' for 1000 digits accuracy a0 = 0 b0 = 1 a1 = t b1 = 1 Do Until b1 > hi z = a0 * y a0 = a1 a1 = a1 * i + z z = b0 * y b0 = b1 b1 = b1 * i + z i = i + 2 hi = hi * x Loop sn=(2*a1*b1)/(b1*b1-a1*a1) ' tanh(x/2) --> sinh(x) For i=1 To n xx=sn*sn y=xx*sn x5=5*sn+20*y y=y*xx sn=x5+y*16 Next i sn=sn+Sqr(1+sn*sn) 'convert sinh To Exp return sn End Function Function mpexp (byref x As decfloat_struct, byval dwords as long=NUM_DWORDS) As decfloat_struct if dwords>NUM_DWORDS then dwords=NUM_DWORDS 'taylor series Dim As decfloat_struct fac,_x,temp,accum,p,term dim as integer i, c temp=si2fp(0) fac.sign=1 fac.exponent=(BIAS+1) fac.mantissa(0)=10000000 /' for i=1 to NUM_DWORDS fac.mantissa(i)=0 next '/ if fpcmp(x, temp, dwords)=0 then return fac c=1 _x=fpdiv_si(x, 262144, dwords) 'fpdiv_si(x, 67108864) ' p=_x accum=fpadd(fac, _x, dwords) '1 + x Do c+=1 temp=accum fac=fpdiv_si(fac, c, dwords) p=fpmul(p, _x, dwords) term=fpmul(p,fac, dwords) Accum=fpadd(temp,Term, dwords) Loop Until fpcmp(accum,temp, dwords) = 0 for i=1 to 18 accum=fpmul(accum, accum, dwords) next Return accum End Function function fpAtan8(byref x1 as decfloat) as decfloat dim as decfloat tn, x, x2, xr, y, eps, gn, gm1, hn, z dim as long i, sign, n, r 'dim as double dx if x1=0 then return x1 x=x1 sign=1 r=0 eps=4.0/NUM_DIGITS '"3e-2" if x<0 then sign=-1 x=-x end if if x>1 then x=1/x r=1 ' set reciprocal flag end if n=0 while x>eps ' divide argument in half x=x/(1+sqr(1+x*x)) n+=1 wend x2=x*x /' dx=x dx=(-log(dx))+1 dx=10*NUM_DWORDS/dx '/ eps="1e-"+trim(str(NUM_DIGITS)) gn=2/x hn=1 tn=0 y=1-4/x2 xr=1/x z=x i=1 while fpcmp_abs(z.dec_num,eps.dec_num)>0 z=gn/((gn*gn+hn*hn)*(2*i-1)) tn=tn+z gm1=gn gn=gn*(y)+4*hn*xr hn=hn*(y)-4*gm1*xr i+=1 wend tn=tn+tn if n>0 then ' if the argument was halved then restore for i=1 to n tn=tn+tn next end if if r=1 then tn=fpPihalf-tn end if if sign<0 then tn=-tn end if return tn end function Function log2_32(Byval value As Ulong) As Long 'https://stackoverflow.com/questions/11376288/fast-computing-of-log2-for-64-bit-integers Static tab32(0 To 31) As Const Long = {0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30, 8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31} value Or= Culng(value Shr 1) value Or= Culng(value Shr 2) value Or= Culng(value Shr 4) value Or= Culng(value Shr 8) value Or= Culng(value Shr 16) Return tab32(Culng(Culng(value * &h07C4ACDD) Shr 27)) End Function function fpatn (byref x As decfloat_struct, byval dwords As ULong = NUM_DWORDS) As decfloat_struct If dwords > NUM_DWORDS Then dwords = NUM_DWORDS Dim As Long sign(3): sign(3) = 1 Dim As long z, c = 1 Dim As decfloat_struct XX, Term, Accum, strC, x_2, mt, mt2, p Dim As decfloat_struct decnum, one, decnum2, factor if NUM_DWORDS>15 then if fppi.dec_num.mantissa(NUM_DWORDS)=0 then fppi.dec_num=pi_chudnovsky_bs fpPihalf=fppi/2 end if end if decnum2 = x decnum2.sign = 0 one = si2fp(1, dwords) If fpcmp(decnum2, one, dwords) = 0 Then decnum = fpdiv_si(fppi.dec_num, 4, dwords) decnum.sign = x.sign return decnum End If decnum2.sign = x.sign Dim As Long limit = (log2_32(NUM_DIGITS)+3)*2 '16 factor = si2fp(2 shl (limit - 1), dwords) For z = 1 To limit decnum = fpmul(decnum2, decnum2, dwords) decnum = fpadd(decnum, one, dwords) decnum = fpsqr(decnum, dwords) decnum = fpadd(decnum, one, dwords) decnum = fpdiv(decnum2, decnum, dwords) decnum2 = decnum Next z mt = decnum x_2 = decnum p = decnum XX = fpmul(x_2, x_2, dwords) Do c = c + 2 mt2 = mt strC = si2fp(c, dwords) p = fpmul(p, XX, dwords) Term = fpdiv(p, strC, dwords) If sign(c And 3) Then Accum = fpsub(mt, Term, dwords) Else Accum = fpadd(mt, Term, dwords) End If Swap mt, Accum Loop Until fpcmp(mt, mt2, dwords) = 0 return fpmul(factor, mt, dwords) End function function fpAtan9(byref x1 as decfloat) as decfloat dim as decfloat tn, x, x2, xr, y, eps, gn, gm1, hn, z dim as long i, sign, s, n, r, limit dim as double dx if x1=0 then return x1 if NUM_DWORDS>15 then if fppi.dec_num.mantissa(NUM_DWORDS)=0 then fppi.dec_num=pi_chudnovsky_bs fpPihalf=fppi/2 end if end if if x1=1 then return fppi/4 end if x=x1 sign=1 r=0 eps="5e-2" '1.0/NUM_DIGITS '"3e-2" if x<0 then sign=-1 x=-x end if if x>1 then x=1/x r=1 ' set reciprocal flag end if n=0 'while x>eps ' divide argument in half limit=16 'NUM_DIGITS/39 for i=1 to limit x=x/(1+sqr(1+x*x)) n+=1 next 'wend '/ x2=x*x dx=x limit=NUM_DIGITS/(log(1/dx)*0.4342944819032518*2) 'limit=NUM_DIGITS/(log10_32(1/dx)*2) s=-1 y=x tn=x for i=1 to limit y=y*x2 tn=tn+y/(2*i+1)*s s=-s next if n>0 then ' if the argument was halved then restore for i=1 to n tn=tn+tn next end if if r=1 then tn=fpPihalf-tn end if if sign<0 then tn=-tn end if return tn end function '===================================================================== /' dim as decfloat x, x2, y, z, sm, j, gm1, gn, hn, eps dim as double t dim as long i, n '/ 'x=".001" 'y=fpAtan(x) '? y /' t=timer sm=0 for i=1 to 10000 y=i z=sqr(y) sm=sm+z next t=timer-t ? fp2str_exp(sm.dec_num, NUM_DIGITS) ? sm ? t ? "NUM_DIGITS = ";NUM_DIGITS '/ /' sm=0 t=Timer For i=1 To 100 For j=0 To 50 Step 0.1 'y=mpexp(j) y.dec_num=mpexp(j.dec_num) 'y=fpexp(j) sm=sm+y Next j Next i t=timer-t Print sm Print "Rational Plynomial Exp time ";t '/ #if 0 p=0;for(i=1,100,forstep(j=0,50,0.1,p=p+exp(j))) p=0;for(i=1,35000,p=p+atan(j)) #endif /' t=timer eps="1e-115" x=.5 x2=x*x gn=2/x hn=1 y=0 n=1 z=x while abs(z)>eps z=gn/((gn*gn+hn*hn)*(2*n-1)) y=y+z gm1=gn gn=gn*(1-4/x2)+4*hn/x hn=hn*(1-4/x2)-4*gm1/x n+=1 wend t=timer-t ? 2*y ? t '/ #if 0 fppi.dec_num=pi_chudnovsky_bs fpPihalf=fppi/2 t=timer sm=0 for i=1 to 1000 '350'00 y=i 'z=fpatan(y) 'z=fpatan8(y) 'z=fpatan9(y) 'z.dec_num=fpatn(y.dec_num) z=fpgamma(y) sm=sm+z next t=timer-t ? sm ? t '? fpatan8(1)*4-fppi /' t=timer sm=0 for i=1 to 1000 y=i z=fpgamma(y) sm=sm+z next t=timer-t ? sm ? t '/ ? "NUM_DWORDS=";NUM_DWORDS ? log2_32(NUM_DIGITS) x=fppi/180 x=x*9 y=fpsin(x) y=fpcos(y) y=fptan(y) y=fpAtan(y) z=1-y*y z.dec_num=fpsqr(z.dec_num) y=fpAtan(z/y) z=1-y*y z.dec_num=fpsqr(z.dec_num) y=fpAtan(y/z) y=y/9 ? y*180-fppi #endif #if 0 dim as decfloat e1, e2, e3, e4, e5 dim as string s dim as double delta, time1, time2, time3, time4 dim as long i s="1." for i=1 to 120 s=s+"7" next 'print s e1=s s="1." for i=1 to 120 s=s+"9" next 'print s e2=s time3=timer ' addition e4=0 time1=timer for i=1 to 500000 e3=e1 + e2 e4=e4 + e3 next time2=timer print "result for addition" s=e4.toString print s delta= (time2 - time1) print "elapsed time for addition is ", delta, " seconds" print ' subtraction e4=0 time1=timer for i=1 to 500000 e3=e1 - e2 e4=e4 + e3 next time2=timer print "result for subtraction" s=e4.toString print s delta= (time2 - time1) print "elapsed time for subtraction is ", delta, " seconds" print ' multiplication e4=0 time1=timer for i=1 to 500000 e3=e1 * e2 e4= e3 + e4 next time2=timer print "result for multiplication" s=e4.toString print s delta= (time2 - time1) print "elapsed time for multiplication is ", delta, " seconds" print ' division e4=0 time1=timer for i=1 to 500000 e3=e1 / e2 e4= e3 + e4 next time2=timer print "result for division" s=e4.toString print s delta= (time2 - time1) print "elapsed time for division is ", delta, " seconds" print ' sin e4=0 time1=timer for i=-5000 to 5000 e5=i e3=fpsin(e5) e4= e3 + e4 next time2=timer print "result for sin" s=e4.toString print s delta= (time2 - time1) print "elapsed time for sin is ", delta, " seconds" print ' cos e4=0 time1=timer for i=-5000 to 5000 e5=i e3=fpcos(e5) e4= e3 + e4 next time2=timer print "result for cos" s=e4.toString print s delta= (time2 - time1) print "elapsed time for cos is ", delta, " seconds" print ' tan e4=0 time1=timer for i=-5000 to 5000 e5=i e3=fptan(e5) e4= e3 + e4 next time2=timer print "result for tan" s=e4.toString print s delta= (time2 - time1) print "elapsed time for tan is ", delta, " seconds" print ' sqr e4=0 time1=timer for i=1to 50000 e5=i e3=sqr(e5) e4= e3 + e4 next time2=timer print "result for sqr" s=e4.toString print s delta= (time2 - time1) print "elapsed time for sqr is ", delta, " seconds" print ' ln e4=0 time1=timer for i=1to 5000 e5=i e3=log_agm(e5) e4= e3 + e4 next time2=timer print "result for ln" s=e4.toString print s delta= (time2 - time1) print "elapsed time for ln is ", delta, " seconds" print ' exp s="0.005" e2=s s="0.5" e3=s e4=0 time1=timer for i=1 to 50000 e3=e2 + e3 e5=fpexp(e3) e4=e4 + e5 next time2=timer print "result for exp" s=e4.toString print s delta= (time2 - time1) print "elapsed time for exp is ", delta, " seconds" print time4=timer delta= (time4 - time3) print "total elapsed time is ", delta, " seconds" #endif /' dim as decfloat x, y, z dim as double t1, t2, t3 dim as long i '/ /' t1=timer z=0 for i=-4000 to 4000 x=i y=fpAtan9(x) z=z+y next t1=timer-t1 ? z t2=timer z=0 for i=-4000 to 4000 x=i y.dec_num=fpAtn(x.dec_num) z=z+y next t2=timer-t2 ? z t3=timer z=0 for i=-4000 to 4000 x=i y.dec_num=fpAtn(x.dec_num) z=z+y next t3=timer-t3 ? z ? t1 ? t2 ? t3 'sleep '/ /' t1=timer x.dec_num=pi_chudnovsky_bs t1=timer-t1 ? x ? t1 '/ #if 0 dim as decfloat a, a1, b, b2, c, ae,be, s, t, p, pi, pp dim as double r, tm, tim dim as string s1, s2 dim as long j pi.dec_num=pi_chudnovsky_bs 'Input "a, b: ",s1, s2 ae=3 's1 be=4 's2 tim=0 pp=0 for j=1 to 100 tm=timer b = be/ae s = (1 + b*b)/2 a = 1 t = 1 Do a1 = (a + b)/2 If a = a1 Then Exit Do c = (a - b)/2 b = Sqr(a*b) a = a1 s = s - t*c*c t = t + t Loop p = 2*pi*ae*s/a pp=pp+p r = Timer - tm tim=tim+r next ? pp ? "62387660229689781473220873777797910164951821520512190192203403315" Print Print "runtime: "; Print tim/100; Print " seconds" /' Function perimeter2(Byval a As Double, Byval b As Double) As Double ' ellipse perimeter ultimate formula Dim As Double b2, s s=a*b b2=Sqr(s) Do a=a+b b=b2*2 b2=Sqr(a*b) s=2*s+b*(a-b) Loop Until b=b2 Return 2*pi*(s/b2) End Function '/ 'Input "a, b: ",s1, s2 ae=3 's1 be=4 's2 tim=0 pp=0 for j=1 to 100 tm=timer a=ae b=be s=a*b b2=Sqr(s) Do a=a+b b=b2*2 b2=Sqr(a*b) s=2*s+b*(a-b) Loop Until b=b2 p = 2*pi*(s/b2) pp=pp+p r = Timer - tm tim=tim+r next ? pp ? "62387660229689781473220873777797910164951821520512190192203403315" Print Print "runtime: "; Print tim/100; Print " seconds" #endif fppi.dec_num=pi_chudnovsky_bs '================================================================== Function perimeter2a(byref a as decfloat, byref b as decfloat) as decfloat ' ellipse perimeter ultimate formula dim as decfloat q, r, s s = Sqr(b/a) q = s r = 0 Do While s <> 1 s = Sqr((s+1/s)/2) r = (q+r)/s q = 2*s*q Loop return 2*fppi*a*(q-r) End Function Function perimeter2b(byref a as decfloat, byref b as decfloat) as decfloat ' ellipse perimeter ultimate formula Dim as decfloat b2, s s=a*b b2=Sqr(s) Do a=a+b b=b2*2 b2=Sqr(a*b) 'if not (b<>b2) then exit do if b=b2 then exit do s=2*s+b*(a-b) Loop Return fppi*(4*s/b2) End Function /' Sub pprint(byref p as decfloat, byval nd as long=NUM_DIGITS) dim as string s dim as long i s = trim(str(p - Int(p))) Print Print str(Int(p)); Print mid(s, 1, 1); For i = 2 To nd + 1 Print mid(s, i, 1); If ((i-1) mod 10) = 0 Then Print " "; If ((i-1) mod 50) = 0 Then Print Print space(1 + Len(str(Int(p)))); End If Next i If ((i - 1) mod 50) <> 0 Or nd = 0 Then Print Print End Sub '/ Sub pprint(byref p as decfloat, byval ll as long=50, byval ngroup as long=10, byval del as string=" ", byval nd as long=NUM_DIGITS) dim as string s dim as long i, lm lm = (ll \ ngroup - ((ll Mod ngroup) <> 0)) * ngroup s = trim(str(p - Int(p))) Print Print str(Int(p)); Print mid(s, 1, 1); For i = 2 To nd + 1 Print mid(s, i, 1); If (((i-1) mod ngroup) = 0) andalso (((i-1) mod lm) <> 0) andalso i<(nd+1) Then Print del; If ((i-1) mod lm) = 0 Then Print Print space(1 + Len(str(Int(p)))); End If Next i If ((i - 1) mod lm) <> 0 Or nd = 0 Then Print Print End Sub '================================================================== /' dim as decfloat pp, x dim as double t, tim1, tim2 dim as long j dim as string s tim1=0 pp=0 t=Timer For j=1 To 10 pp=pp+perimeter2a(3, 4) Next j tim1=tim1+(timer-t) pprint(pp, ,,,NUM_DIGITS) Print Using "##.#####"; tim1/10 tim2=0 pp=0 t=Timer For j=1 To 10 pp=pp+perimeter2b(3, 4) Next j tim2=tim2+(timer-t) pprint(pp, ,,,NUM_DIGITS) Print Using "##.#####"; tim2/10 ? tim1/tim2 ? pp ?:? pprint(pp, 70, 5, ", ",NUM_DIGITS) '/ /' function ErfSeries(byref x as decfloat, byval wp as long=NUM_DIGITS) as decfloat dim as decfloat x2, sum, tol, t, termNum dim as long n x2 = x * x termNum = x ' x^(2n+1) / n! sum = x tol = "1e-"+trim(str(wp + 20)) n = 1 do termNum = (termNum * x2)/n t = termNum/(2 * n + 1) if (n and 1) = 1 then sum = sum - t else sum = sum + t if abs(t) < tol then exit do n+=1 if n > (40 * wp + 200) then exit do loop return sum * decfloat(2)/sqr(fppi) end function '/ function ErfSeries(byref x as decfloat, byval precision as long=NUM_DIGITS) as decfloat dim as decfloat x2, term, sum, tol dim as long n x2 = x * x term = 1 ' n=0 term: x^0 / (3/2)_0 = 1 sum = 1 tol = "1e-"+trim(str(NUM_DIGITS+6)) n = 0 do term = term * x2 / (1.5 + n) ' (3/2)_n recurrence, all positive sum = sum + term if term < tol then exit do ' no abs() needed, term is always >= 0 n += 1 if n > (40 * precision + 200) then exit do loop x2=-x2 x2.dec_num=mpexp(x2.dec_num) return (2 * x / sqr(fpPi)) * x2 * sum end function function erf(byref x as decfloat, byval precision as long=NUM_DIGITS) as decfloat dim as decfloat t, r dim as double td dim as long p, neg p = precision if p < 0 then p = 0 if x=0 then return decfloat(0) neg = x.dec_num.sign t = abs(x) td = t ' once erfc drops below the rounding threshold, erf is +-1 if td * td > (p + 3) * 2.302585 + 5 then return iif (neg<0, decfloat(-1), decfloat(1)) end if ' the series cancels for large t; widen the working precision to compensate 'var guard := System.Trunc(0.44 * td * td) + 12; r = ErfSeries(t, p) if neg<0 then r = -r return r end function #if 0 dim as decfloat e1, e2, e3, e4, e5 dim as long i dim as string s dim as double t s="1."+string(119,"7") e1=s s="1."+string(119,"9") e2=s /' e4=0 t=timer for i=-5000 to 5000 e5=i e5=e1+e5 'e5=i 'e5=e5+e1 'e2=e1*e5 'e3=e1/e2 'e3=fpsin(e5) 'e3=fpcos(e5) 'e3=fptan(e5) 'e3=log_agm(e5) 'e3=fpexp(e3) e3=Erf(e5) 'e3=fpgamma(e5) 'e3=sqr(e5) e4=e4+e3 'e4=e4+sin(e1) next 'e4=sqr(e3) t=timer-t ? fp2str_exp(e4.dec_num, NUM_DIGITS) ? t '/ t=timer e1=".1" e4=0 e5=0 for i=1 to 100 e5=e5+e1 e3=Erf(e5) e4=e4+e3 next t=timer-t ? fp2str_exp(e4.dec_num, NUM_DIGITS) ? t ? "NUM_DIGITS = ";NUM_DIGITS #endif #if 0 ' ========================================================================= ' Karatsuba multiplication for decfloat_struct using 8-digit limbs ' ========================================================================= Declare Function decfloat_karatsuba (Byref x As decfloat_struct, Byref y As decfloat_struct) As decfloat_struct Declare Sub decfloat_normalize (Byref num As decfloat_struct) Declare Sub decfloat_set_zero (Byref num As decfloat_struct) Declare Sub decfloat_set_string (Byref num As decfloat_struct, Byref s As String) Declare Function decfloat_to_string (Byref num As decfloat_struct) As String Declare Function karatsuba_limbs (x() As Long, y() As Long, n As Long) As String Declare Function add_limbs (a() As Long, b() As Long, n As Long) As String Declare Function sub_limbs (a() As Long, b() As Long, n As Long) As String Declare Function shift_limbs (s As String, n As Long) As String Declare Function strip_limbs (s As String) As String Declare Function pad_limbs (s As String, l As Long) As String ' ========================================================================= ' Main Karatsuba function for decfloat ' ========================================================================= Function decfloat_karatsuba (Byref x As decfloat_struct, Byref y As decfloat_struct) As decfloat_struct Dim As decfloat_struct result Dim As String x_str, y_str, result_str ' Convert to strings for processing x_str = decfloat_to_string(x) y_str = decfloat_to_string(y) ' Handle zero cases If x_str = "0" Or y_str = "0" Then decfloat_set_zero(result) Return result End If ' Perform karatsuba multiplication result_str = karatsuba_limbs(x.mantissa(), y.mantissa(), NUM_DWORDS + 1) ' Set result result.sign = x.sign * y.sign result.exponent = x.exponent + y.exponent - BIAS ' Convert result string back to mantissa Dim As Integer i, j Dim As String temp = result_str ' Pad to fit mantissa temp = pad_limbs(temp, (NUM_DWORDS + 1) * 8) For i = 0 To NUM_DWORDS result.mantissa(i) = Val(Mid(temp, i * 8 + 1, 8)) Next i decfloat_normalize(result) Return result End Function ' ========================================================================= ' Karatsuba multiplication on limb arrays ' ========================================================================= Function karatsuba_limbs (x() As Long, y() As Long, n As Long) As String Dim As String x_str, y_str Dim As Integer i ' Convert limbs to string representation x_str = "" y_str = "" For i = 0 To n - 1 x_str += Right("00000000" + Str(x(i)), 8) y_str += Right("00000000" + Str(y(i)), 8) Next i ' Strip leading zeros x_str = strip_limbs(x_str) y_str = strip_limbs(y_str) ' Base cases If x_str = "0" Or y_str = "0" Then Return "0" End If ' If small enough, do direct multiplication If Len(x_str) <= 8 And Len(y_str) <= 8 Then Return Ltrim(Str(Culng(x_str) * Culng(y_str))) End If ' Equalize lengths Dim maxlen As Long maxlen = Len(x_str) If Len(y_str) > maxlen Then maxlen = Len(y_str) ' Round up to multiple of 8 for limb alignment If (maxlen Mod 8) <> 0 Then maxlen = maxlen + (8 - (maxlen Mod 8)) Dim As String xpad, ypad xpad = pad_limbs(x_str, maxlen) ypad = pad_limbs(y_str, maxlen) ' Split into halves (aligned to 8-digit boundaries) Dim As Long m, m2 m = maxlen \ 2 ' Adjust m to be multiple of 8 If (m Mod 8) <> 0 Then m = m - (m Mod 8) m2 = maxlen - m Dim As String xh, xl, yh, yl xh = Left(xpad, m) xl = Right(xpad, m2) yh = Left(ypad, m) yl = Right(ypad, m2) ' Recursive steps Dim As String z2, z0, z1 Dim As Long xh_limbs(0 To (m \ 8) - 1), yh_limbs(0 To (m \ 8) - 1) Dim As Long xl_limbs(0 To (m2 \ 8) - 1), yl_limbs(0 To (m2 \ 8) - 1) ' Convert halves back to limbs for recursion For i = 0 To (m \ 8) - 1 xh_limbs(i) = Val(Mid(xh, i * 8 + 1, 8)) yh_limbs(i) = Val(Mid(yh, i * 8 + 1, 8)) Next i For i = 0 To (m2 \ 8) - 1 xl_limbs(i) = Val(Mid(xl, i * 8 + 1, 8)) yl_limbs(i) = Val(Mid(yl, i * 8 + 1, 8)) Next i z2 = karatsuba_limbs(xh_limbs(), yh_limbs(), m \ 8) z0 = karatsuba_limbs(xl_limbs(), yl_limbs(), m2 \ 8) ' z1 = (xh + xl) * (yh + yl) - z2 - z0 Dim As String sumx, sumy, p3 sumx = add_limbs(xh_limbs(), xl_limbs(), m2 \ 8) sumy = add_limbs(yh_limbs(), yl_limbs(), m2 \ 8) ' Convert sums to limbs for recursion Dim As Long sumx_limbs(0 To (m2 \ 8) - 1), sumy_limbs(0 To (m2 \ 8) - 1) For i = 0 To (m2 \ 8) - 1 sumx_limbs(i) = Val(Mid(pad_limbs(sumx, m2), i * 8 + 1, 8)) sumy_limbs(i) = Val(Mid(pad_limbs(sumy, m2), i * 8 + 1, 8)) Next i p3 = karatsuba_limbs(sumx_limbs(), sumy_limbs(), m2 \ 8) z1 = sub_limbs(p3, z2) z1 = sub_limbs(z1, z0) ' Combine results with shifting Dim As String term1, term2 term1 = shift_limbs(z2, m2 * 2) term2 = shift_limbs(z1, m2) Dim As String finalresult finalresult = add_limbs(term1, term2) finalresult = add_limbs(finalresult, z0) Return strip_limbs(finalresult) End Function ' ========================================================================= ' Helper functions for limb operations ' ========================================================================= Function add_limbs (a() As Long, b() As Long, n As Long) As String Dim As String a_str, b_str Dim As Integer i a_str = "" b_str = "" For i = 0 To n - 1 a_str += Right("00000000" + Str(a(i)), 8) b_str += Right("00000000" + Str(b(i)), 8) Next i Return add_limbs_string(strip_limbs(a_str), strip_limbs(b_str)) End Function Function add_limbs_string (Byref a As String, Byref b As String) As String Dim As Long maxlen maxlen = Len(a) If Len(b) > maxlen Then maxlen = Len(b) Dim As String apad, bpad apad = pad_limbs(a, maxlen) bpad = pad_limbs(b, maxlen) Dim res As String, carry As Long res = Space(maxlen) carry = 0 Dim As Long i, digit_sum Dim As Long limb_a, limb_b ' Process 8 digits at a time For i = maxlen To 1 Step -8 limb_a = Val(Mid(apad, i - 7, 8)) limb_b = Val(Mid(bpad, i - 7, 8)) digit_sum = limb_a + limb_b + carry carry = digit_sum \ 100000000 Mid(res, i - 7, 8) = Right("00000000" + Str(digit_sum Mod 100000000), 8) Next i If carry > 0 Then res = Right("00000000" + Str(carry), 8) + res End If Return strip_limbs(res) End Function Function sub_limbs (Byref a As String, Byref b As String) As String Dim As Long maxlen maxlen = Len(a) Dim As String bpad bpad = pad_limbs(b, maxlen) Dim res As String, borrow As Long res = Space(maxlen) borrow = 0 Dim As Long i, diff Dim As Long limb_a, limb_b For i = maxlen To 1 Step -8 limb_a = Val(Mid(a, i - 7, 8)) limb_b = Val(Mid(bpad, i - 7, 8)) diff = limb_a - limb_b - borrow If diff < 0 Then diff = diff + 100000000 borrow = 1 Else borrow = 0 End If Mid(res, i - 7, 8) = Right("00000000" + Str(diff), 8) Next i Return strip_limbs(res) End Function Function shift_limbs (Byref s As String, Byval n As Long) As String If s = "0" Or n <= 0 Then Return s Else ' Shift by n digits (decimal) Return s + String(n, "0") End If End Function Function pad_limbs (Byref s As String, Byval l As Long) As String Dim diff As Long diff = l - Len(s) If diff > 0 Then Return String(diff, "0") + s Else Return s End If End Function Function strip_limbs (Byref s As String) As String Dim As Long i For i = 1 To Len(s) If Mid(s, i, 1) <> "0" Then Return Mid(s, i) End If Next i Return "0" End Function ' ========================================================================= ' decfloat_struct utility functions ' ========================================================================= Sub decfloat_set_zero (Byref num As decfloat_struct) num.sign = 0 num.exponent = 0 For i As Integer = 0 To NUM_DWORDS num.mantissa(i) = 0 Next i End Sub Sub decfloat_set_string (Byref num As decfloat_struct, Byref s As String) ' Implementation would parse string into decfloat format ' This is a placeholder End Sub Function decfloat_to_string (Byref num As decfloat_struct) As String Dim As String result = "" For i As Integer = 0 To NUM_DWORDS result += Right("00000000" + Str(num.mantissa(i)), 8) Next i Return strip_limbs(result) End Function Sub decfloat_normalize (Byref num As decfloat_struct) ' Normalize the decfloat number (left-justify mantissa) ' This is a placeholder for the normalization logic End Sub #if 0 ' ========================================================================= ' Example usage ' ========================================================================= Cls Dim As decfloat_struct num1, num2, result ' Example: Set up test values decfloat_set_string(num1, "12345678901234567890") decfloat_set_string(num2, "98765432109876543210") Print "Multiplicand: "; decfloat_to_string(num1) Print "Multiplier: "; decfloat_to_string(num2) Print "--------------------------------------------------" result = decfloat_karatsuba(num1, num2) Print "Product: "; decfloat_to_string(result) #endif dim as decfloat u dim as long i u=5 for i=0 to 10 u=6*fpcos(u) next ? u #endif #if 0 'dim as decfloat_struct x, y, z dim as string s, sa dim as long i dim as double t, dx /' s="3.14159265358979323846264338327950288419716939938" sa="4.14159265358979323846264338327950288419716939938" x=str2fp(s) y=str2fp(sa) z=fpmul(x,y) ? fp2str_exp(z) ? " 1.30111970546791518572971343831556540195109" '/ dim as decfloat a, b, q s=string(20000,"7") a=s s=string(10000,"9") b=s t=timer 'pi.dec_num=pi_chudnovsky_bs 'pi.dec_num=Pi_Ramanujan for i=1 to 1000 q=a/b next t=timer-t ? q ? t #endif #if 0 dim as decfloat x, y, z dim as long i dim as double t dim as string s s="7."+string(8*(NUM_DWORDS+1)-1,"7") x=s s="9."+string(8*(NUM_DWORDS+1)-1,"9") y=s z=0 t=timer for i=1 to 10000 z=z+x/y next t=timer-t print z print using "####.##"; t #endif #if 0 /' t=timer y.dec_num=pi_chudnovsky_bs 'Pi_Ramanujan t=timer-t ? y ? t '/ '3.141592653589793 dim as decfloat_struct x, y, z dim as string s, sa dim as long i dim as double dx s="3.14159265358979323846264338327950288419716939938" sa="4.14159265358979323846264338327950288419716939938" x=str2fp(s) y=str2fp(sa) z=fpmul(x,y) ? fp2str_exp(z) ? " 1.30111970546791518572971343831556540195109" #endif /' dim as decfloat_struct x, y, z dim as string s, sa dim as long i dim as double t t=timer z=si2fp(1) y=si2fp(1) x=si2fp(2) for i=2 to 10000 z=fpmul(z, x) x=fpadd(x, y) next t=timer-t ? t t=timer x=si2fp(10000) for i=2 to 10000 z=fpdiv(z, x) x=fpsub(x, y) next t=timer-t ? fp2str_exp(z) ? t '/ /' Dim As decfloat_struct x, y, p, z Dim As String s dim as double t dim as long i '/ /' s = "3.14159265358979323846264338327950288419716939938" x = str2fp(s) s = "2.71828182845904523536028747135266249775724709370" y = str2fp(s) z = fpmul(x, y) Print fp2str_exp(z) Print " 8.53973422267356706546355086954657449503488853577" Print z = fpmul(x, y, 2) Print fp2str_exp(z) Print " 8.53973422267356706546355086954657449503488853577" '/ /' s="1."+string(NUM_DIGITS, "7") x = str2fp(s) s="1."+string(NUM_DIGITS, "9") y = str2fp(s) z=si2fp(0) t=timer for i=1 to 1000 p=fpdiv(x, y) z=fpadd(z, p) next t=timer-t Print fp2str_exp(z) ? t '/ /' z=si2fp(0) t=timer 'for i=1 to 10000 'y=Pi_Ramanujan z=Pi_Ramanujan ' z=fpadd(z, y) 'next t=timer-t Print fp2str_exp(z) ? t '/ /' Dim As Integer j Dim As decfloat w, a = 0, b = 1, n = 10 Dim As decfloat cheby(n), coef(n) For i = 0 To n-1 coef(i) = fpCos(fpCos(fppi/n*(i+1/2))*(b-a)/2+(b+a)/2) Next i For i = 0 To n-1 w = 0 For j = 0 To n-1 w += coef(j) * fpCos(fppi/n*i*(j+1/2)) Next j cheby(i) = w*2/n Print i; " : "; fp2str(cheby(i).dec_num,16) Next i '/ dim shared as decfloat global_A, global_B, global_C3, pi type BS_Node P as decfloat Q as decfloat S as decfloat end type private sub sato_binary_split(byref node as BS_Node, byval a as ulong, byval b as ulong) if (b - a) = 1 then if a = 0 then node.P = 1 node.Q = 1 node.S = global_A else dim as decfloat t1, t2(0 to 0) node.P = 24 t1 = (6 * a) - 1 node.P = node.P * t1 t1 = (2 * a) - 1 node.P = node.P * t1 t1 = (6 * a) - 5 node.P = node.P * t1 node.Q = a node.Q.dec_num=fpipow(node.Q.dec_num, 3) node.Q = node.Q * global_C3 t1 = a t1 = t1 * global_B node.S = global_A + t1 node.S = node.S * node.P end if else dim midle as ulong = a + ((b - a) / 2) dim lft as BS_Node dim rht as BS_Node sato_binary_split(lft, a, midle) sato_binary_split(rht, midle, b) node.P = lft.P * rht.P node.Q = lft.Q * rht.Q dim as decfloat temp1, temp2 temp1 = lft.S * rht.Q temp2 = lft.P * rht.S node.S = temp1 + temp2 end if end sub dim shared as decfloat Anum(8), Aden(7) Anum(0)="1" '1 Anum(1)="3.50539460920830099644536085206731977894640094679385243" '3 Anum(2)="4.87710283492156398889295470575928514131188129485235350" '5 Anum(3)="3.41940895501646579979358718696598697251607140267823961" '7 Anum(4)="1.26394711149268552452566275541316358327959174191875335" '9 Anum(5)="0.233405328867973440371165770076974564078377353226555068" '11 Anum(6)="0.0178472673488843614118914367316745040139135799989080498" '13 Anum(7)="0.000294966662039048682597418242243216059997035836571251729" '15 Anum(8)="-2.16890333117587634195148035855408334732073816472822690e-6" '17 Aden(0)="1" '0 Aden(1)="3.83872794254163432977869418540065311227973428012636609" '2 Aden(2)="5.95667881576877543215251943422616951207179268167760818" '4 Aden(3)="4.78008011462154026836487863748505571122691489039321328" '6 Aden(4)="2.10324998151237574926453504405669234671879893125883020" '8 Aden(5)=".493810513612874221431592015003681204508470847406417473" '10 Aden(6)="0.548683522157329900969740174066228981407728597822493421e-1" '12 Aden(7)="0.206274285026300984945965748824214534265137852664365510e-2" '14 function fpAtan123(byref x1 as decfloat) as decfloat dim as decfloat tn, x, x2, eps, num, den dim as long i, sign, n, r if x1=0 then return x1 x=x1 sign=1 r=0 eps="0.0061361" ' approximately Pi/512 if x<0 then sign=-1 x=-x end if if x>1 then x=1/x r=1 ' set reciprocal flag end if n=0 while x>eps ' divide argument in half x=x/(1+sqr(1+x*x)) n+=1 wend x2=x*x num = Anum(8) den = Aden(7) For i = 7 To 0 Step -1 num = num * x2 + Anum(i) Next i For i = 6 To 0 Step -1 den = den * x2 + Aden(i) Next i tn=x*num/den if n>0 then ' if the argument was halved then restore for i=1 to n tn=tn+tn next end if if r=1 then tn=fpPihalf-tn end if if sign<0 then tn=-tn end if return tn end function sub main() dim iterations as ulong = (NUMBER_OF_DIGITS \ 50 - ((NUMBER_OF_DIGITS Mod 50) <> 0)) dim as decfloat sqrt5 dim as decfloat inner_A dim as decfloat inner_B dim as decfloat inner_C dim as decfloat C dim as decfloat temp1 dim as decfloat temp2 temp1 = 5 sqrt5 = sqr(temp1) temp1 = "4912746253692362754607395912" temp1 = temp1 * sqrt5 temp2 = "10985234579463550323713318473" inner_C = temp2 + temp1 inner_C = sqr(inner_C) C = "-214772995063512240" temp1 = "-96049403338648032" temp1 = temp1 * sqrt5 C = C + temp1 temp1 = -1296 temp1 = temp1 * sqrt5 temp1 = temp1 * inner_C C = C + temp1 global_C3.dec_num=fpipow(C.dec_num, 3) temp1 = "4870929086578810225077338534541688721351255040" temp1 = temp1 * sqrt5 temp2 = "10891728551171178200467436212395209160385656017" inner_A = temp2 + temp1 inner_A = sqr(inner_A) global_A = "63365028312971999585426220" temp1 = "28337702140800842046825600" temp1 = temp1 * sqrt5 global_A = global_A + temp1 temp1 = 384 temp1 = temp1 * sqrt5 temp1 = temp1 * inner_A global_A = global_A + temp1 temp1 = "2799650273060444296577206890718825190235" temp1 = temp1 * sqrt5 temp2 = "6260208323789001636993322654444020882161" inner_B = temp2 + temp1 inner_B = sqr(inner_B) global_B = "7849910453496627210289749000" temp1 = "3510586678260932028965606400" temp1 = temp1 * sqrt5 global_B = global_B + temp1 temp1 = 3110 temp1 = sqr(temp1) temp2 = 2515968 temp1 = temp1 * temp2 temp1 = temp1 * inner_B global_B = global_B + temp1 dim root as BS_Node sato_binary_split(root, 0, iterations) temp1 = -global_C3 temp1 = sqr(temp1) temp1 = temp1 * root.Q pi = temp1 / root.S end sub 'dim as double t /' t=timer main t=timer-t print pi print t '/ /' t=timer pi.dec_num=Pi_Ramanujan 'pi_chudnovsky_bs t=timer-t print pi print t '/ dim as decfloat a, c, f, s, x, z, y dim as long i, j dim as double t /' a = "1" c = "0" t=timer for i=1 to 100000 step 2 b = str(2*i-1) d = a / b c = c + d b = str(2*i+1) d = a / b c = c - d next t=timer-t ? "c = "; c ? t '/ /' a="1.23456789e2" b=fpexp(a) ? b '/ /' t=timer s=0 for j=1 to 10000 f=1 for i=1 to 89 x=i f=f/x s=s+f next next t=timer-t ? s ? t '/ /' t=timer z=0 for j=1 to 1000 for i=1 to 1000 x=i y.dec_num=fpsqr(x.dec_num) z=z+y next next t=timer-t Print fp2str_exp(z.dec_num) ? t '/ t=timer 'pi.dec_num=Pi_Ramanujan pi.dec_num=pi_chudnovsky_bs t=timer-t print pi print t /' x="0.23" a="1" c=0 t=timer for i=1 to 1000 x=x+a y=fpsin(x) c=c+y next t=timer-t ? c ? t '/ /' ? "fppi = ";fppi ? "fpln2 = ";fpln2 ? "fpexp1 = ";fpexp1 ? "fpPihalf = ";fpPihalf ? "fpPi2 = ";fpPi2 ? "Pi3o2 = ";Pi3o2 ? "fpdtor = ";fpdtor ? "fpPIo6 = ";fpPIo6 ? "fpSQR3 = ";fpSQR3 ? "fpTMS3 = ";fpTMS3 ? "fpconst1olog10 = ";fpconst1olog10 ? "sqrt2pi = ";sqrt2pi '/ /' for i=1 to 10 x=i y=fpAtan123(x) ? i,fp2str(y.dec_num,50) next '/