On Oct 1, 2008, at 12:29 AM, Michael Robinson wrote:

sin(90); returns 0 as well.  When I use Apple's Calculator and ask it
to tell me the result of sin(90), it gives me 1.

The C function sin() takes radians as its parameter. In order to use degrees you need to multiply by M_PI and divide by 180:

        int angle = 90;
        float result = sin(angle * M_PI / 180);

One other thing you have to remember is that when you are doing operations with integers your result will be an integer. Doing:

        float result = 90 / 180;

Will give you a value of 0. This is because a value of 0.5 will have the fractional part dropped to make it an integer. In order to retain the fractional part I need to make one of the values into a type that preserves the fractional part:

        float result = 90.0 / 180;

or

        float result = (float)90 / 180;

Adding a decimal point or using a cast makes one of the values into a type that preserves the fractional part. Now the result of any operations will retain the fraction. In my example the constant M_PI is already a fractional type so I didn't need to do any more work to make sure the fraction was retained.
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to