- Home /
Wrong implementation of Pi in Mathf Library
I just noticed that the value of Mathf.PI seems to be wrongly implemented. The definition reads:
public const float PI = 3.14159274F;
Obviously the last two digits should be 65. Is this a bug?
Whats your use case that those last digits matter for you?
Answer by Bunny83 · Aug 22, 2019 at 11:04 AM
No, It's not a bug. 32 bit floating point numbers only have a 24 bit mantissa. So the number that is stored here is the best fit. Actually the "4" at the end is already no longer representable. Try this online IEEE754 converter. Copy&paste the value "3.14159274" in the top field and press enter. Now try to toggle the least significant bit (the last one far to the right). The number will turn into 3.1415925 which is too small. This is the next representable value. The error with the 74 at the end is just "9" off while one below is "15" off. So the value is the best fit.
For more information see this computerphile video and the table I've posted over here
Your answer
Follow this Question
Related Questions
There is a way to import an ODE solver library and use it? 0 Answers
How to import an expression evaluator into unity? 2 Answers
Are there any easing functions built into Unity 5? 1 Answer
[RESOLVED] Raycast - Calculate Angular Difference from Object 3 Answers
radar orthogonal line to plane 1 Answer