There’s not much to say, but why are the trig functions returning inaccurate values?
At pi or 180 degrees, sin should be 0 and cosine should be -1, but for some reason, sin is 1.2? Is this a bug, or did I forget my trig values?
There’s not much to say, but why are the trig functions returning inaccurate values?
At pi or 180 degrees, sin should be 0 and cosine should be -1, but for some reason, sin is 1.2? Is this a bug, or did I forget my trig values?
It’s because math.pi is an approximation of pi, not the exact number. It isn’t 1.2, it’s actually like 0.0000000000000000012 or something.
this is just floating point arithmetic, internally, math.pi
is not exactly π but a 64bit binary approximation, likewise cos(math.pi)
comes out exactly -1.0 only cause rounding happens to land on a value whose cosine is as -1.0 and If you really need to test for zero, use a tolerance:
abs(math.sin(x)) < 1e-15
or round results
round(math.sin(math.pi), 10) # => 0.0
I know math.pi
is not the exact value because it’s infinitely long, but calculators use fewer decimal places and they still get it right. I could swear that before the whole studio revamp update the values were fine.
As you can see, the decimal length for the TI-84 calculator is less than that of Roblox but yet it can accurately give me the correct values.
Also, can you or just anyone else explain the huge difference here? How do I have only 2 decimal places of pi but yet I’m more accurate?
The calculator is rounding results. We don’t even know all of pi’s decimals so how could we mathematically reach zero without shaving some decimals off?
Yea I know the answer is rounded but can you answer the question at the end? That’s what Im more interesting in if you don’t mind
You are not more accurate. The “e-16” at the end of the long number means that there are so many zeros that it can’t be displayed in a clearly readable way.
You’re comparing 0.00159… to 0.000000000000000012…
Roblox uses a fixed point representation of floating point numbers as a 16-bit integer divided by some power of 2. This fixed point representation has a limited range and precision compared to the standard 32-bit or 64-bit floating-point representations used in most computers. With that, certain computations involving very large numbers or number(s) near the 16-bit fixed point representation’s limits might not produce the correct results.
See here this documentation:
Feel free to see this, as I feel this is similar in this case.
Oh yeah I forgot how to read scientific notation lol. It’s kinda late, so I tend to miss things. Tbh Roblox should adopt the rounding system of calculators to make things easier
It used to be like that but they stopped because it was confusing when 3 ~= 3 because it was actually 3 being compared against 3.000000000000002 or w/e
We do not want to round numbers before comparing them. This leads to inaccuracies and tolerance issues. It’s better to leave the output as is and let developers handle rounding if they desire
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.