Search Unity

Resolved Unity.Mathematics.math.asint(14f) = 1096810496

Discussion in 'Scripting' started by vildauget, Apr 11, 2021.

  1. vildauget

    vildauget

    Joined:
    Mar 10, 2014
    Posts:
    121
    I thought math.asint and math.asfloat were meant to be used to cast to int and float respectively, without getting garbage collection. Though, when debugging my code, I found that e.g. math.asint(14f) = 1096810496, using mathematics package 1.2.1.

    So what's the correct use case for these functions, and more importantly - if they're meant to cast, how to use them properly and safely?
     
  2. HellGate94

    HellGate94

    Joined:
    Sep 21, 2017
    Posts:
    132
    it just changes the type wihtout reinterpreting the value. so binary value of 14f as int type. since float is stored as sign, base, exponent you will get some very different int value this way
     
    vildauget likes this.
  3. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,537
    If the method you're talking about is from Unity.Mathematics, and is this one specifically:
    https://github.com/Unity-Technologi...lob/master/src/Unity.Mathematics/math.cs#L179

    Note it states:
    It returns the bit pattern. The binary representation of the float as an int. Like as @HellGate94 said, it's the sign, exponent, and mantissa formatted like so:

    (image from wikipedia: https://en.wikipedia.org/wiki/Single-precision_floating-point_format)

    If all you're doing is trying to convert a float to an int:
    Code (csharp):
    1. float a = 5f;
    2. int b = (int)a;
    Code (csharp):
    1. int a = 5;
    2. float b = (float)a; //you don't even need the cast here, int to float is implicit
    No garbage, no nothing... it's a simple cast.

    ...

    Not sure where you got the idea it'd cause "garbage collection"... unless you're referring to how BItConverter.GetBytes will create garbage if you try to get the bit pattern of a float using it:
    https://docs.microsoft.com/en-us/do...0#System_BitConverter_GetBytes_System_Single_

    Do you need the binary representation for something? Or did you think that casting using (int) and (float) causes garbage?
     
    Bunny83 and vildauget like this.
  4. vildauget

    vildauget

    Joined:
    Mar 10, 2014
    Posts:
    121
    Thank you for clearing that up, @HellGate94 and @lordofduct .

    Yes, indeed, when recently learning about garbage collection and boxing, I wrongly thought all casting would box, and thus cause garbage. So, normal (int)14f casting is the simple answer then. No need for bit patterns in my use case.
     
  5. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,998
    At least they wrote that. The usual thread takes several posts to establish they're on the wrong track: "I'm doing X and it isn't working", "try this", "still not working", "wait, X is obscure, why do you need it?", "Uh, I may have read it on a blog? Something about garbage collection? They said it can damage your computer."
     
    vildauget and lordofduct like this.