Jump to content

Differences between calculating server/clientside


driver2

Recommended Posts

Hello,

I ran into a bit of trouble calculating with big numbers in a clientscript. I investigated into it and it seems the client has a different version of LUA than the server. Apparently there is a "float" and a "double" version, the former being only able to hold exact numbers up to 2^24. With bigger numbers, it doesn't calculate accurately anymore. For example, I tried to calculate with a number representing about a year in seconds (and thus being about 30,000,000), which lead to strange results like that:

crun 30000000 + 1
Command results: 30000000 [number]

However on the server:

run 30000000 + 1
Command results: 30000001 [number]

Apparently numbers are internally floating point numbers. If the number gets too big, the floating point is moved and it doesn't store all the digits anymore, which obviously makes the number less accurate and can lead to e.g. the '1' in the example above to not register anymore. Someone else can probably explain it better, since I only just learned about that myself.

The question is, can this be changed to work with bigger numbers?

Greetings

Link to comment

It might be also the reason why getPlayerMoney() (client-side) returns not exact amount of money if that is large number.

Eg. if player's money is between 99 999 999 and 99 999 992 than you will get 1 000 000 000. It seems to change only every 8 numbers.

Link to comment

This is how floating point numbers work - they can't represent any number you want them to, they might represent 2 as 2.000001 and 3000 as 2999.001, but they can represent more accurately the closer you are to 0. This is why you should never check if a floating point number equals something in particular, it's likely never to even if you think it should. As such, if you keep doing calculations with floating point numbers (particularily large ones) they'll get increasingly inaacurate as the results of each calculation is rounded to a (seemingly random) number. A solution would be to reprsent big numbers as two smaller ones, or to generally reduce your accuracy (do you need to represnt time in seconds over a year? What about days, hours, minute and seconds or just days and seconds?)

Link to comment

All numbers in Lua are floating point, even if they could be represented as an integer. Which is clearly a somewhat odd decision given their limitations, but it does bring with it some convenience...

A double can store numbers from roughly -2^308 to 2^308, whereas a similar sized (64 bit) integer can only store 2^64 values...

http://lua-users.org/wiki/FloatingPoint might be of interest...

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...