Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local function to_frac(num)
- local int, fract = math.modf(num)
- if fract == 0 then
- return int, 1, 0
- end
- local W = math.floor(fract)
- local F = fract - W
- local pn, n, N = 0, 1
- local pd, d, D = 1, 0
- local x, err, q, Q
- repeat
- x = x and 1 / (x - q) or F
- q, Q = math.floor(x), math.floor(x + 0.5)
- pn, n, N = n, q*n + pn, Q*n + pn
- pd, d, D = d, q*d + pd, Q*d + pd
- err = F - N/D
- until math.abs(err) < 1e-15
- return N + D*W, D, int + err
- end
- if not _ARGS then return '!!fract [numb] (numb2)' end
- local n, n2 = string:match(_ARGS, '(%S+)%s*(.*)')
- n = tonumber(n)
- if not n then return 'Send numb.' end
- n2 = tonumber(n2)
- if n2 then
- local numb, dec, int = to_frac(n / n2)
- return string.format('%g/%g = %d/%d + %g = %d/%d', n, n2, numb, dec, int, numb + int * dec, dec)
- else
- local numb, dec, int = to_frac(n)
- return string.format('%g = %d/%d + %g = %d/%d', n, numb, dec, int, numb + int * dec, dec)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement