We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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
I doubt it's efficient, but this may serve for now: :-\"
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.
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."
Any votes as to which of the three methods executes fastest?
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.
You might try
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.
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 :/