• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: [Math] sin(), cos() not working? What special magic must I use to summon their powers?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Math] sin(), cos() not working? What special magic must I use to summon their powers?


  • Subject: Re: [Math] sin(), cos() not working? What special magic must I use to summon their powers?
  • From: Graff <email@hidden>
  • Date: Tue, 30 Sep 2008 10:57:23 -0400

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 (email@hidden)

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:
This email sent to email@hidden


  • Prev by Date: Re: [Math] sin(), cos() not working? What special magic must I use to summon their powers?
  • Next by Date: Re: NSManagedObject subclass accessor pattern mystery?
  • Previous by thread: Re: To determine if the application is running from a network volume?
  • Next by thread: Property attributes
  • Index(es):
    • Date
    • Thread