Search Unity

UnityEngine.Mathf should use System.MathF

Discussion in 'Experimental Scripting Previews' started by runner78, Oct 12, 2021.

  1. runner78

    runner78

    Joined:
    Mar 14, 2015
    Posts:
    471
    I have seen UnityEngine.Mathf often uses the double based System.Math. But since .NET Standard 2.1 there are System.MathF.

    Unity from 2021.2 could switch to the new library and thus should improve performance in some places.
     
  2. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    5,668
    We have not done it so far because the UnityEngine assembly code does not have access to the .NET Standard 2.1 APIs. But we're working on changing that internally now. Once that is complete, then we can look at doing this. There are a few caveats related to the VM implementations though.

    For IL2CPP, the UnityEngine.Mathf APIs are already re-mapped as intrinsics to C standard library methods that do floating point math, so I think we're already getting the best performance.

    For Mono, the Mono version Unity used prior to Unity 2021.2 always did floating point math using 64-bit doubles, so this would not provide any benefit. The newer Mono has an option to use 32-bit floats, but that changes the behavior of some floating point code, so if we enable that, it will likely be an option.

    But at the API level, I think this suggestion makes a lot of sense. I'll raise it with the team. Thanks!
     
unityunity