Math question: storing 2 numbers in one

Get help using Construct 2

Post » Thu Aug 06, 2015 6:09 pm

is there a mathematical way to store 2 numbers (let's say x/y coordinates) as 1 number? perhaps like a seed?

if my x/y was -3002/1506 there doesn't seem to be a way to store that as 1 number, but is there a way to store a different number that somehow can be extrapolated into these 2?

You could maybe store them as hex and parse them out.. hex:AE would = 10,14

I supposed negative numbers would make that even more complicated. Any ideas? it's more of a curiosity...
B
100
S
33
G
16
Posts: 1,204
Reputation: 16,865

Post » Thu Aug 06, 2015 6:37 pm

It's more of a programming question that a math one. ;) I guess you could try your luck with bits manipulation, however it's quite limited in its current implementation. See manual's system expression "getbit" and "setbit".
B
74
S
31
G
26
Posts: 994
Reputation: 20,198

Post » Thu Aug 06, 2015 6:46 pm

@Magistross I could program parsing easy enough if it were saved as a string. I'm really asking is there a way to store 2 numbers as a single integer. i.e. do some sort of calculation to the two numbers so that a 3rd number somehow represents them both - one would obviously have to do something to the said 3rd number to "unpack" the other two...
B
100
S
33
G
16
Posts: 1,204
Reputation: 16,865

Post » Thu Aug 06, 2015 7:07 pm

I wasn't talking about parsing. getbit() and setbit() are used to set individual bits of a 32-bit integer. One could use them to create a function that take two integers in the 16-bit range and combine them in a single 32-bit integer. If we had bit-shifting operation, that would be much easier to do.

You could maybe try to use decimal shifting instead :

-packing
CombinedInt = FirstInt * 1000000 + SecondInt

-unpacking
FirstInt = floor(CombinedInt / 1000000)
SecondInt = CombinedInt % 1000000

It would work to combine numbers in the 0-999999 range.
B
74
S
31
G
26
Posts: 994
Reputation: 20,198

Post » Thu Aug 06, 2015 9:17 pm

@Magistross yeah I've done bit shifting, but I see that as a sort of parsing since you need to elect 16 of the bits to be one number and the remaining 16 to be a different number.

That (decimal shifting) math is what I was looking for.. cool, thanks! the only problem is negative numbers which presents a real problem. :(
B
100
S
33
G
16
Posts: 1,204
Reputation: 16,865

Post » Thu Aug 06, 2015 9:27 pm

You can offset that too. Add 100000 to the number before storing, and subtract that on restore. As long as no number can be beyond +/- 100000, then it all works out.
ImageImageImage
B
71
S
22
G
248
Posts: 3,758
Reputation: 138,067

Post » Fri Aug 07, 2015 3:20 am

okay so here's an example:

X:251 Y:3200

compress:
251 * 1,000,000 = 251,000,000 + 3200 = 251,003,200

unpacking:
first number:
251,003,200 / 1,000,000 = 251.0032 (use floor to get 251)

second number:
251,003,200 / 1,000,000 = 251.0032 (use floor to get 251)
251 * 1,000,000 = 251,000,000
251,003,200 - 251,000,000 = 3200 (or just use mod)

works perfect!

for negative numbers:

X:4 Y:-8

compress:
4 + 100,000 = 100,004
-8 + 100,000 = 99,992
100,004 * 1,000,000 = 100,004,000,000 + 99,992 = 100,004,099,992

@blackhornet so far this is a problem right? does this go beyond the highest value? isn't it int32: 2,147,483,647?
Last edited by jobel on Fri Aug 07, 2015 4:05 am, edited 3 times in total.
B
100
S
33
G
16
Posts: 1,204
Reputation: 16,865

Post » Fri Aug 07, 2015 3:51 am

The numbers Magistross and I picked were arbitrary. What are the maximum values you are expecting to get? Scale the constants based on those.
ImageImageImage
B
71
S
22
G
248
Posts: 3,758
Reputation: 138,067

Post » Fri Aug 07, 2015 4:01 am

the numbers that I need to use are between -20,000 and 20,000
B
100
S
33
G
16
Posts: 1,204
Reputation: 16,865

Post » Fri Aug 07, 2015 4:11 am

So to offset to a positive, you need only add 20000, pushing the top to 40000. So use 100000 as the multiplier: 2,000,000,000 + 40000 = 2,000,040,000 max.
ImageImageImage
B
71
S
22
G
248
Posts: 3,758
Reputation: 138,067

Next

Return to How do I....?

Who is online

Users browsing this forum: R0J0hound and 17 guests