Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

veshman

macrumors newbie
Original poster
Sep 21, 2005
10
0
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 ^"
 

Attachments

  • Picture 1.png
    Picture 1.png
    14.2 KB · Views: 84
  • Picture 2.png
    Picture 2.png
    13.3 KB · Views: 67

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
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.
 

mdeh

macrumors 6502
Jan 3, 2009
345
2
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;
}
 

veshman

macrumors newbie
Original poster
Sep 21, 2005
10
0
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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.