### Everything is fine until I got the result of cos x at 90

Everything is fine until I got the result of cos x at 90. Can anyone help me to find whats wrong with it? :'(

I got the result for cos(90) like this
6,08736e-0,05

 ``12345678910111213141516171819202122232425262728`` `````` #include #include #include using namespace std; int main() { cout << "***TABEL SINUS DAN COSINUS DALAM SATUAN DERAJAD\n"; using std::setw; cout << std::left; cout <
6.08736e-005 means 6.08736 × 10-5 which is close to zero.

You can get much closer by using a more accurate conversion from degrees to radians but you will probably still get some rounding error. This is something that you should expect when working with floating point numbers.

You might want to use std::fixed (to disable scientific notation) and std::setprecision (to limit the number of digits after the decimal point). That way the number will appear as zero even though there is slight rounding errors in your calculations.

 `` `` ``std::cout << std::fixed << std::setprecision(2);``

http://www.cplusplus.com/reference/ios/fixed/
http://www.cplusplus.com/reference/iomanip/setprecision/
Last edited on
> I got the result for cos(90) like this
> 6,08736e-0,05
the result is 6,08736e-05
that's pretty close to 0
¿what's the problem?
The value of 12 * pi needs to be more precise.

Before you define main() add this:

double my_pi(void); //declaration
double my_pi(void){return (4*atan2f(1,1));}; //definition of pi/4 * 4

Then in your main definition:
{

y=x/(12.0*my_pi());
float a =sin((y));
float b = cos((y));
...

}
I prefer floats to doubles.

I would keep the precision at 7 instead of using 7 and 15.

-I prefer floats to doubles.

I advise to prefer doubles to floats; floats are smaller but much more imprecise. floats are better in the uncommon situation where you have to transmit or store a very large # of them (large varies depending on your bandwidth/space/needs) AND the data is of acceptable precision in that format. For almost anything else, doubles are probably the right choice.
John56432 wrote:
The value of 12 * pi needs to be more precise.

The value of 12 * pi needs to be replaced by 180.0 / pi.
Just kinda summarizing advice from Peter87 , John56432, lastchance into working code. I agree with Peter87's findings that scientific should be removed -- with just setprecision, the scientific would still show values very close to zero but not zero for sin(180). Still one funny point here is sin(360) is giving me negative 0!
 ``1234567891011121314151617181920212223242526272829`` ``````#include #include #include using namespace std; double pi() { return std::atan(1)*4; } int main() { cout << "***TABEL SINUS DAN COSINUS DALAM SATUAN DERAJAD\n"; cout << left; cout << setw(7) <<"X" << setw(15) <<"Sin(x)"<< setw(15) << "Cos(x)"<<'\n'<

In action: https://repl.it/repls/DutifulPrestigiousBotany
Topic archived. No new replies allowed.