I’m making axe, and I’m trying to store health of the log at this height, but I want to round it to one decimal, because setAttribute doesn’t allow to use dots in name. So I want to replace it by _.
The problem is when I store the ceiled value, Y, into the Vector3. It somehow gets diffrent value, in this example it changed 7.397985458374023 to 7.4 to 7.400000095367432 . I want to keep the middle number as Y value in Vector3.
I’ve tried to make the commented ceil function into real function, but the same problem happened. It ceiled the number into 7.4, but after I printed Y value of Vector3 I recieved 7.40000…
local placeholderPos : Vector3, treePart : BasePart? = Raycast()
print(placeholderPos) -- -131.2268829345703, **7.397985458374023**, -11.67298412322998
-- ceil function
local Y = placeholderPos.Y * 10
print(Y) -- 73.97985458374023
Y = math.ceil(Y)
print(Y) -- 74
Y = Y / 10
print(Y) -- **7.4**
local position = Vector3.new(
placeholderPos.X,
Y,
placeholderPos.Z
)
print(position.Y) -- **7.400000095367432**
If i remember it’s because computers suck at precise calculations hence why we only know X amount of numbers of Pi. I don’t think there is any way to combat this, as long as its 74.000 you should be fine.
I get right number in the function. It returns 7.4. But when I set the Y value of Vector3 to 7.4, it changes to 7.4.00…095367432. The problem is with storing the number in Vector3.
I am assuming you’re getting the right number in the function because it is being formatted to 1 decimal by Roblox automatically, 7.4 then when you try and print the position of Y you get (7.4000 e.t.c) which is still 7.4. This isn’t an issue, and i believe it’s just cause computers can’t do precise math.
If you really want to be precise maybe you can try doing something like this, although i do not see the point in this and i’m not sure if it would even work basically just converting to a string thats formatted to 2 decimal places using %.2f" then converting back to a number no clue if it would actually even work but in theory it should. But keep in mind it is a 10/10 on the chart of no use case lol
function format(num)
local formatted = string.format("%.2f", math.floor(num*100)/100)
if string.find(formatted, ".00") then
return string.sub(formatted, 1, -4)
end
return formatted
end
tonumber(format(position.Y))
I think there’s a misunderstanding. “7.4000…” that I wrote in original message was 7.400000095367432, but I didn’t want to write the entire number, so I wrote 7.4000 with 3 dots. I don’t get number 7.40000000000 or 7.4000 or anything like that. I get 7.400000095367432 as Y value in the Vector3 - position. And that value breaks the code.
A little hard to understand what you want here… Is this what you’re looking for?
local function format(num)
return string.format("%.1f", num):gsub("%.", "_")
end
local y = math.ceil(7.397985458374023 * 10) / 10
local result = format(y)
print(result)
Okay so,
I’m trying to ceil Y value of Vector3 - placeholderPos and save it as Y value of another Vector3 - position. The problem is the when I create the new Vector3 - position with Y value as 7.4 and I try to print the Y value of position, I get this number: 7.400000095367432.
It doesn’t print the initial number 7.4 that I set the Y value of position, but it prints 7.400000095367432.
This number makes my code break. Sometimes I get the right value, but sometimes it prints this long number. For example I tried the axe on diffrent height and I get 5.26885 as Y value in placeholderPos so the code ceiled and saved it as Y value in position. And this time it worked the way it should. But other times the Y value in position is off by 0.0000547226 or number like that.
I haven’t tried your code yet, so I’ll check it. I’ll write you in a minute or two.
I tried your code.
It works right, but I would like if the Y value in Vector3 would stay ceiled to 1 digit. If I set the Y value of Vector3 to 7.4, I want it to stay it like that, but it changes to 7.400000095367432.
I’ll consider this as solution, if I won’t find anything else.
But thanks for suggestion.
Little bit of different way of looking at it… using nearlyEqual
local function ceil1(x)
return math.ceil(x * 10 + 1e-6) / 10
end
local function nearlyEqual(a, b)
return math.abs(a - b) < 1e-4
end
local placeholderPos = Vector3.new(-131.2268829345703, 7.397985458374023, -11.67298412322998)
local y = ceil1(placeholderPos.Y)
local position = Vector3.new(placeholderPos.X, y, placeholderPos.Z)
print(position.Y)
print(nearlyEqual(position.Y, 7.4))
There is no way to make the float internally “exactly 7.4” in Roblox.
I’m “cheating” using strings in that other script. This script don’t need it to be exactly 7.4
local placeholderPos = Vector3.new(-131.2268829345703, 7.397985458374023, -11.67298412322998)
local y = math.ceil(placeholderPos.Y * 10) / 10
local pos = Vector3.new(placeholderPos.X, y, placeholderPos.Z)
print(string.format("%.1f", pos.Y)) -- prints exactly 7.4
That’s the same thing you suggested a few messages back.
The y variable is 7.4, but position.Y is 7.400000095367432.
I want “print(position.Y)” to print exactly 7.4, not 7.400000095367432.
I don’t want to use “string.format(”%.1f", position.Y)".
local Vector3_64 = {}
Vector3_64.__index = Vector3_64
function Vector3_64.new(x, y, z)
return setmetatable({x = x, y = y, z = z}, Vector3_64)
end
function Vector3_64:__tostring()
return string.format("Vector3_64(%.10f, %.10f, %.10f)", self.x, self.y, self.z)
end
local function ceil1(x)
return math.ceil(x * 10 + 1e-6) / 10
end
local placeholderPos = Vector3.new(-131.2268829, 7.3979854, -11.6729841)
local pos64 = Vector3_64.new(placeholderPos.X, ceil1(placeholderPos.Y), placeholderPos.Z)
print(pos64)
print(pos64.y) -- will print exactly 7.4 (as stored)
local function ceil1(x)
return math.ceil(x * 10) / 10
end
local placeholderPos = Vector3.new(-131.2268829345703, 7.397985458374023, -11.67298412322998)
local y = ceil1(placeholderPos.Y)
local position = vector.create(placeholderPos.X, y, placeholderPos.Z)
print(y) -- 7.4
print(position.Y) -- 7.400000095367432
Even if the placeholderPos X value and Z value is “normal” values, like -132 and -13, the issue is the same., Y value of position is unceiled - 7.800000190734863 (diffrent location). (The other values, X and Z, are the same as they are in placeholderPos.
Now I noticed, in propeties of part that has that position.Y value as it’s Position.Y, it is ceiled. Y position of the part is ceiled, but name, which is position.Y is unceiled. Position is right, but the name and the value, after printing is off. Name of that part is 7.800000190734863.
7.400000095367432 is the closest possible value to 7.4 because it is not possible to make 7.4 out of any number of digits divided by a power of two, which is how it must be stored. https://www.h-schmidt.net/FloatConverter/IEEE754.html
The ceiled number is 7.3, position.Y is 7.3 (Vector3_64). But when I pass it to waitforchild it breaks.
If I click once, it makes the part and names it 7.3.
Based on calculator from azqjanna, the number should be 7.30000019073486328125, not 7.2999999999999998224.