Omnimaga

Calculator Community => TI Calculators => Lua => Topic started by: pianoman on September 12, 2011, 06:26:28 pm

Title: Exact Lua
Post by: pianoman on September 12, 2011, 06:26:28 pm
I just realized that Lua doesn't give exact answers, only approximations.
Can you guys think of a way to get it to display exact answers?
Title: Re: Exact Lua
Post by: calc84maniac on September 12, 2011, 07:01:34 pm
Lua numbers are 64-bit floating point, they don't store any extra information like TI-Nspire Basic does about whether it's the square root of 3 or pi or whatever.
Title: Re: Exact Lua
Post by: NecroBumpist on September 12, 2011, 07:36:10 pm
If you need more accuracy than Doubles can offer, you're going to have to create your own layer of abstraction.
This might be something like a Big Number library, or a complex number library.
Title: Re: Exact Lua
Post by: Jim Bauwens on September 13, 2011, 03:54:00 am
I would suggest to use math.eval and call basic function to do the calculation. But sadly enough getNum and getDenom don't work good through math.eval.
But ... gcd() (greatest common divisor) does, so I made my own function:
Code: (Lua) [Select]
function exact(n)
local n_int, n_float = math.modf(n)

local n_size = math.pow(10, #tostring(n_float)-2)

local num = n_float * n_size
local denom = n_size

local gcd = math.eval("gcd(" .. num .. "," .. denom .. ")")

num = num/gcd
denom = denom/gcd

return n_int, num, denom
end

It works like this:
Code: [Select]
big, num, denom = exact(13.125)big will be 13
num will be 1
denom will be 8
So, 13 1/8 .

It doesn't do any error checking though, thats your task :p
Title: Re: Exact Lua
Post by: pianoman on September 13, 2011, 11:04:36 pm
Very interesting... thanks guys :)