pianoman
LV6 Super Member (Next: 500)
    
Offline
Gender: 
Last Login: 20 October, 2011, 04:23:28
Date Registered: 21 May, 2011, 19:13:57
Location: You can know that my current velocity is about 0 m/s.
Posts: 427
Topic starter
Total Post Ratings: +24
|
 |
« on: 13 September, 2011, 00:26:28 » |
0
|
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?
|
|
|
|
|
Logged
|
|
|
|
calc84maniac
Epic z80 roflpwner
Coder Of Tomorrow
LV11 Super Veteran (Next: 3000)
Offline
Gender: 
Last Login: Today at 07:09:04
Date Registered: 28 August, 2008, 05:09:05
Location: Right behind you.
Posts: 2737
Total Post Ratings: +376
|
 |
« Reply #1 on: 13 September, 2011, 01:01:34 » |
0
|
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.
|
|
|
|
|
Logged
|
"Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman
|
|
|
NecroBumpist
LV4 Regular (Next: 200)
  
Offline
Gender: 
Last Login: 04 November, 2012, 07:02:18
Date Registered: 18 August, 2011, 05:44:50
Location: In my IDE, programming shit
Posts: 129
Total Post Ratings: +9
|
 |
« Reply #2 on: 13 September, 2011, 01:36:10 » |
0
|
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.
|
|
|
|
|
Logged
|
|
|
|
Jim Bauwens
Lua! Nspire! Linux!
Editor
LV10 31337 u53r (Next: 2000)
Offline
Gender: 
Last Login: Today at 14:26:18
Date Registered: 28 February, 2011, 22:32:12
Location: Belgium
Posts: 1736
Total Post Ratings: +180
|
 |
« Reply #3 on: 13 September, 2011, 09:54:00 » |
0
|
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: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| 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: 1
| 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
|
|
|
|
« Last Edit: 13 September, 2011, 09:59:35 by jimbauwens »
|
Logged
|
|
|
|
pianoman
LV6 Super Member (Next: 500)
    
Offline
Gender: 
Last Login: 20 October, 2011, 04:23:28
Date Registered: 21 May, 2011, 19:13:57
Location: You can know that my current velocity is about 0 m/s.
Posts: 427
Topic starter
Total Post Ratings: +24
|
 |
« Reply #4 on: 14 September, 2011, 05:04:36 » |
0
|
Very interesting... thanks guys 
|
|
|
|
|
Logged
|
|
|
|
|