I am reading a C book. In the Arithmetic Expression section, they say:
"Division typically uses more resources. To avoid division, we multiply rather than divide. For example, we multiply by 0.5 rather than divide by 2.0."
Why Division typically uses more resources?. Can anyone give me a detail explanation, please?
Thanks a lot.
Binary Multiplication is simple using the peasant algorithm - you basically shift, then sum: http://en.wikipedia.org/wiki/Multiplication_algorithm#Peasant_or_binary_multiplication
Binary Division is much harder, as it's a sequence of subtractions (like long division you may have done in school). The main algorithm class is called 'radix', which you can see an example of here: http://www.bearcave.com/software/divide.htm
Remember though - measure first, then optimise. It's much easier to maintain code that matches the problem domain than code that is already optimised.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With