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.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Mathf for doubles?

Discussion in 'Scripting' started by ackley14, Jul 8, 2015.

  1. ackley14

    ackley14

    Joined:
    Oct 31, 2014
    Posts:
    31
    So im working with some fairly large doubles (upwards of 1+E20) and i was wondering if there are any Mathf libraries that use doubles? of if there's a workarround for this alternatively. i know unity can do normal maths with doubles just fine but the .NET mathf functions convert to a float first which breaks numbers above 1+E18.

    specifically Mathf.Log() and Mathf.Ceil/Floor() in my case is what i need to be able to use

    Im not really sure how to do those myself so i was hoping someone knew of a free library that would provide that functionality. or alternatively a place where i could learn to replicate the functions for my own use would actually be prefered.
     
  2. ackley14

    ackley14

    Joined:
    Oct 31, 2014
    Posts:
    31
    NEVERMIND: >_> I FEEL SO DUMB!!!

    so apparently .NET has built in math functions that use doubles instead of floats...referenced by Math.log() instead of Mathf.log()....welll...problem solved i guess XD
     
  3. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    Yep, System.Math:
    https://msdn.microsoft.com/en-us/library/system.math(v=vs.110).aspx

    You may also be surprised to know that Mathf is really just a wrapper around System.Math that does the double to float conversion for you.

    Code (csharp):
    1.  
    2. using System;
    3. using System.Runtime.CompilerServices;
    4. using UnityEngine.Internal;
    5.  
    6. namespace UnityEngine
    7. {
    8.   public struct Mathf
    9.   {
    10.     public const float PI = 3.141593f;
    11.     public const float Infinity = float.PositiveInfinity;
    12.     public const float NegativeInfinity = float.NegativeInfinity;
    13.     public const float Deg2Rad = 0.01745329f;
    14.     public const float Rad2Deg = 57.29578f;
    15.     public const float Epsilon = 1.401298E-45f;
    16.  
    17.     public static float Sin(float f)
    18.     {
    19.       return (float) Math.Sin((double) f);
    20.     }
    21.  
    22.     public static float Cos(float f)
    23.     {
    24.       return (float) Math.Cos((double) f);
    25.     }
    26.  
    27.     public static float Tan(float f)
    28.     {
    29.       return (float) Math.Tan((double) f);
    30.     }
    31.  
    32.     public static float Asin(float f)
    33.     {
    34.       return (float) Math.Asin((double) f);
    35.     }
    36.  
    37.     public static float Acos(float f)
    38.     {
    39.       return (float) Math.Acos((double) f);
    40.     }
    41.  
    42.     public static float Atan(float f)
    43.     {
    44.       return (float) Math.Atan((double) f);
    45.     }
    46.  
    47.     public static float Atan2(float y, float x)
    48.     {
    49.       return (float) Math.Atan2((double) y, (double) x);
    50.     }
    51.  
    52. //... so on so forth
    53.  
     
    Alaadel and Kiwasi like this.