If Statement for Decimal Value?

Hello! :smiley:
I’m trying to achieve a local script that’s inside StarterCharacterScripts that detects if the local player’s head has a transparency of 0.3!

It works if I detect if it equals a whole number like 1, but not if it equals a decimal like 0.3.
How do I do it?
Here is my code

if script.Parent.Parent.Head.Transparency == 0.3 then --it only works for whole numbers?

1 Like

it does not, you can compare a value with decimals, what do you mean?

  Head.Transparency = 0.3

  if Head.Transparency == 0.3 then
     return print("it is 0.3") 
  end;

Why can I set the transparency of the head to 0.3 but can’t detect it?
I hope this isn’t the case…

I can’t detect it if it’s a decimal for some reason. That’s the problem im facing.

Unfortunately, most decimal fractions cannot be represented exactly as binary fractions. A consequence is that, in general, the decimal floating-point numbers you enter are only approximated by the binary floating-point numbers actually stored in the machine.

This is a quote from the python article about this topic. You can google “floating point decimal issues” for more information.

Basically, you should be doing this:

local function trunc(x)
    return math.floor(x * 10) / 10
end

if trunc(script.Parent.Parent.Head.Transparency) == 0.3 then
    -- now you know that it's actually 0.3
end
4 Likes

Do you want to know if it contains a decimal is between 0 and 1?

You can know if any number is a decimal because the sin(pi * x) will return a decimal if x is a decimal.

local function isDecimal(x)
    return math.sin(math.pi * x) ~= 0
end

A more practical function would be to use though:

local function isDecimal(x)
    return math.floor(x) ~= x
end

If the floor(x) is not x then x is a decimal.

He already knows that it’s a decimal.

oh I get it! your post makes sense. To add on, you can implement a round function by shifting your number over by 10, then flooring it, then shifting it back.

local function round(x)
    return math.floor(x * 10) / 10
end

(This is more accurately a truncate function but for purpose we can just call it “rounding”)

1 Like

i’ll edit my post so it includes this function

This is my whole script.
How could I implement that function into it?

while true do
if script.Parent.Parent.Head.Transparency == 0.3 then
script.Parent.CFrame = script.Parent.Parent.HumanoidRootPart.CFrame
end
wait()
end

I just edited my original post, you can see it in there.

Here’s another example of floating point decimal issues:

function randomOffset(vector, maxAngle)
	local rand1 = random:NextNumber(0, tau)
	local rand2 = random:NextNumber(math.cos(maxAngle / 1e3), 1)

	local res = (CFrame.new(zeroVector, vector)
		* CFrame.Angles(0, 0, rand1)
		* CFrame.Angles(math.acos(rand2), 0, 0)
	).LookVector

	print(res:Dot(vector.Unit))
	return res
end

When using this function, if you set maxAngle to 0, the resulting vector should be the same as the .Unit of the one originally provided. You can test this using result:Dot(originalVector.Unit), which is what I did in the function. If it’s the same vector, it should print 1, but it prints numbers like these instead: