|
| 12 Jul 2011 05:43 PM |
So, I was strolling through the wiki when I came accross the math library page, and decided to take a looksies.
One of the first things I noticed was the derpy usage of numbers like 1.5707963267948966192313216916398, and 2.71828182845904523536.
So I decided to do a little research.
print(1.5707963267948966192313216916398)
results in 1.5707963267949 That's because Lua automagically rounds number constants/literals at compilation so that they fit within an IEEE 754 64bit double.
Now this made no difference in the example: print(math.deg (1.5707963267948966192313216916398)) --> 90 print(math.deg (1.5707963267949)) --> 90
Now that's all fine and dandy, but it was the second, smaller number and its example that really made me herp my derp.
print(math.log (2.71828182845904523536)) --> 1
So that time it prints one, but what happens when we try shortening the number ?
print(2.71828182845904523536) --> 2.718281828459 print(math.log (2.71828182845904523536)) --> 1 print(math.log (2.718281828459)) --> 0.99999999999998
So this made me derp quite a bit. So what causes these two different answers ?
One would think that "if the big number rounds down to the little one why doesn't it give the same answer ?"
The only speculation that I can offer you is that Lua has some derpy Float-To-ASCII conversion routines and vice versa. (aka; when you print the big number it leaves out just a tiny bit of accuracy that can be influenced by entering the larger number in full)
I'm going to assume my conjecture is correct because after a quick disassembly, it turns out that Lua uses two different constants for the larger number and the smaller number, leading me to believe they are encoded slightly different (though they both resolve to "2.718281828459" according to a function supplied by XlegoX), and I'm too lazy to compare the bytecode by hand.
I hope you guys have enjoyed this little adventure.
On a special note, this isn't the first time I've encountered problems with Lua's derpy handling of floats, problems can also arise from functions like math.modf()
local a, b = math.modf(3.16) b = b * 100
print(b) --> 16 print(16 == b) --> false |
|
|
| Report Abuse |
|
|
|
| 12 Jul 2011 05:45 PM |
| Why do floats in any programming language ever have to be so limited? :L |
|
|
| Report Abuse |
|
|
LocalChum
|
  |
| Joined: 04 Mar 2011 |
| Total Posts: 6906 |
|
|
| 12 Jul 2011 05:53 PM |
MahBrain.Status == "Exploded";
TL;DR |
|
|
| Report Abuse |
|
|
|
| 12 Jul 2011 05:58 PM |
"Why do floats in any programming language ever have to be so limited? :L"
Limited? How often do you have to use a number like 1.5707963267948966192313216916398? "MahBrain.Status == "Exploded";"
This thread isn't that hard to understand.. |
|
|
| Report Abuse |
|
|
LocalChum
|
  |
| Joined: 04 Mar 2011 |
| Total Posts: 6906 |
|
|
| 12 Jul 2011 06:00 PM |
| TL;DR == BLAARGH TOO MUCH TEXT |
|
|
| Report Abuse |
|
|
blocco
|
  |
| Joined: 14 Aug 2008 |
| Total Posts: 29474 |
|
|
| 12 Jul 2011 09:52 PM |
they truncate the float to convert it to a string duh |
|
|
| Report Abuse |
|
|
burgly
|
  |
 |
| Joined: 20 Aug 2006 |
| Total Posts: 2843 |
|
|
| 12 Jul 2011 10:10 PM |
Of course it's limited. I was once stupid enough to try to use Lua to do competition programming: It was Slow, and couldn't support huge numbers. Lua took 2 and a half hours to do what C++ did in about 45 seconds. |
|
|
| Report Abuse |
|
|
blocco
|
  |
| Joined: 14 Aug 2008 |
| Total Posts: 29474 |
|
|
| 12 Jul 2011 10:11 PM |
| Coding a huge C++ program in 45 program takes like, hyper fingers and freaking skillz. |
|
|
| Report Abuse |
|
|
blocco
|
  |
| Joined: 14 Aug 2008 |
| Total Posts: 29474 |
|
| |
|
|
| 12 Jul 2011 10:51 PM |
"they truncate the float to convert it to a string"
Exactly. The print method calls tostring() on the string. io.write does it too, but unlike print, it doesn't print memory adresses.
print({})
--> table: insert memory adress in hex here
io.write({})
--> UMG DER'S AN ERROR UMG UMG |
|
|
| Report Abuse |
|
|
burgly
|
  |
 |
| Joined: 20 Aug 2006 |
| Total Posts: 2843 |
|
|
| 12 Jul 2011 11:03 PM |
@blocco, I was talking about run time. C++ in 45 seconds? You must be crazy, XD |
|
|
| Report Abuse |
|
|
|
| 12 Jul 2011 11:10 PM |
That's an unfair measurement. C: Compiles in an hour. Runs in 45 seconds.
Lua: Is interpreted, compiles, and runs in 30 minutes.
Hmmm... I think I see the problem... |
|
|
| Report Abuse |
|
|
|
| 12 Jul 2011 11:21 PM |
"Lua took 2 and a half hours to do what C++ did in about 45 seconds."
I'm going to give you the same answer I give everyone else who claims such incredibly large differences: You're doing it wrong. |
|
|
| Report Abuse |
|
|
|
| 12 Jul 2011 11:41 PM |
| pokelover980, he probably didn't use LuaJIT |
|
|
| Report Abuse |
|
|
|
| 12 Jul 2011 11:42 PM |
| LuaJIT master race! I <3 LuaJIT |
|
|
| Report Abuse |
|
|
burgly
|
  |
 |
| Joined: 20 Aug 2006 |
| Total Posts: 2843 |
|
|
| 12 Jul 2011 11:50 PM |
| I was using SciTE at the time. How good is LuaJIT? |
|
|
| Report Abuse |
|
|
|
| 12 Jul 2011 11:51 PM |
LuaJIT is just Lua on steroids. SciTE is an editor, LuaJIT is an entirely different implementation of Lua. |
|
|
| Report Abuse |
|
|
|
| 12 Jul 2011 11:59 PM |
"LuaJIT is just Lua on steroids."
Nah, it's more like if you took soma (lol Brave New World reference. If you Google it, make sure you add in Brave New World as well or you'll get the wrong thing), mixed it with caffeine, and then finally added steroids. |
|
|
| Report Abuse |
|
|
|
| 13 Jul 2011 09:07 AM |
Then LuaJIT isn't vanilla Lua and it isn't really Lua we're testing here, is it?
btw, what's a huge calculation I can do that will take a long time suitable for a program race? I'll probably try C/C++ vs. Java/Lua/JavaScript/Python. I'm not a mathematician, so I have no idea what calculations I could do.. |
|
|
| Report Abuse |
|
|
booing
|
  |
| Joined: 04 May 2009 |
| Total Posts: 6594 |
|
|
| 13 Jul 2011 09:17 AM |
trolololool int a = 1 double b = 12495071342895127 triple c = nu triplez? :( Again, assembly __asm { MOV AX,9999 MOV BX,9999 MOV CX,9999 MOV DX,9999 }
|
|
|
| Report Abuse |
|
|
|
| 13 Jul 2011 09:20 AM |
octo d=nu octo? :(
(For dat we gotta go to Japan)
|
|
|
| Report Abuse |
|
|