driver2 Posted October 18, 2009 Share Posted October 18, 2009 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
DiSaMe Posted October 18, 2009 Share Posted October 18, 2009 I don't like this problem either. This happens not only with big numbers. For example, if you divide 256 by 10, you get 25.60000038147. Link to comment
50p Posted October 18, 2009 Share Posted October 18, 2009 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
eAi Posted October 18, 2009 Share Posted October 18, 2009 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
50p Posted October 18, 2009 Share Posted October 18, 2009 it's not only floating point numbers unless the money is actually a floating point number but than rounded? Try to give yourself lots of money via admin panel and see how much money the panel will display. Link to comment
eAi Posted October 18, 2009 Share Posted October 18, 2009 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
driver2 Posted October 19, 2009 Author Share Posted October 19, 2009 My point is that there seem to be different versions of Lua used for the server and the client. Or why is one more exact than the other? Why not use the more accurate 'double' version for both? Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now