Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jan 30, 2009, 09:56 AM   #1
veshman
macrumors newbie
 
Join Date: Sep 2005
Kochan Exercise 4-4

Hi, I'm very new to programming and was wondering if I could get some help on this problem. I'm working through Kochan's 2.0 book, and am getting an error on problem 4-4, as shown in the image.


If I make x an integer, the program seems to run fine. But if I make it a float, it errors with "invalid operands to binary ^"
Attached Thumbnails
Click image for larger version

Name:	Picture 1.png
Views:	8
Size:	14.2 KB
ID:	155330   Click image for larger version

Name:	Picture 2.png
Views:	7
Size:	13.3 KB
ID:	155331  
veshman is offline   0 Reply With Quote
Old Jan 30, 2009, 10:04 AM   #2
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: Dallas, TX
^ is not the exponentiation operator, it is the bitwise XOR operator. There is, in fact, no exponentiation operator. There is a pow function in math.h for dealing with doubles. For integers, for powers of 2, you can use << to shift left. For other integers... you have to do it yourself.

-Lee

EDIT: For simple, fixed powers, just multiply the appropriate number of times.
lee1210 is offline   0 Reply With Quote
Old Jan 30, 2009, 10:08 AM   #3
lazydog
macrumors 6502a
 
Join Date: Sep 2005
Location: Cramlington, UK
Send a message via MSN to lazydog
... or since you're using floats, powf()!

b e n
lazydog is offline   0 Reply With Quote
Old Jan 30, 2009, 11:04 AM   #4
mdeh
macrumors 6502
 
Join Date: Jan 2009
Quote:
Originally Posted by veshman View Post
Hi, I'm very new to programming and was wondering if I could get some help on this problem. I'm working through Kochan's 2.0 book, and am getting an error on problem 4-4, as shown in the image.


If I make x an integer, the program seems to run fine. But if I make it a float, it errors with "invalid operands to binary ^"
As Lee said below, just use simple substitution. You have not yet used Pow or shift ( << ) operators.

So,

Code:
#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
	/* 3x - 2x + 6  for x = 2.55 */
	
	float x = 2.55;
	
	NSLog(@"Given x = 2.55\n");
    NSLog(@" 3x - 2x + 6 = %f:", 3 * (x * x * x) - 2 *( x * x) + 6);
    [pool drain];
    return 0;
}
mdeh is offline   0 Reply With Quote
Old Jan 30, 2009, 10:21 PM   #5
veshman
Thread Starter
macrumors newbie
 
Join Date: Sep 2005
Thanks much!

Thanks a lot for your responses!

That makes sense.....I thought I was missing something big. And as you pointed out, I'm thus far unfamiliar with powf and bit operations (read about them, but haven't used them).

Bhavesh
veshman is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
Downgrading from 4.3.1 to 4.1/4.2 seble iPhone 0 Apr 14, 2011 01:55 AM
Restore backup from iPhone 4 4.1 01.59.00 unlocked to iPhone 4 4.0.2 01.59.00 unlocke carreragt7 iPhone Tips, Help and Troubleshooting 1 Jan 19, 2011 09:23 PM
Unlocked IPhone 4 on 4.0.1 to 4.1? cubiewong Jailbreaks and iOS Hacks 8 Nov 9, 2010 09:48 PM


All times are GMT -5. The time now is 09:45 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC