Big numbers

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Big numbers

Hugo <Nabble>
Try running this code in the lua demo page:
local x = 1432090800000
print(x + 3898092908)
It prints 1435988892908.

Now run this code in Luan:
local Io = require "luan:Io"
local x = 1432090800000
Io.print(x + 3898092908)
It prints 1.435988892908E12.

The Luan output is useless because we usually want to run other operations with the number (e.g., call javascript time functions, etc) and this format makes it hard to use.
Reply | Threaded
Open this post in threaded view
|

Re: Big numbers

Hugo <Nabble>
Remember that this should also work with string concatenation:
local Io = require "luan:Io"
local x = 1432090800000
local s = x .. ''
Io.print(s)
Reply | Threaded
Open this post in threaded view
|

Re: Big numbers

fschmidt
Administrator
In reply to this post by Hugo <Nabble>
You can do what you want like this:
local Luan = require "luan:Luan"
local Io = require "luan:Io"
local x = 1432090800000
Io.print(Luan.assert_long(x + 3898092908))
Lua 5.3 makes an explicit disctinction between ints and floats which I think is a very bad idea.  Try this in Lua:
local x = 1432090800000000000000
print(x + 3898092908)
It works in Luan.

I will keep this thread assigned to me to see if I can improve Luan's default behavior.
Woe to those who call bad good and good bad -- Isaiah 5:20
Following the Old Testament, not evil modern culture
Reply | Threaded
Open this post in threaded view
|

Re: Big numbers

Hugo <Nabble>
fschmidt wrote
You can do what you want like this:
local Luan = require "luan:Luan"
local Io = require "luan:Io"
local x = 1432090800000
Io.print(Luan.assert_long(x + 3898092908))
This works, thanks.
fschmidt wrote
Lua 5.3 makes an explicit disctinction between ints and floats which I think is a very bad idea.  Try this in Lua:
local x = 1432090800000000000000
print(x + 3898092908)
It works in Luan.
It only works in Luan if you don't use the Luan.assert_long() function. If you use it, it throws an error.
Reply | Threaded
Open this post in threaded view
|

Re: Big numbers

fschmidt
Administrator
I fixed the parser to parse numeric constants to ints or longs if possible.  I will leave optimizations for later.

Hugo <Nabble> wrote
It only works in Luan if you don't use the Luan.assert_long() function. If you use it, it throws an error.
As it should since the number is too big to be a long.  I think Luan is doing the right thing and Lua is doing the wrong thing by returning the wrong value.  Numbers should work consistently with mathematics, not consistently with computer architecture.
Woe to those who call bad good and good bad -- Isaiah 5:20
Following the Old Testament, not evil modern culture
Reply | Threaded
Open this post in threaded view
|

Re: Big numbers

Hugo <Nabble>
fschmidt wrote
Hugo <Nabble> wrote
It only works in Luan if you don't use the Luan.assert_long() function. If you use it, it throws an error.
As it should since the number is too big to be a long.  I think Luan is doing the right thing and Lua is doing the wrong thing by returning the wrong value.  Numbers should work consistently with mathematics, not consistently with computer architecture.
This is controversial because Luan doesn't necessarily have to depend on Java primitives. For example, big numbers could be automatically converted into java.math.BigInteger objects. But since programmers can access java directly from Luan, this isn't an issue. You can close this thread.
Reply | Threaded
Open this post in threaded view
|

Re: Big numbers

fschmidt
Administrator
closing
Woe to those who call bad good and good bad -- Isaiah 5:20
Following the Old Testament, not evil modern culture