- Home /
Mathf.log rounding issue
log of e, ln(1.0001) should = 0.000099995
https://www.google.com/search?q=ln(1.0001)
however, using Unity's Math.f , which uses a float, I get
Mathf.Log(1.0001f) = 0.0001000116
using System.Math, which uses a double
System.Math.Log(1.0001) 9.99950003332973E-05
is there anyway to stop Math.f from rounding this value?
or is there some variable type that will get me the exact values that google calc spits out.
The only option I see is converting my ToString("0.0000000000") or however many zeros are in a float, truncating it, then turning the double into a float.
Answer by Bunny83 · Jan 23, 2013 at 03:46 PM
Unity does use Math.Log inside the Mathf.Log function, however a float (single precision) can't represent the value that accurate. Even a double doesn't represent the value accurate. It's more accurate as a float, but still not right. There is no way to get a better result with a float. The only way is to use a double and cast the final result to float.
Answer by Berenger · Jan 23, 2013 at 03:26 PM
I guess Mathf has some approximation optimizations. Use System.Math if you need that much precision. You seldom do though.
Btw, $$anonymous$$athf.Log is implemented like this:
public static float Log(float f)
{
return (float)$$anonymous$$ath.Log((double)f);
}
$$anonymous$$ost $$anonymous$$athf functions look like this.
Your answer
Follow this Question
Related Questions
Mathf.Log() with negative numbers? 1 Answer
Troubleshooting desktop build receiving unhandled NULL exception 0 Answers
Debug.Log(); question. 1 Answer
What to do when there is an error at runtime 1 Answer
AABB Error, attached Log fragments. 1 Answer