Vector3 modifying values

I’m confused as to why Vector3 is modifying values I init with… Any ideas?
If you run this code do you get the same result?

Code:
local number = 5983914828
local v3 = Vector3.new(number,0,0)
print("number: ", number)
print("v3 number: ", v3.X)

output:
number: 5983914828
v3 number: 5983915008

Have you considered not putting such large numbers in for your Vector3?

The Vector3 documentation says Vector3 uses floats for the X, Y, and Z values, and not integers. The further you get from 0, the less accuracy you will have. When you lose enough accuracy, you’ll start to see that you won’t be able to enter certain values, and those values will be rounded or otherwise changed to the next closest value it can still represent.

Probably not a 100% technically accurate explanation, but should suffice for this response.

The vector3 docs say it uses numbers, and this page below says a number is a double in lua:

So is the Vector3 documentation wrong then, it’s not a number but a float? It actually doesn’t say float anywhere on the vector3 page.

Have you considered not putting such large numbers in for your Vector3?

I need to work with large numbers. Might have to create a custom vector3 implentation if vector3 doesn’t work for me.

When I said float, I meant floating point numbers in general. The documentation is not wrong, my post was just ambiguous. My bad.

If you need to work with large numbers, then you probably need to make your own implementation.

Just out of curiosity, what exactly are you doing that requires you to have a vector pointing 5983914828 studs in the X direction? Seems oddly specific and I can’t imagine a scenario with two objects in Roblox being that far apart to begin with lmfao

Can be a velocity, or used in parameters for Forces (either body movers or constraints) when you need a large number. Usually when you need an abnormally large force you write math.huge or inf instead of typing out a large number. Position is only one case and I agree, it doesn’t go beyond 100k studs without behaving weird.

@SirOliverGreenCheese I guess you might have to work with it yourself if it’s being annoying. If you are working with purely integers then I guess you can just use 3 int values or write them in lua

It’s because that’s a single precision, 32 bit floating point number. What that means is, this is not as precise as normal numbers that we use.

Thanks for all the info everyone. It looks like at least I know why it’s doing what it’s doing.
I’ll have to make my own implementation of some things.

That was just sample code, not specific code to my project. I’m working on something that involves massive distances between objects (galaxies, solar systems, etc…) I know roblox can’t handle that, so I’m doing a lot of fudging of things. This just appears to be another place I’ll have to work around it. I’ve done plenty of that already to get what I do have going.

Why not just scale the numbers down?

Any physically large representation of the universe isn’t practical (FPZ demonstrates this well), so why not scale it down a bunch to make the numbers more manageable? Instead of 10000000 studs, scale it down so it’s 100 instead or something.

Oh ya, I was already scaling stuff down to fit within robloxs limitation. Just was storing some large numbers for some other calculations of things and noticed this issue so I posted about it. I will be changing the way that section works now as well.

1 Like

As has been previously mentioned a few times in this thread, ROBLOX uses an outdated version of Lua that only has the capability of storing double precision floating point numbers, and with that comes some complex “randomization” when it comes to storing large numbers.

In no way are you “near the limit” though, as Lua can store numbers up to 2^53, just be aware that since ROBLOX’s Lua version can’t store integers you may be unable to get perfectly accurate results when storing numbers, especially with external factors like what you would experience with Vector3’s.

Not sure how much this is contributing now but I’d figured I’d give a bit more of a technical explanation as to why this is happening.

Roblox Vector3s and luau’s native vector datatype hold 3 single precision floats (three 4-byte floats). Regular lua numbers are doubles or 8 byte floats.

Single-precision values with float type have 4 bytes, consisting of a sign bit, an 8-bit excess-127 binary exponent, and a 23-bit mantissa
https://learn.microsoft.com/en-us/cpp/c-language/type-float?view=msvc-170

This means that it can hold up to a 2^23 -1 (8,388,607) values before losing precision and having to truncate the smaller digits. Vectors can store numbers larger than 8 million, but not always precisely if it has a lot of digits after it, the exact math is tricky depending on if something is a power of 2, but a rule of thumb is just to not trust vectors with anything bigger than 8 million if its an integer, and not do direct == comparisons if it is a decimal number. You can see how numbers are represented using this calculator

I know this is a 5 year old post but the documentation doesn’t have this anywhere