topic
Problem description
Given a real number x, output the value of sign(x).
sign(x) is a sign function. If x>0, it returns 1; if x=0, it returns 0; if x<0, it returns -1.
Input format
one real number x per line.
Output format
an integer on a line represents the answer.
Sample input-
0.0001
Sample output
-1
Data scale and convention
|x|<=10000, the input data accuracy can reach 4 decimal places at most.
Tips: When
judging whether the real number x is equal to zero, due to the computer real number calculation error, a very small amount of eps should be introduced. The core code is as follows:
where fabs is the absolute value function in cmath.
const double eps=1e-6;
if (fabs(x) <= eps) { //x is zero }
Code
#include <stdio.h>
int main()
{
double x;
int y;
scanf("%lf",&x);
if(x>0)
y=1;
else if(x<0)
y=-1;
else
y=0;
printf("%d",y);
return 0;
}