Re: [Math] sin(), cos() not working? What special magic must I use to summon their powers?
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: Jason Coco <email@hidden>
- Date: Tue, 30 Sep 2008 07:42:44 -0400
On Sep 30, 2008, at 07:29 , Michael Robinson wrote:
Hello,
I am trying to convert the following from Javascript to Cocoa ObjC:
var retVal = Math.round(cornerSize*(1-Math.cos(Math.asin(i/
cornerSize))));
return retVal;
Now retVal _must_ be an int, because the value is used as a margin
attribute ("rounding" corners of divs).
This is what I've got:
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?
This is happening because the function is returning a value less than
zero and your integer is getting truncated. You want to do something
like this:
double retVal;
retVal = cornerSize*(1-cos(asin(i/cornerSize)));
return lround(retVal);
(this function should return a long integer--see the man page for
lround for other options)
Jason
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
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