I saw the function below that should return sign of double d. But I couldn’t understand how it works?
int sgn(double d){
return d<-eps?-1:d>eps;
}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
That means:
dis less than-eps, the result is “negative”dis more thaneps, the result is “positive” (d>epsreturns 1)epswould normally be a small number, so we consider numbers between, say -1e-5 and 1e-5 as “practically zero”. This approach is used to water down some deficiencies of the computer’s floating-point numbers, like thatsin(pi)!=0. However, it comes at the cost of introducing an arbitrary numberepsin the calculation, and losing the property that eg. ifaandbare positive numbers,a*bis positive provided underflow doesn’t occur.