Most Efficient Conversion of Integer Value to Floating Point Value

Note this is not a question about casting.

I am curious as to the most efficient way to take an integer value and convert it to a floating point so that the whole numbers become fractional numbers.

To illustrate, I want to convert any number from 99999 to 0.99999. So if the value is 7 I want the output to be 0.7. If the value is 8412223, I want the output to be 0.8412223

Is there some "magic" bit-oriented operation that can achieve this or will it take a brute-force method (for example determine the number of digits in the value and then divide by an equivalent power of 10).

Any insights will be greatly appreciated. Thanks

Answers

  • Answer ✓

    I doubt it's efficient, but this may serve for now: :-\"

    // forum.Processing.org/two/discussion/14169/
    // most-efficient-conversion-of-integer-value-to-floating-point-value
    
    // GoToLoop (2015-Dec-28)
    
    void setup() {
      println(becomeDecimal(99999));
      println(becomeDecimal(7));
      println(becomeDecimal(8412223));
      exit();
    }
    
    static final float becomeDecimal(int n) {
      return float("." + n);
    }
    
  • Ah, so the gist of what you are suggesting is to create a function that converts the number to a string and adds as a prefix to the string a decimal point and then converts that to a float.

    That is brilliantly simple. Thank you.

  • Again I don't know about efficiency but for positive integers with up to 7 digits (and some with 8 digits) this is a fully numeric way to do it.

    int n = 12345678;
    float nf = n / pow(10, 1 + (int)(Math.log10(n)));
    println(nf);
    
  • edited December 2015

    Thanks for this alternative method Quark. This is the sort of equation I was seeking to avoid though as you've got division, pow() and log10 all. What I originally came up with was the following, which left me asking "surely there is a better way."

     int n = 12345678;
     String ns = String.valueOf(n);
     float nf =  n / pow(10, ns.length());
     println(nf);  // just to prove it works
    

    Any votes as to which of the three methods executes fastest?

  • edited December 2015

    You also have pow and division the only difference is the logarithm, but then you have String to float conversion which is not a simple cast.

    If I had to choose I would go for the pure numerical solution since modern CPUs have built in math processors and the String to float is likely to be more time consuming.

    I believe the difference between the three methods to be totally unimportant unless you want to do this zillions of times which is most unlikely.

  • Hi Quark, You wrote You also have pow and division which is correct and which is why I came here in search of an alternative.

    The difference computationally may be trivial but old habits die hard and I'm regularly looking for short cuts to make code run faster. And of course the slower your computer is, the bigger the impact there will be from any performance improvements you can squeeze out of your code.

  • edited December 2015

    You might try

    float f = (float) n;
    while (f > 1) f /= 10;
    

    I am on my tablet so I have not tested this but I don't see why it shouldn't work. :)

    Both the Java compiler and JVM will optomise your code so any savings at this micro level will be measured in nanoseconds and hardly worth the effort. Real savings come at a higher level, for instance selecting / creating the best data structures and algorithms. A simple mistake at this level will swamp any savings at the statement level.

  • Hi Quark, You are certainly correct when you say Real savings come at a higher level, for instance selecting / creating the best data structures and algorithms. Programmers are faced with a variety of challenges/trade-offs on this front. Memory/storage vs cpu optimization for example. When I can spare the memory, I'll use LUTs to speed up execution (no reason to recompute the same values over and over and over and...) And if you are interacting with a sketch in real time, small savings here and there accumulate. Individually irrelevant, but collectively relevant.

    At the risk of dating myself, I started programming in an era where a program's execution was costed out based on cpu cycles and I/O requests so otherwise trivial optimizations would, over the course of a year, add up to real savings from an accounting perspective.

    On a related note, these days I experience more and more browser hangs as social media platforms and web sites become ever more top-heavy with all the browser-executed code it take to implement their features. I doubt that the developers devote any effort to optimizing their code as the benefits to them are minimal, if not non-existent.

    Thanks for your insights Quark.

  • On a related note, these days I experience more and more browser hangs as social media platforms and web sites become ever more top-heavy with all the browser-executed code it take to implement their features. I doubt that the developers devote any effort to optimizing their code as the benefits to them are minimal, if not non-existent.

    A little knowledge is a dangerous thing: JS libraries like jQuery have made 'web development' almost too accessible, with web designers relying heavily on loading in multiple libraries to implement functionality that can often be achieved fairly easily in raw JS. It's often the loading/parsing of the libraries that leads to browser hangs on load :/

Sign In or Register to comment.