Search Unity

  1. Check out the Unite LA keynote for updates on the Visual Effect Editor, the FPS Sample, ECS, Unity for Film and more! Watch it now!
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    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:
    31,872
    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.
     
  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 :)
     
  7. VorpalStudios

    VorpalStudios

    Joined:
    Nov 5, 2014
    Posts:
    6
    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:
    15
    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.
     
  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:
    658
  11. eskivor

    eskivor

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