Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

UnityEngine.Mathf vs System.Math

Discussion in 'Editor & General Support' started by TheEnigmist, Jul 5, 2012.

  1. TheEnigmist

    TheEnigmist

    Joined:
    Jun 10, 2012
    Posts:
    51
    I'm new in unity and i don't know what are the difference between Mathf and Math. Which is faster and better optimised? There are difference if i use Math instead of Mathf?
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,341
    System.Math uses doubles, UnityEngine.Mathf uses floats. Since Unity uses floats, it's generally better to use Mathf so you're not constantly converting floats/doubles.

    --Eric
     
  3. Krobill

    Krobill

    Joined:
    Nov 10, 2008
    Posts:
    282
    Math is usually faster than Mathf even with the necessary double to float conversion (on PC at least, didn't run the tests on other platforms). So if you don't mind putting (float) everywhere and you're looking for the optimal performances go for Math. Then again if you're really after extreme performances, some operations like Sin and Cos are better handled with home made precomputed results...
     
  4. TheEnigmist

    TheEnigmist

    Joined:
    Jun 10, 2012
    Posts:
    51
    What will be the difference if i try to implement my own math lib? Will performance be high or just a bit more?
     
  5. Toasttify

    Toasttify

    Joined:
    Mar 27, 2012
    Posts:
    143
    Don't try to reinvent the wheel unless you truly have to, more than likely the original will be faster.
     
    SamFernGamer4k and TV4Fun like this.
  6. TheEnigmist

    TheEnigmist

    Joined:
    Jun 10, 2012
    Posts:
    51
    Infact it is only a wast of time :D
    So System.Math is the best! Thx for the info :)
     
    SamFernGamer4k and yaguat like this.
  7. atr0phy

    atr0phy

    Joined:
    Nov 5, 2014
    Posts:
    29
    I highly doubt your 'benchmarks' are accurate, regardless. From what I can tell from the assemblies, Mathf is just a wrapper using casts for the respective System.Math functions.

    E.g.
    Code (CSharp):
    1. public static float Sqrt(float f) { return (float)Math.Sqrt((double)f); }
    So, if anything, Mathf will generally perform slightly slower.
     
  8. skauth

    skauth

    Joined:
    Jun 4, 2017
    Posts:
    17
    Also, something I just discovered:
    `System.Math.Round(val, precision)` exists.
    `Mathf.Round(val, precision)` does not work.
    I'm sure there's more like this.
     
    josefloresvargas likes this.
  9. icanseebraggplanes

    icanseebraggplanes

    Joined:
    Mar 23, 2018
    Posts:
    1
    Hi, sorry I know this thread is really old I'm just starting out in c# and unity and just wondered where you can find the definitions of methods like Mathf.Sqrt()? For example, all i can see in the docs is an example of using Mathf.Sqrt() but I would like to know where I can find the explicit definition of the method itself, ie. how it wraps and casts the input and output of System.Math.Sqrt(). Where did you find your specific example? Thanks
     
  10. rakkarage

    rakkarage

    Joined:
    Feb 3, 2014
    Posts:
    683
  11. eskivor

    eskivor

    Joined:
    Aug 5, 2015
    Posts:
    11
    some Mathf methods are just System.Math conversions to float
     
unityunity