Amazon.com Widgets Mathmatical "to the power of" in Objective-C ?
Welcome, Guest. Please login or register.
Did you miss your activation email?
October 02, 2014, 03:26:13 AM
Home Help Search chat Login Register 
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+ Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+ Old Stuff
| |-+ Chapter Study
| | |-+ Chapter 4 - Data Types and Expressions
| | | |-+ Mathmatical "to the power of" in Objective-C ?
Pages: [1] Go Down
Print
Author Topic: Mathmatical "to the power of" in Objective-C ? (Read 43618 times)
Nathan_e
Newbie
*
Posts: 4






on: April 25, 2009, 08:04:30 AM

Is there a power function in Objective-C ?
In MS Excel I could enter five squared as "5^2", or seven cubed as "7^3", etc. is there something similar in Objective-C that would enable me to define the power as a variable, i.e. 7^n ?

Thanks,
Nathan.
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #1 on: April 25, 2009, 08:49:33 AM

There is no operator to do this in Objective-C.  The Standard C Library has an exp (x, y) that raises x to the y power, but it works all in doubles.

Cheers,

Steve Kochan
Logged
steve1978
Newbie
*
Posts: 7


Email




Reply #2 on: May 24, 2009, 11:22:54 AM

I tried using the exp(x,y) that you discuss here.  It seems to be looking for only one argument here, like exp(x) being the representation for e^x.

The function pow(x,y) works like you say to raise x^y.  Is that the correct function?

Thanks,

Steve
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #3 on: May 24, 2009, 12:51:24 PM

Yeah, that's it.  Thanks for pointing that out!

Cheers,


Steve Kochan
Logged
akamaized
Newbie
*
Posts: 21






Reply #4 on: August 14, 2009, 06:29:46 PM

Hi - so if pow or exp work only in doubles, does that mean we cast it back to int?

If we feed it ints, does the function cast to double internally and return a double even if the result is basically an integer?

such as 2^8 being a positive int.

Huh
Logged
rgronlie
Global Moderator
Full Member
*****
Posts: 212







Reply #5 on: August 15, 2009, 01:29:02 AM

The compiler will cast the ints into doubles for you. But it wouldn't be a bad practice to explicitly cast them yourself.

Code: (Objective-C)
resultInt = (int)pow((double)someInt, (double)someOtherInt);

Be aware however, that if the result of the pow function is greater than the maximum value an int can hold, the value of the int won't be what you expected.

Code: (Objective-C)
#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
  NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

  NSLog(@"(double)pow(2, 32)                    = %lf", (double)pow(2, 32));
  NSLog(@"(short int)pow(2, 32)                 = %hi", (short int)pow(2, 32));
  NSLog(@"(unsigned short int)pow(2, 32)        = %hu", (unsigned short int)pow(2, 32));
  NSLog(@"(int)pow(2, 32)                       = %i" , (int)pow(2, 32));
  NSLog(@"(unsigned int)pow(2, 32)              = %u" , (unsigned int)pow(2, 32));
  NSLog(@"(long long int)pow(2, 32)             = %qi", (long long int)pow(2, 32));
  NSLog(@"(unsigned long long int)pow(2, 32)    = %qu", (unsigned long long int)pow(2, 32));

  [pool drain];
  return 0;
}
Logged

Sanity: Minds are like parachutes. Just because you've lost yours doesn't mean you can borrow mine.
Pages: [1] Go Up
Print
Jump to:



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.