62.5k views
12 votes
What is the algorithm to determine the absolute of a number

User FisNaN
by
3.1k points

2 Answers

9 votes

Answer:

Conditionals are slower than plain arithmetic operations, but much, much faster than something as silly as calculating the square root

Step-by-step explanation:

integer or bitwise op: 1 cycle floating -point add/ sub/mul: 4 cycles . Floating- point div ~ 30 cycles Floating - point exponentiation :~ 60 cycles depending on implementation Conditional branch : avg. 10_ cycles, better if well- predicted , much worse if mispredicted.

User Rgaut
by
3.4k points
8 votes

This was written in python, let me know if it needs to be in another language.

What is the algorithm to determine the absolute of a number-example-1
User Riyaz Ahamed
by
4.0k points