Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

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,401
    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:
    43
    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:
    13
    some Mathf methods are just System.Math conversions to float
     
    ArunPrakasam likes this.
  12. marcospgp

    marcospgp

    Joined:
    Jun 11, 2018
    Posts:
    194
    Sorry for the necro but this is the first Google result

    I believe at this point it makes sense to use C#'s MathF library instead of Unity's Mathf?
     
    andyz likes this.
  13. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,502
    Wow, I wasn't even aware: https://learn.microsoft.com/en-us/dotnet/api/system.mathf?view=net-7.0

    It lacks common gamedev things like Lerp and Smoothstep, for most other cases it probably doesn't matter. I can't see myself switching out of convenience.

    For maximal performance, there's Burst and Mathematics https://docs.unity3d.com/Packages/com.unity.mathematics@1.3/manual/index.html

    But Unity themselves don't recommend switching for an existing application. Mathf is fine for normal usage, despite its notoriety.

    https://docs.unity3d.com/Packages/com.unity.mathematics@1.3/manual/compatibility.html
     
  14. marcospgp

    marcospgp

    Joined:
    Jun 11, 2018
    Posts:
    194
    UnityEngine.Mathf
    relies on
    System.Math
    which only works with
    double
    s, while
    System.MathF
    works with
    single
    s/
    float32
    s.

    This may have a small effect if any however, since this code runs on the CPU. I believe the GPU should have a stronger variance between 32 and 64 bit floating point math.
     
  15. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,594
    It's a bit interesting to me why Mathematics is so much slower when using Mono. In a presentation when they showed the numbers (many years ago), it seems they didn't quite have an answer why it was so much slower with Mono (I believe the presenter said something along the lines of "we definitely need to investigate what happens there"). I guess many years later no one has an answer?

    EDIT: I misremembered, the presenter gives an explanation at around 32minutes.
     
    Last edited: Jan 2, 2024
    FaithlessOne likes this.
  16. andyz

    andyz

    Joined:
    Jan 5, 2010
    Posts:
    2,243
    This is an important point! - I feel some il2cpp tests are needed to see what the final comparison is!

    Edit: But if using il2cpp it seems they already consider this UnityEngine.Mathf should use System.MathF - Unity Forum