Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 03-19-2011, 05:55 PM   #1 (permalink)
Registered Member
 
Join Date: Apr 2009
Posts: 118
lexy0202 is on a distinguished road
Question HELP: Exponent function in Objective-C

Hi there,

Could someone tell me what I'm doing wrong please.

I'm writing a simple app which will work out a sequence form any formula you enter.

The code for working out a quadratic sequence goes like this:

Code:
int i;
        for (i=1; i<=12; i++) {
            sequenceValues[i-1] = (nNum*(i*i))+cNum;
        }
That works fine, but in this app the user inputs the exponent for n^whatever.

That works for the case of n2, but what if the user wanted to calculate n3 or n4? Sure I could use switch statements, but that's not very efficient, and also wouldn't calculate n1.5... so I though how about this:

Code:
int i;
        for (i=1; i<=12; i++) {
            sequenceValues[i-1] = (nNum*(i^userExponentValue))+cNum;
        }
Turns out, if I use i^variable, I get really random results, for example:

n3 with that code =
2, 1, 0, 7, 6, 5, 4, 11

when really it should be (using i*i*i) =
1, 8, 27, 64, etc...

So, my simple question is what is the correct usage of ^ in objective-c... or is there another way to do it

Thanks for help in advance,
Alex
lexy0202 is offline   Reply With Quote
Old 03-19-2011, 06:15 PM   #2 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

The carat ^ is the bitwise OR operator, not exponent. Try the pow() function.

pow - C++ Reference
__________________

Free Games!
smasher is offline   Reply With Quote
Old 03-19-2011, 06:18 PM   #3 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by lexy0202 View Post
Hi there,

Could someone tell me what I'm doing wrong please.

I'm writing a simple app which will work out a sequence form any formula you enter.

The code for working out a quadratic sequence goes like this:

Code:
int i;
        for (i=1; i<=12; i++) {
            sequenceValues[i-1] = (nNum*(i*i))+cNum;
        }
That works fine, but in this app the user inputs the exponent for n^whatever.

That works for the case of n2, but what if the user wanted to calculate n3 or n4? Sure I could use switch statements, but that's not very efficient, and also wouldn't calculate n1.5... so I though how about this:

Code:
int i;
        for (i=1; i<=12; i++) {
            sequenceValues[i-1] = (nNum*(i^userExponentValue))+cNum;
        }
Turns out, if I use i^variable, I get really random results, for example:

n3 with that code =
2, 1, 0, 7, 6, 5, 4, 11

when really it should be (using i*i*i) =
1, 8, 27, 64, etc...

So, my simple question is what is the correct usage of ^ in objective-c... or is there another way to do it

Thanks for help in advance,
Alex
You want the function pow(x,y).

It raises x to the y power, where x and y are both floating point numbers.

Note that it is MUCH slower than repeated multiplication, so if you are doing thousands or millions of calculations, it is faster to special-case integer powers and use repeated multiplication instead. I would use a for loop rater than a a case statement however, to make the code cleaner.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 03-19-2011, 06:24 PM   #4 (permalink)
Registered Member
 
Join Date: Apr 2009
Posts: 118
lexy0202 is on a distinguished road
Default

Thanks very much guys!

That works perfectly,
Alex
lexy0202 is offline   Reply With Quote
Reply

Bookmarks

Tags
formulas, maths, noob, objective-c, simple

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 355
11 members and 344 guests
7twenty7, condor304, Creativ, Domele, dreamdash3, laureix68, LEARN2MAKE, michelle, mistergreen2011, Sami Gh, tinamm64
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,661
Threads: 94,119
Posts: 402,896
Top Poster: BrianSlick (7,990)
Welcome to our newest member, tinamm64
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 01:30 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0