Search Unity

Common Factor to convert from float array to integer array and getting back original float array

Discussion in 'Scripting' started by sara_unityads, Feb 13, 2018.

  1. sara_unityads

    sara_unityads

    Joined:
    Feb 7, 2015
    Posts:
    26
    Hi,
    For a specific reason, i want to convert the float array to integer array with a common conversion factor, and after usage - again i want to retrieve the original float array from the integer array with the help of that common conversion factor. How can i do this? I can store the original float array into another float array - but it will consume lot of time to compare and perform. Can anyone tell me an alternative and speedy way to achieve this?

    Thanks,
    Saravanan.M
     
  2. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    899
    Why not just create an int array of the desired length and then copy each element from the float array to the int array (using the conversion factor). Then you will have the new int array as well as the original float array. If the arrays are always of the same length, you wouldn't need to recreate the int array all the time.
     
  3. sara_unityads

    sara_unityads

    Joined:
    Feb 7, 2015
    Posts:
    26
    That is the question - what could be the common conversion factor(value) - for random number floating array.
     
  4. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    899
    Sorry, I thought you had a specific conversion factor in mind, that is that you wanted to scale all numbers by a factor. If you just want to convert them to int, wouldn't it work if you cast them with intArray = (int)floatArray?
     
  5. sara_unityads

    sara_unityads

    Joined:
    Feb 7, 2015
    Posts:
    26
    It will. But what i ask is I need a generic conversion factor to change random float values to integer and back those floating values, for example i googled and got this, but it needs a little tweak:

    long to_long(double v)
    {
    // representable range
    double min_value = long.MinValue / scale;
    double max_value = long.MaxValue / scale;

    if(v < 0)
    {
    if(v < min_value)
    return 0;
    return (long)(v * scale - 0.5);
    }
    else
    {
    if(v > max_value)
    return 0;
    return (long)(v * scale + 0.5);
    }
    }

    But, i cant get it. But i think it can give int and float - back and forth through this conversion. can you tweak this?
     
  6. sara_unityads

    sara_unityads

    Joined:
    Feb 7, 2015
    Posts:
    26

    No Problem. Problem Solved. Its that simple. Used a scale value to multiply and floor it to int and divide the same scale value to get back float. Thanks for your Replies.
     
  7. sara_unityads

    sara_unityads

    Joined:
    Feb 7, 2015
    Posts:
    26
    OMG... How could i forgot this?. Multiply and divide by a same value with something will give the same result back and forth. Oh... My memory power?... This problem is a general issue.
     
  8. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    3,356
    To make it convenient you can just put something like this into an extension class.

    Code (csharp):
    1.  
    2. public static float ToFloat(this int i)
    3.         {
    4.             return i / 100f;
    5.         }
    6.  
    7.         public static int ToInt(this float i)
    8.         {
    9.             return (int)(Math.Round(i * 100, 2));
    10.         }
    11.  
     
  9. sara_unityads

    sara_unityads

    Joined:
    Feb 7, 2015
    Posts:
    26
    yeah... i did it like that...