Here's a head scratcher for you math fans...
#2
hi Pete
I would do something like the following
Code: (Select All)
Dim As Double x, ip, fp, eps
Dim As _Integer64 n, n0, n1, d, d0, d1
Dim As Long sign

x = .33333333333333333333
x = 3.1415926535897932
eps = 1E-13
n0 = 0
n1 = 1
d0 = 1
d1 = 0

sign = Sgn(x)
x = Abs(x)
ip = Fix(x)
fp = x - ip 'fp=frac(x)

n = ip * n1 + n0
d = ip * d1 + d0
n0 = n1: n1 = n
d0 = d1: d1 = d
While (fp > eps) And n < 1000000000 And d < 1000000000
    'Print n; "/"; d
    x = 1 / fp
    ip = Fix(x)
    fp = x - ip 'fp=frac(x)

    n = ip * n1 + n0
    d = ip * d1 + d0
    n0 = n1: n1 = n
    d0 = d1: d1 = d
Wend
If sign < 0 Then n = -n
Print n; "/"; d
the code converts a given decimal to continued fraction convergents but in this example only the last convergents are printed
Reply


Messages In This Thread
RE: Here's a head scratcher for you math fans... - by Jack - 08-12-2022, 11:01 AM



Users browsing this thread: 13 Guest(s)