I've been assigned to write a program that takes an int or a float in degrees as input and returns the sin
and cos
of that value with 6 decimal places. But when the input is 90 and 180 I get weird negative zeros, instead of "positive" zeros.
Code:
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
const double PI = 3.14159265;
int main() {
float x;
cin >> x;
x = x * PI / 180;
cout << fixed;
cout << setprecision(6) << sin(x);
cout << ' ' << setprecision(6) << cos(x) << endl;
}
Undesired Input and output:
In:90 Out:1.000000 -0.000000
In:180 Out:-0.000000 -1.000000
I've tried to fix this by multiplying the result of the cos function by -1 when the input is 90 but it still outputs -0.000000.
Solution: As @harold and @user2618142 pointed out, the result of the calculation was negative, so the approximation returned a negative 0.