Roblox's rounding doesnt work for vectors

I am trying to round my numbers inside a function, but I am getting unexpected behavior. Sometimes the rounding simply never happens, while others it does. I tried adding a wait, and it still didn’t work.


image
( the 14.69999… is the line of code where I printed newModelSize.X )
( the 14.7 is the line of code I did the calculation outside the vector )

I am trying to round a vector3 so that my script can properly detect whether or not a vector3 is a devisor of another vector3, and in order to do this, I need perfectly rounded numbers.

As you can see from the images, the output incorrectly states the rounded number from the vector but not when I directly round it.

System information:

Windows 10 Home
10.0.19045 Build 19045
Intel(R) Core™ i5-10400 CPU, 2.90GHz, 2904 Mhz, 6 Cores, 12 Logical P…

Device Info

  • User agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 OPR/105.0.0.0
  • Platform Type: PC
  • Device Type: Computer

Beta Features Enabled:

AI-Powered Code Completion
Face Capture
Grass Length
Live Animation Creator
Material Generation
Multilayer Wrap Fix
Notification Overhaul
UIListLayout Flex Features
Updated Roblox Controls
Upgrade Shorelines
Video Uploads

Expected behavior

It should display a correctly rounded vector3 when I print.

1 Like

This is due to the new tostring functionality, i believe. I will try to find the post

Expected behavior.

As in many other programming languages, numbers in Lua are represented using floating point values. A floating point value cannot exactly represent the value 14.7 so the computation results in the closest representable value (14.6999…) instead.

This is the same reason you cannot do:

for i = 0, 1, 0.1 do
    ...

The rounding needs to be part of the printing function, not done on the number. For example, string.format("%.1f").

image

4 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.