Re: mathematical functions?
Re: mathematical functions?
- Subject: Re: mathematical functions?
- From: Greg Titus <email@hidden>
- Date: Mon, 14 May 2001 19:37:55 -0700
On Monday, May 14, 2001, at 07:25 PM, Funkaster wrote:
>
How can I access mathematical functions from cocoa?
>
what I've done so far (dunno if it's good):
>
for (int i=0; i < total; i++) {
>
theta = [NSDecimal decimalNumberWithDecimal:(ang * 3,14159 /
>
180.0) * i/ total];
>
NSDecimalPower(r, theta, int power, NSRoundUp);
>
radio = [NSDecimal decimalNumberBySubstracting:[NSDecimal
>
decimalNumberWithDecimal:1.0]];
>
}
The advantage of using NSDecimal like you are doing above, is that
NSDecimal is a high precision arithmetic package (it internally stores
numbers in decimal instead of binary, so you get more precision and
don't have to worry about binary rounding). But, the exact analog of
your Java code:
>
for (int i=0; i < total; i++) {
>
double theta = (ang * Math.PI/180.0)*i/total;
>
double r = Math.pow(base,theta) - d;
>
double x = (int)r * Math.cos(theta);
>
double y = (int)r * Math.sin(theta);
>
}
Would look extremely similar:
for (i = 0; i < total; i++) {
double theta = (ang * 3.14159 / 180.0) * i / total;
double r = pow(base, theta) - d;
double x = (int)r * cos(theta);
double y = (int)r * sin(theta);
}
All (I think?) of the math functions in Java's Math class are built from
C functions that are built in to OS X. There aren't constants though, so
you will have to use your own value for Pi.
Hope this helps,
--Greg