Omnimaga: The Coders Of Tomorrow
Welcome, Guest. Please login or register.
 
Omnimaga: The Coders Of Tomorrow
19 June, 2013, 15:21:05 *
Welcome, Guest. Please login or register.

Login with username, password and session length
 
   home   news downloads projects tutorials misc forums rules new posts irc about Login Register  
+-OmnomIRC

You must Register, be logged in and have at least 40 posts to use this shout-box! If it still doesn't show up afterward, it might be that OmnomIRC is disabled for your group or under maintenance.

Note: You can also use an IRC client like mIRC, X-Chat or Mibbit to connect to an EFnet server and #omnimaga.

Pages: [1]   Go Down
  Print  
Author Topic: Exact Lua -  (Read 351 times) Bookmark and Share
0 Members and 1 Guest are viewing this topic.
pianoman
LV6 Super Member (Next: 500)
******
Offline Offline

Gender: Male
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

View Profile
« 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 Offline

Gender: Male
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

View Profile
« 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 Offline

Gender: Male
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

View Profile
« 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

Developing Lua scripts for the NSpire ?
Check out the Necrotorium
Need a few routines to run faster ? Checkout the MODS Lua Assembly Toolkit.
Need to save space for your scripts ? Checkout LuaSrcDiet
Jim Bauwens
Lua! Nspire! Linux!
Editor
LV10 31337 u53r (Next: 2000)
*
Offline Offline

Gender: Male
Last Login: Today at 14:26:18
Date Registered: 28 February, 2011, 22:32:12
Location: Belgium
Posts: 1736


Total Post Ratings: +180

View Profile WWW
« 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 Offline

Gender: Male
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

View Profile
« Reply #4 on: 14 September, 2011, 05:04:36 »
0

Very interesting... thanks guys Smiley
Logged

Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by EzPortal
Powered by MySQL Powered by SMF 1.1.18 | SMF © 2013, Simple Machines Powered by PHP
Page created in 0.282 seconds with 31 queries.
Skin by DJ Omnimaga edited from SMF default theme with the help of tr1p1ea.
All programs, games and songs avaliable on this website are property of their respective owners.
Best viewed in Opera, Firefox, Chrome and Safari with a resolution of 1024x768 or above.