Like the string 'abc ' that has 4 values, 97, 98, 99 and 32. I can use those as variables with string.byte() and string.char(), so a 4 character string has 4 bytes of memory I can use as 4 separate variables, so long as I know what position corresponds to what variable. The string functions will slow it down to far below performance with actual bytes, but it might save on memory while making saving/loading easier.Boolsheet wrote:To address your other questions.I don't understand this question. Can you rephrase it?retrotails wrote:Would it be noticeably more efficient in any manner to have all globals in ONE string? (assuming they're numbers from 0-255)
Rounding issues & Memory
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- retrotails
- Party member
- Posts: 212
- Joined: Wed Apr 18, 2012 12:37 am
Re: Rounding issues & Memory
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Rounding issues & Memory
It's not going to be worth it. Just use a table of numbers when you need a table of numbers.
Help us help you: attach a .love.
Re: Rounding issues & Memory
Odds are this problem can also be fixed by multiplying your numbers by 1000000 and then dividing them when you need to output them, that'll garuantuee they remain integers during the calculations. (Well... simple calculations anyway; you still won't be able to do 1/3 or such)
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Rounding issues & Memory
That works, under these very specific assumptions:
- You multiply the numeric constants with this constant. So you have literally 999000 in your code and not 0.999 * 1000000
- You are very careful with addition and multiplication. For addition, you need to multiply any "normal" integer with 1000000 before adding it to such a "fake" integer. For multiplication, you need to make sure at least one of the numbers is a "normal" integer. If you multiply two "fake" integers, you need to divide the result of the multiplication by 1000000.
- That's it, I think. This space reserved for future gotchas I might think of.
Help us help you: attach a .love.
Re: Rounding issues & Memory
Using an inexact representation of floating numbers is a very reasonable optimization, especially for games where performance is often much more important than accuracy, and should be considered a feature, not a bug.
My game called Hat Cat and the Obvious Crimes Against the Fundamental Laws of Physics is out now!
Re: Rounding issues & Memory
I was running into the same issue when using floats in loops.
I've posted it to the Lua mailing list and got some interesting answers, maybe that clears things up regarding floats and rounding errors:
http://lua-users.org/lists/lua-l/2013-03/msg00979.html
I've posted it to the Lua mailing list and got some interesting answers, maybe that clears things up regarding floats and rounding errors:
http://lua-users.org/lists/lua-l/2013-03/msg00979.html
experimental art since 13.75 gigayears
https://github.com/humansarepuppies
http://stardiaries-lab.blogspot.com/
https://github.com/humansarepuppies
http://stardiaries-lab.blogspot.com/
- retrotails
- Party member
- Posts: 212
- Joined: Wed Apr 18, 2012 12:37 am
Re: Rounding issues & Memory
micha wrote:Do you have an example, where it is important to obtain an integer in the end, while in between you have non-integers?
Code: Select all
table = {}
for x = .2, 0, -0.1 do
table[x*10] = x
end
for i, k in pairs(table) do
print('i = ' .. i, 'k = ' .. k)
end
print(table[1])
Code: Select all
i = 2 k = 0.2
i = 1 k = 0.1
i = 2.7755575615629e-16 k = 2.7755575615629e-17
nil
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Rounding issues & Memory
What's wrong with:
?
Code: Select all
for x = 2, 0, -1 do
table[x] = x/10
end
Help us help you: attach a .love.
Re: Rounding issues & Memory
I understand the code, but I don't understand why in reality you would ever code something like this.retrotails wrote:micha wrote:Do you have an example, where it is important to obtain an integer in the end, while in between you have non-integers?Out:Code: Select all
table = {} for x = .2, 0, -0.1 do table[x*10] = x end for i, k in pairs(table) do print('i = ' .. i, 'k = ' .. k) end print(table[1])
This, maybe. Unless I did something wrong which is totally possible.Code: Select all
i = 2 k = 0.2 i = 1 k = 0.1 i = 2.7755575615629e-16 k = 2.7755575615629e-17 nil
So, I won't let that count as a valid example.
Check out my blog on gamedev
Who is online
Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 4 guests