PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Floats and powers?

Im just making a simple pc dos program to calculate the volume of a sphere and for some reason it wont compile.

Code:
#include <stdio.h>

main(){
const float PI = 3.14159;
char chrRadius[20];
float fltRadius;
float fltVolume;

puts("");
puts("Please enter the radius of the sphere wich you wish to find the volume of:");
fgets(chrRadius, sizeof(chrRadius), stdin);
sscanf(chrRadius, "%f", &fltRadius;);
//Do the math
fltVolume = ((4/3)*PI*(fltRadius^3));
printf("The vloume of the sphere is %;f.\n", fltVolume);

return 0;
}


This line:
fltVolume = ((4/3)*PI*(fltRadius^3));
Gives this error:
'^' : illegal, left operand has type 'float'

Now is this telling me you cant raise decimal numbers to powers?
simonjacobs · 14 May 2002, 01:42 UTC
Erm, ^ is the bitwise XOR operator.

You need a function call to do float powers, or I guess a small loop to do int powers.

Check out math.h
simonjacobs · 14 May 2002, 01:43 UTC
Also: I would change the (4/3) bit to read (4.0f/3.0f) because otherwise it might be calculated as 1
wow, i feel like a dumbass. I just kind of assumed the carrot was the power of operator in c. Thanks for the info.