|
Tue, May 06, 2008
![[Link]](http://geekblog.oneandoneis2.org/img/chain_link.gif)
K&R 4.5: Add things like sin and pow to the calculator
Okay. So you just put in the math.h library and off you go. Right?
Apparently.. wrong.
It SAYS it works, but it gives me the wrong answers for the sin/cos/tan stuff.
1 s
0.84147098
2 s
0.90929743
3 s
0.14112001
Must have coded it wrong.. right?
So let's try something a bit simpler. A bit of code to calculate sin(0-10):
#include <stdio.h>
#include <math.h>
int main()
{
int i;
for (i = 0; i < 11; i++)
printf("sin %d = %f\n", i, sin(i));
return 0;
}
Result?
sin 0 = 0.000000 sin 1 = 0.841471 sin 2 = 0.909297 sin 3 = 0.141120 sin 4 = -0.756802 sin 5 = -0.958924 sin 6 = -0.279415 sin 7 = 0.656987 sin 8 = 0.989358 sin 9 = 0.412118 sin 10 = -0.544021
Same numbers that my calculator code gives me.
But not the same numbers that any other scientific calculator I try gives me.
What's going on??
The radian is a unit of plane angle, equal to 180/π degrees, or about 57.2958 degrees.
| Mon | Tue | Wed | Thu | Fri | Sat | Sun |
|---|---|---|---|---|---|---|
| << < | > >> | |||||
| 1 | 2 | 3 | 4 | |||
| 5 | 6 | 7 | 8 | 9 | 10 | 11 |
| 12 | 13 | 14 | 15 | 16 | 17 | 18 |
| 19 | 20 | 21 | 22 | 23 | 24 | 25 |
| 26 | 27 | 28 | 29 | 30 | 31 | |