Converting floating point maths to int maths
Floating point math is also much slower than integer math in performing calculations, so should be avoided if, for example, a loop has to run at top speed for a critical timing function. Programmers often go to some lengths to convert floating point calculations to integer math to increase speed.
How would one generaly go about doing this?
For instance is there any way to make a function like pow(x , 2.2) faster?
How much slower is it than pow(x , 2) ?
You can leave an optional "tip" with Mahalo's virtual currency, Mahalo Dollars. If you are asking a difficult question that might require some research, or if you'd like a wide variety of feedback, a higher tip often leads to more answers to your question.
M$3 Answers
For example, when dealing with money, you can do a lot with fixed-precision rather than floating point math. If you make your fundamental unit the cent rather than the dollar, everything is really integer arithmetic. You may need to drop into floating point when doing some calculations (compound interest, for example), and you need to be very judicious about when you convert back to fixed point. But you can save yourself a lot of cycles.
Or an example that I do, cost calculations in database optimizations. There, the floating point precision simply isn't necessary, though the numbers grow larger than integers can handle. The numbers are all very approximate anyway, so you don't really need to compute 24 bits of precision. You can cut off the precision and handle it with integer arithmetic, though you need to make separate allowances for the exponents. It's a bit like trying to work with a floating point number that has only a smallish mantissa (say, 8 bits). The real work is done on the exponent, but that's already integer math.
Another example is in graphics, which uses a lot of trigonometry. Since the ultimate output of the graphical calculation is integer (screen coordinates) you can use a number of design tricks to do as much of each calculation in integer as you can. But ultimately, for precision, they put a LOT of work into making graphics chips able to do vast amounts of highly parallel floating-point operations. (So much so that they're re-using those chips for other operations, like weather modeling, where it's even harder to do integer work.)
They are already doing a fair bit of work on the chips, in fact. The chip's division algorithm is very different from what you do by hand, like SRT division:
http://www.articlesbase.com/information-technology-articles/srt-division-algorithm-1830007.html
which is actually a TRInary computation. It's very fast, but still slower than integer arithmetic.
For at least some applications, it's no longer necessary to avoid floating point operations; the computers have sped up to the point where you can just let the computer do the work, and put more energy into the coding. You certainly want to avoid premature optimization, since it takes longer and often provides no benefit. But for some applications, you need specialists in the domain and in numerical methods to see which techniques will suit the problem.
You can leave an optional "tip" with Mahalo's virtual currency, Mahalo Dollars. If you are asking a difficult question that might require some research, or if you'd like a wide variety of feedback, a higher tip often leads to more answers to your question.
M$You can leave an optional "tip" with Mahalo's virtual currency, Mahalo Dollars. If you are asking a difficult question that might require some research, or if you'd like a wide variety of feedback, a higher tip often leads to more answers to your question.
M$Enjoy,
Ray
You can leave an optional "tip" with Mahalo's virtual currency, Mahalo Dollars. If you are asking a difficult question that might require some research, or if you'd like a wide variety of feedback, a higher tip often leads to more answers to your question.
M$