Author Topic: decfloat 2.0  (Read 10282 times)

0 Members and 1 Guest are viewing this topic.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: decfloat 2.0
« Reply #15 on: October 12, 2021, 11:28:56 pm »
Quote
if someone were to actually use this

I have this thread book marked to return to later to study more closely.

Is the Bernoulli thing for his equation used in physics for fluid flow?

Offline jack

  • Seasoned Forum Regular
  • Posts: 408
    • View Profile
Re: decfloat 2.0
« Reply #16 on: October 12, 2021, 11:44:23 pm »
bplus, the example program simply computes the Bernoulli numbers, as you may know the numbers come up frequently in power series, aside from that I don't know.
but thank you for your interest :-)
here's a Float version, should be easier to follow the flow
notice how quickly the numbers become inaccurate
Code: QB64: [Select]
  1. Dim As String f: f = "###  ####.############"
  2. Print "  Bn      computed"
  3. Const n = 20
  4. Dim As Long i, j
  5. Dim As _Float b(0 To n)
  6. For i = 0 To n
  7.     b(i) = 1 / (i + 1)
  8.     For j = i To 1 Step -1
  9.         b(j - 1) = j * (b(j - 1) - b(j))
  10.     Next j
  11.     Print Using f; i; b(0)
  12.  
« Last Edit: October 12, 2021, 11:53:43 pm by jack »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: decfloat 2.0
« Reply #17 on: October 13, 2021, 12:01:55 am »
Quote
the example program simply computes the Bernoulli numbers, as you may know the numbers come up frequently in power series,

Ah even better, always liked Taylor series. That was how I was going to do higher functions for String Math.

Offline jack

  • Seasoned Forum Regular
  • Posts: 408
    • View Profile
Re: decfloat 2.0
« Reply #18 on: October 14, 2021, 06:51:16 am »
did a major upgrade, fixed several bugs and renamed the sub's and functions to be easier to remember
added trig functions see https://www.qb64.org/forum/index.php?topic=4276.msg136873#msg136873
list of sub's/functions
Code: [Select]
Sub fpadd (result As decfloat, x As decfloat, y As decfloat, digits_in As Long)
Sub fpsub (result As decfloat, x As decfloat, y As decfloat, digits_in As Long)
Sub fpmul (result As decfloat, x As decfloat, y As decfloat, digits_in As Long)
Sub fpmul_si (result As decfloat, x As decfloat, y As Integer64, digits_in As Long)
Sub fpdiv (result As decfloat, x As decfloat, y As decfloat, digits_in As Long)
Sub fpdiv_si (result As decfloat, num As decfloat, den As Long, digits_in As Long)
Sub fpinv (result As decfloat, m As decfloat, digits_in As Long)
Sub fpipow (result As decfloat, x As decfloat, e As Integer64, digits_in As Long)
Sub fppow (result As decfloat, lhs As decfloat, rhs As decfloat)
Sub fpnroot (result As decfloat, x As decfloat, p As Long, digits_in As Long)
Sub fpsqr (result As decfloat, num As decfloat, digits_in As Long)
Sub fplog (result As decfloat, x As decfloat, digits_in As Long)
Sub fpexp (result As decfloat, x As decfloat, digits_in As Long)
Sub fpsin (result As decfloat, x As decfloat, digits_in As Unsigned Long)
Sub fpcos (result As decfloat, z As decfloat, digits_in As Unsigned Long)
Sub fptan (result As decfloat, z As decfloat, digits_in As Unsigned Long)
Sub fpasin (result As decfloat, x As decfloat, digits_in As Unsigned Long)
Sub fpacos (result As decfloat, x As decfloat, digits_in As Unsigned Long)
Sub fpatn (result As decfloat, x As decfloat, digits_in As Unsigned Long)
Sub fpabs (result As decfloat, n As decfloat)
Sub fpneg (result As decfloat, n As decfloat)
Sub fpfix (result As decfloat, num As decfloat)
Sub fpfrac (result As decfloat, num As decfloat, digits_in As Long)

Sub si2fp (result As decfloat, m As Integer64, digits_in As Long)
Sub str2fp (result As decfloat, value As String)
Function fp2dbl# (n As decfloat)
Function fp2str$ (n As decfloat, places As Long)
Function fp2str_exp$ (n As decfloat, places As Long)
Function fp2str_fix$ (n As decfloat, places As Long)

Offline jack

  • Seasoned Forum Regular
  • Posts: 408
    • View Profile
Re: decfloat 2.0
« Reply #19 on: April 02, 2022, 10:15:47 pm »
here's a program that calculates the first and last 40 digits of insanely huge Fibonacci numbers, in this example it's for Fibonacci( 2 ^ 1024)
the fib program is based on a program in page 14 of this document https://www.uni-math.gwdg.de/tschinkel/gauss/Fibon.pdf
result on my PC
you will need the bi and bm from  https://www.qb64.org/forum/index.php?topic=4276.msg136873#msg136873
Code: [Select]
2116996771162424982619007079477296709285
 5727677983280861229066251547465117987387
 4.023000000001048
Code: QB64: [Select]
  1. 'you must use $NoPrefix
  2. Dest Console
  3.  
  4. Option Explicit
  5.  
  6. Const NUMBER_OF_DIGITS = 620 ' you must have NUMBER_OF_DIGITS defined -- not to exceed 1024
  7. '                             before including decfloat.bi
  8. '$include: 'decfloat.bi'
  9.  
  10. '======================================================================
  11. ' your code follows
  12.  
  13. Dim As decfloat first, last, fn, m, x, y, z, v
  14. t = Timer(.001)
  15. 'str2fp fn, "4784969" ' "4784969"
  16. si2fp fn, 2, NUM_DWORDS
  17. fpipow fn, fn, 1024, NUM_DWORDS
  18. str2fp m, "10000000000000000000000000000000000000000"
  19. si2fp x, 5, NUM_DWORDS
  20. fpsqr x, x, NUM_DWORDS
  21. si2fp z, 1, NUM_DWORDS
  22. fpadd y, x, z, NUM_DWORDS
  23. fpdiv_si y, y, 2, NUM_DWORDS
  24. fplog y, y, NUM_DWORDS
  25. fpmul y, y, fn, NUM_DWORDS
  26. fplog z, x, NUM_DWORDS
  27. fpsub y, y, z, NUM_DWORDS
  28. si2fp x, 10, NUM_DWORDS
  29. fplog v, x, NUM_DWORDS
  30. fpdiv y, y, v, NUM_DWORDS
  31. fpfrac z, y, NUM_DWORDS
  32. fppow y, x, z
  33. s = fp2str(y, 40)
  34. dp = InStr(s, ".")
  35. s = Left$(s, dp - 1) + Mid$(s, dp + 1)
  36. fib fn, m, last
  37. t = Timer - t
  38. s = fp2str(last, NUMBER_OF_DIGITS)
  39. dp = InStr(s, ".")
  40. Print Left$(s, dp - 1)
  41.  
  42.  
  43. Sub fib (k As decfloat, m As decfloat, fibonacci As decfloat)
  44.     Dim As decfloat b, x, y, xx, temp, tmp, rn
  45.     Dim As Long i, bit_length
  46.     str2fp rn, ".05"
  47.     si2fp tmp, 1, NUM_DWORDS
  48.     If fpcmp(k, tmp, NUM_DWORDS) <= 0 Then
  49.         fibonacci = k
  50.         Exit Sub
  51.     End If
  52.     si2fp x, 1, NUM_DWORDS
  53.     si2fp y, 0, NUM_DWORDS
  54.     fplog tmp, k, NUM_DWORDS
  55.     si2fp temp, 2, NUM_DWORDS
  56.     fplog xx, temp, NUM_DWORDS
  57.     fpdiv b, tmp, xx, NUM_DWORDS
  58.     fpfix tmp, b
  59.     bit_length = fp2dbl(tmp)
  60.     si2fp b, 2, NUM_DWORDS
  61.     fpipow b, b, bit_length - 1, NUM_DWORDS
  62.     For i = bit_length - 1 To 0 Step -1
  63.         fpmul xx, x, x, NUM_DWORDS
  64.         fpdiv xx, xx, m, NUM_DWORDS
  65.         fpfrac xx, xx, NUM_DWORDS
  66.         fpmul xx, xx, m, NUM_DWORDS
  67.         fpadd xx, xx, rn, NUM_DWORDS
  68.         fpfix xx, xx
  69.         fpmul x, x, y, NUM_DWORDS
  70.         fpmul_si x, x, 2, NUM_DWORDS
  71.         fpadd x, xx, x, NUM_DWORDS
  72.         fpdiv x, x, m, NUM_DWORDS
  73.         fpfrac x, x, NUM_DWORDS
  74.         fpmul x, x, m, NUM_DWORDS
  75.         fpadd x, x, rn, NUM_DWORDS
  76.         fpfix x, x
  77.         fpmul y, y, y, NUM_DWORDS
  78.         fpadd y, y, xx, NUM_DWORDS
  79.         fpdiv y, y, m, NUM_DWORDS
  80.         fpfrac y, y, NUM_DWORDS
  81.         fpmul y, y, m, NUM_DWORDS
  82.         fpadd y, y, rn, NUM_DWORDS
  83.         fpfix y, y
  84.         fpdiv temp, k, b, NUM_DWORDS
  85.         If fpfix_is_odd(temp) Then
  86.             temp = x
  87.             fpadd x, x, y, NUM_DWORDS
  88.             fpdiv x, x, m, NUM_DWORDS
  89.             fpfrac x, x, NUM_DWORDS
  90.             fpmul x, x, m, NUM_DWORDS
  91.             fpadd x, x, rn, NUM_DWORDS
  92.             fpfix x, x
  93.             y = temp
  94.         End If
  95.         fpdiv_si b, b, 2, NUM_DWORDS
  96.     Next
  97.     fibonacci = x
  98.  
  99. ' end of your code
  100. ' you must include decfloat.bm
  101. '$include: 'decfloat.bm'
  102.