Math.Log vs. Mathf.Log

Tried it in Unity and could only make it work using Mathf.Log.

1 Like

You have to include the System namespace to use any of the methods from the Math class, but yeah, you can also use Mathf and not include the System namespace.

as in System.Math.Log ?

Yes, you could do that too, or put at the top of your code “using System;”

1 Like

thanks Yee!

@Martin_Castaneda, the code examples are in pseudo-code, so aren’t written for any particular language. I’ve tried to keep them roughly equivalent to how you’d do things in C++/C# but you may have to make a few alterations to get them working in your engine of choice.

In this case, @Yee is correct, that Math is part of the System library, so you can either write System.Math.---- or add using System; to the top of your class.

As for the difference between System.Math and UnityEngine.Mathf - the only real difference is that, for most functions, the former tends to use doubles and the latter uses floats (hence the f at the end of the name). So mathf mainly acts as a nice wrapper so that you don’t have to constantly cast your variables.
As a result you may see some differences in performance and precision but it’s not usually all that noticeable unless you’re doing something fairly intense.

If you look at the actual implementation of Mathf.Log, it’s;

public static float Log(float f)
 {
     return (float)Math.Log((double)f);
 }
8 Likes

Thank you for asking this; I had the exact same question about c# and Unity.

Privacy & Terms