[1+1=2]
OneAndOneIs2

Tue, May 06, 2008

[Link][Icon]Ehhh..?

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??

6 comments • Categories: Omni, Programming

Comments:

Comment from: George Styles [Visitor] Email
I reckon its units of angle. Its probably expecting radians, whereas calculators normally work in degrees... you have to multiply by 2pi or something (maybe divide?) before feeding it into the math.h functions... g
PermalinkPermalink 06/05/08 @ 20:07
Comment from: Åke [Visitor] Email · http://akeiexil.wordpress.com
If memory serves me it's:

360 degrees = 2*pi radians => 1 degree = ((2*pi)/360) radians (or pi/180)

I think...
PermalinkPermalink 06/05/08 @ 20:37
Comment from: Al [Visitor] Email · http://eggriedrice.com
Damn, I thought I'd be able to get in first with my world beating grade B Higher Maths knowledge!

You certainly have radians there, try putting your calculator into radian mode (look for a mode button) and you'll get the same answers as your code.

According to mathematicians, radians are pretty awesome because they're easier to use or something. I'm yet to be completely convinced but they do seem to make more sense than 360 entirely arbitrary degrees...
PermalinkPermalink 07/05/08 @ 00:10
Comment from: oneandoneis2 [Member] · http://geekblog.oneandoneis2.org/
Ahhh... THAT's what it's doing!

Thanks all! This was really bugging me. As Al says, putting the calculator into rad mode first gets me the same answers as the calculator. Or multiplying the number by 57.2958 first - from Wikipedia:

The radian is a unit of plane angle, equal to 180/π degrees, or about 57.2958 degrees.


Much obliged!
PermalinkPermalink 07/05/08 @ 11:09
Comment from: Christophe [Visitor] Email · http://christophoronomicon.blogspot.com/
Radians make sense because they make calculations easier. What's the length of an arc of radius R and angle theta? With degrees you need a complicated formula. With radians it's simply R*theta (which is why 360 degrees equals 2*Pi radians: 2*Pi*R is the perimeter of a circle).

Moreover, radians make sense in other places in maths and physics, where formulas are simpler if angles are in radians. Then there are areas where sines and cosines appear naturally without there being an angle involved. In those cases radian is the only unit that makes sense. In calculations involving complex numbers for instance (using the imaginary root i), you often end up writing things in terms of sines and cosines, and the only natural unit to use is the radian. Anything else will just complicate things.
PermalinkPermalink 07/05/08 @ 12:29
Comment from: Anceps [Visitor] Email · http://anceps.canalblog.com
If you have to define precisely and mathematicaly what an angle is, or how to tell what the angle is between 2 lines, you could have a hard time trying. But if you choose any length to be your unit, just draw a unit-circle centered where the segments cross: your angle is the length on the circle between the segments. Now, that's just simple.

But there's more! Try to derivate the function sin(x), you simply get cos(x). That is, if x is in radians.
(For x in degrees, sin(x)' = cos(x)*Pi/180.)

Same in physic (or in what some would call "applied maths", too):
For a small angle x, you can approximate sin(x) = x... but only if x is in radians.
PermalinkPermalink 07/05/08 @ 16:29

Leave a comment:

Your email address will not be displayed on this site.
Your URL will be displayed.

Allowed XHTML tags: <p, ul, ol, li, dl, dt, dd, address, blockquote, ins, del, span, bdo, br, em, strong, dfn, code, samp, kdb, var, cite, abbr, acronym, q, sub, sup, tt, i, b, big, small>
(Line breaks become <br />)
(Set cookies for name, email and url)
(Allow users to contact you through a message form (your email will NOT be displayed.))

Categories

May 2008
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  

Search

Misc

XML Feeds

What is this?
eXTReMe Tracker

Valid XHTML 1.0 Transitional

Valid CSS!

[Valid RSS feed]

powered by
b2evolution

blank