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.FloatToHalf

Discussion in 'Scripting' started by Natrem, Nov 6, 2016.

  1. Natrem

    Natrem

    Joined:
    Feb 24, 2015
    Posts:
    31
    Hello!

    I found these functions in Mathf struct:
    Code (CSharp):
    1. public static ushort FloatToHalf(float val);
    2. public static float HalfToFloat(ushort val);
    I would be highly interested for networking paquets compression purposes and other topics, but I literally didn't find anything on Internet about it.
    I'd like to know how reliable it is, on which platforms it works, and if it will always be there in future releases. Bonus points if there is also information on performance (garbage generation?), but this I could try it myself (however on all wanted platforms, it could be long).

    Also, it's totally unrelated but why is Mathf a struct and not a static class?

    Thank you in advance!
     
  2. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    Its because all the backend of Unity is written in c++. And unless your using Managed c++ which I doubt they are, you can't create static classes in c++. So they used a struct instead of a class with static methods in them.
     
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    If it's not documented, then there are no guarantees about anything and using it would be at your own risk. (Although there are no guarantees about "always there in future releases" for any function, anyway, documented or not.)

    --Eric
     
  4. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,201
    If you complain about undocumented functions in the documentation forums, you usually get a reply to what they do.
     
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Why not just cast the thing yourself?

    Code (CSharp):
    1. half myHalf = (half)myFloat;
    I bet that's all Mathf does anyway.
     
  6. Natrem

    Natrem

    Joined:
    Feb 24, 2015
    Posts:
    31
    Thank you all for your replies!
    "half" doesn't exist natively in C#, unless I'm wrong.
    I'll probably ask in the documentation forums, since from the small tests I've made, Mathf.HalfToFloat(Mathf.FloatToHalf(myFloat)) == Mathf.HalfToFloat(Mathf.FloatToHalf(Mathf.HalfToFloat(Mathf.FloatToHalf(
    myFloat
    )))) seems to be true, but Mathf.HalfToFloat(Mathf.FloatToHalf(10000000f)) == Float.Infinity too.
     
  7. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,150
  8. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,201
    Looking at ILSpy, the method's extern. So I'm going to guess that FloatToHalf does a c++ downcast from float to half. The return value is an ushort, since C# doesn't have halfs, so the data's just stuck in ushort since it's 16 bits.

    No clue why they'd use a ushort over a short.

    If you're comfortable with the loss of precision from float to half, this seems like an ideal way to send data. It's probably a workaround specifically to handle C# not supporting halfs, to allow for sending lossy information about floating point values. Which is pretty neat!