Why output is giving 3 , expecting -3. How to handle such preprocessing in c?
#include<stdio.h>
#include<math.h>
#define sq(x) ((x<0)?sqrt(-x):sqrt(x))
int main()
{
int x;
x=sq(-9);
printf("%d\n",x);
return 0;
}
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.
because your # define “sq” checks if its a negative number and turns it into a positive number before calculating a square root
its doing sqrt(-x) which is sqrt(-(-9)) ( taking the negative of a negative is the positive)
so its doing sqrt(9)