vendredi 3 février 2017

What is the speed for the following methods to get the absolute value?

Task: check if x (double) is very close to zero using a threshold of let's say 1e-8. Which led me to two performance questions.

Methods I currently use:

1: The save method for when I'm unsure if the data is completely positive or not.

double threshold = 0.00000001;
if ( abs(x) < threshold );
 //remove x

2: If I'm pretty sure that the data is completely positive.

if ( x < threshold )
 //remove x

So that leads me to my first question, is the second method faster than the first method or not? I just always assumed the second method to be faster...

Another way I've been thinking about as an alternative to method 1 would be to use

if ( x < threshold && x > -threshold )
 //remove x

Which leads me to my second question, is this method faster or slower than method 1?

Aucun commentaire:

Enregistrer un commentaire