Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are
spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the
password reset link.
Due to spam on this forum, all posts now need moderator approval.
Entire forum
➜ MUSHclient
➜ Lua
➜ Lua and large numbers
It is now over 60 days since the last post. This thread is closed.
Refresh page
| Posted by
| Nick Gammon
Australia (23,166 posts) Bio
Forum Administrator |
| Date
| Wed 27 Feb 2008 11:36 PM (UTC) Amended on Wed 27 Feb 2008 11:37 PM (UTC) by Nick Gammon
|
| Message
| I want to point out that Lua stores numbers internally as "double" (in C terminology). Since MUSHclient is compiled under the Microsoft compiler, the definition of the Microsoft double is:
The double type contains 64 bits: 1 for sign, 11 for the exponent, and 52 for the mantissa. Its range is +/–1.7E308 with at least 15 digits of precision.
An important point here is that the mantissa is 52 bits, so the largest number you can reliably store and add 1 to (to reach the next number up) is 2^52, or 4,503,599,627,370,496.
Whilst you can store larger numbers some of the low-order precision will be thrown away, since the mantissa only stores 52 bits. As an example:
print (2e100 == 2e100 + 1) --> true
This example shows that it thinks that 2e100 and 2e100 plus one are the same number. (The number 2e100 is a "2" followed by 100 zeroes). Effectively the processor (this is the maths chip, not Lua specifically) has had to throw away some of the low-order bits in order to store that really large number.
By way of comparison, smaller numbers work correctly:
print (2e10 == 2e10 + 1) --> false
The break-even point, as I mentioned earlier, is 52 bits, so you can test it like this:
print (2^52 == 2^52 + 1) --> false (correct)
print (2^53 == 2^53 + 1) --> true (incorrect, they are different)
This is really only of academic interest, usually if you are working with really large numbers you don't care if they are absolutely accurate.
However if it really matters, you can use the "bc" library, which works with full precision.
http://www.gammon.com.au/scripts/doc.php?general=lua_bc
|
- Nick Gammon
www.gammon.com.au, www.mushclient.com | | Top |
|
The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).
To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.
7,049 views.
It is now over 60 days since the last post. This thread is closed.
Refresh page
top