• 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: Graham Cox <email@hidden>
  • Date: Tue, 30 Sep 2008 21:44:44 +1000


On 30 Sep 2008, at 9:29 pm, Michael Robinson wrote:

Variables i, cornerSize are passed to the function from a for loop. i being the counter for the loop, cornerSize being the size of corner desired by the user.

	float retVal;

	retVal = cornerSize*(1-cos(asin(i/cornerSize)));

	return (int)retVal;

Obviously, I'm no whiz at ObjC, and the fact that this keeps return '0' has got me stumped.

At first I thought it was some stupid mistake I made translating the formula, but I tested a few things and now I'm not so sure.

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.

What what whaaaaat?


I can't comment on the correctness of the formulas, and I'm not very familiar with Javascript.

But I suspect your problem is that in C and Obj-C (and pretty much every language AFAIK), the argument you pass to cos, sin, etc. is in radians, not degrees.

So you can't pass sin(90) and get 1, because 90 radians is ~0.89, truncate that to an int and you have 0.

You need to convert everything to radians before using these functions. These macros may be handy:

#define	DEGREES_TO_RADIANS( d )			((d) * 0.0174532925199432958)
#define RADIANS_TO_DEGREES( r )			((r) * 57.29577951308232)

hth,

Graham


(FWIW, radians are nice, lovely, easy to use units based on some solid maths. Degrees on the other hand are a legacy of the ancient Egyptians believing that a year was 360 days and inventing an arbitrary system around it. You only think you're comfortable in degrees because schools drum it into you from an early age. But radians are much, much nicer when you come to compute physics and geometry problems. Just my 2ยข worth!).


_______________________________________________

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


References: 
 >[Math] sin(), cos() not working? What special magic must I use to summon their powers? (From: Michael Robinson <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: [Math] sin(), cos() not working? What special magic must I use to summon their powers?
  • Previous by thread: Re: [Math] sin(), cos() not working? What special magic must I use to summon their powers?
  • Next by thread: Re: [Math] sin(), cos() not working? What special magic must I use to summon their powers?
  • Index(es):
    • Date
    • Thread