One time, a very tired and exhausted friend and former student, needed to hurry up on a meeting and asked some support on the completion of a code. The code asked of him was simple, it was supposed to convert values ranging from 0.01 to 0.99 into its integral (whole number) form. Therefore, 0.01 becomes 1 and 0.99 becomes 100….oops just kidding 0.99 is 99. Simplistically this can be done by multiplying the said values to 100.
The question however dawned on me then, why then would anyone need to convert real numbers into their integral form?
To better analyze let us Assume that these three values are to be added :
value 1 : 0.01 Value 2 : 0.2 Value 3 : 0.03
Coding this in C, we will have something like :
———————————————
#include <stdio.h>
int main(void)
{
double val[3] = {1e-2,2e-1,3e-2};
double total_val = 0;
int cntr = 0;
for (cntr=0; cntr<3; cntr++) {
total_val += val[cntr];
}
printf(“total is : %e \n”,total_val);
printf(“total is : %f \n”,(float)total_val);
return 0;
}
Again let me warn you, my quest here is not to dissuade anybody from using real numbers. Or strictly “C-speaking”, dissuading the use of double or float data types. My goal is to learn something here and share what little I have learned. Since I was not from the very start a computer scientist nor systems architect, I take great stride in learning as much as I can to augment whatever learnings I have and become better in my profession.
Going back to the conversation at hand, what we don’t see in the above C-code is the logic of floating point manipulations. First off before I continue sharing, funny that after so many years in programming, it is only now that I took time to understand what floating points really are. I just treated them before like ordinary data types, just like integer and char. However, like a shocking horror movie, and much to my surprise, it was not “just” a data type.
Simplistically, floating points are real numbers. They are called as such because the decimal point (or radix point) can be found anywhere within the number. Thus, it is said that the radix point, “floats”. For example :
a. 1.23456 -> radix point is in between 1 and 2
b. 12.3456 -> radix point is in between 2 and 3
Again, why then would anyone need to convert real numbers into their integral form?
A. Learning 1 : Floating points are not innate to computing machines therefore, it has to find some extraneous way to preserve the data.
Computers do not operate on real numbers. Most operate on a binary system, the beloved “1″ and “0″. What it does however, in my very rough understanding of it, is that for computers to express -134.56 it has to save or represent it in an integral manner. For example :
a. -134.569 can be expressed as : -13456 x (10 exponent -2)
- According to the mathematical experts : A real number can be expressed as : s x b exponent e
- Where “s” is the signed number in this case “-13456″
- Where “b” is the base in this case “10″
- Where “e” is the exponent in this case “-2″
- Therefore, to save float, computers have to preserve s, b and e.
- And if a float in an architecture is a 32bit data, parts of the 32 bit will be set for “b” and “e”
Therefore in my coclusion, that by just declaring and initializing a double or a float data, i am led to believe that some form of procedural or operating cost is automatically incurred when the cpu tries to analyze your input and then save on a bit level the corresponding levels for “s”, “b”, and “e”.
B. Learning 2 : Floating point operations require more processing power from the PC or microcomputer.
Floating point arithmetic is very difficult for limited resourced computers like embedded systems. For example, if we add 0.01 and 0.2, a simplified flow on how the computer processes “0.01 + 0.2” is as follows :
1. Check if the exponents are the same for both operands. Represent both numbers 0.01 and 0.2 in their respective S x (B ^ e) form :
- 1 x 10^-2
- 2 x 10^-1
2. If not of same exponents, get the lowest exponent value
- 1 x 10^-2 -> lowest is -2
3. Operate / Shift so as to make both operands use the lowest exponent value.
- 2 x 10^-1 = 20 x 10^-2
4. Now that they have the same exponents, add the values.
- 20 x 10^-2 + 1 x 10^2
- (20 + 1) x 10^2
- 21 x 10^2
I dont think I have to expound any further. The above example speaks on how heavy it is to manipulate real numbers, or float / double data types. (Wait till you do floating point multiplication !!!
) As the number of floating point operations increase, the overhead acquired from real number usage is increased too.
Though I will no disscuss anymore, one more problem with floating points is its limitation / range problem. Well the “proving” part of this problem, I leave to those who read this blog.
However, though it is very difficult to compute floats, real numbers undeniably exist in the real world. The computer or processor has to comply with this. More so, there are so many benefits that come along speedy and accurate computations. And due to these benefits, some add FPUs (Floating Point Unit) into their computers. It also is now no wonder for me why today’s supercomputers are rated against Flops -> Floating point operations per second. TRIVIA : At current, as of this writing, IBM’s Roadrunner holds the record for being first to sustain 1 petaflops (1 quadrillion operations/second).
In closing, I finally realized that there was some sense to the instruction given to my friend. I may be wrong here about my conjecture, but I believe that the instruction came from an embedded engineer. I also remembered one of the advices, my previous embedded supervisor (in another company) gave me, “Avoid Floating.”
Again as I have said, I am not dissuading anybody from using floating points.
However, this learning has undoubtedly granted me the following realizations :
1. use floating points sparingly, when creating embedded applications
2. in optimizing and speeding up solutions, check out and minimize the number of float computations
3. should i need a real number, i would be inclined to use double
4. I will always ask for a double cheese burger in McDonalds (hahahahah
just kidding …)
I hope you enjoyed this really really geeky blog from me. Let us enjoy learning together.
Resources :
1. http://en.wikipedia.org/wiki/IEEE_754
2. http://bytes.com/forum/thread161561.html
3. http://pages.cs.wisc.edu/~smoler/x86text/lect.notes/arith.flpt.html
4. http://en.wikipedia.org/wiki/Flops
0 Responses to “When the procesor Flops (Floating Point Operations)”