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

abcdefg12345

macrumors 6502
Original poster
Jul 10, 2013
280
86
Im trying to calculate volume of sphere in Xcode where i enter the radius and it gives me the volumes

volume of sphere formula = (4/3)#r³

xcode volume of sphere formula I'm using

Code:
- (IBAction)Sphere:(id)sender
{
    float result = (4/3) * 3.141592653589793 * ([_Sphereradius floatValue] * [_Sphereradius floatValue] * [_Sphereradius floatValue]);
    
    [_SphereVolume setFloatValue:result];
}

i keep getting wrong value for the volume

if i for example enter 5 to radius field I'm getting 392.699096679688 when i should be getting 523.5987756

what am i doing wrong
 

Phil A.

Moderator emeritus
Apr 2, 2006
5,745
2,954
Shropshire, UK
Im trying to calculate volume of sphere in Xcode where i enter the radius and it gives me the volumes

volume of sphere formula = (4/3)#r³

xcode volume of sphere formula I'm using

Code:
- (IBAction)Sphere:(id)sender
{
    float result = (4/3) * 3.141592653589793 * ([_Sphereradius floatValue] * [_Sphereradius floatValue] * [_Sphereradius floatValue]);
    
    [_SphereVolume setFloatValue:result];
}

i keep getting wrong value for the volume

if i for example enter 5 to radius field I'm getting 392.699096679688 when i should be getting 523.5987756

what am i doing wrong

It looks to me like the (4/3) is being treat as an integer and is rounding down to 1 (if you calculate 1 * 3.141592653589793 * ([_Sphereradius floatValue] * [_Sphereradius floatValue] * [_Sphereradius floatValue] it gives you the error you're getting)

Try

float result = (4.0/3.0) * 3.141592653589793 * ([_Sphereradius floatValue] * [_Sphereradius floatValue] * [_Sphereradius floatValue]);
 

abcdefg12345

macrumors 6502
Original poster
Jul 10, 2013
280
86
It looks to me like the (4/3) is being treat as an integer and is rounding down to 1 (if you calculate 1 * 3.141592653589793 * ([_Sphereradius floatValue] * [_Sphereradius floatValue] * [_Sphereradius floatValue] it gives you the error you're getting)

Try

float result = (4.0/3.0) * 3.141592653589793 * ([_Sphereradius floatValue] * [_Sphereradius floatValue] * [_Sphereradius floatValue]);

I tried (4.0/3.0) and it worked, u r right it's been treating (4/3) as an integer
 

Dranix

macrumors 65816
Feb 26, 2011
1,057
527
left the forum
Yes as is default in all c-based languages. Simply put a "." or ".0" on one of the constants to force the expression to floating point.

The problem is that the parser treats sub-expressions separated from each other when considering type. So "(4/3)" is a integer expression because both operands are integers. I normally add "." to the first operand so it would look like "(4./3)".
 

Senor Cuete

macrumors 6502
Nov 9, 2011
406
26
The value of pi (and 2pi, etc.) is defined in header file math.h as
#define M_PI 3.14159265358979323846264338327950288
so you should #include <math.h> and use M_PI instead of declaring a value for pi.

On some systems eg. double * double * double is much faster than pow(double, 3.0) so this is sometimes found as an optimization but there is some overhead in fetching your value with the accessor method [_Sphereradius floatValue], so I would just call pow([_Sphereradius floatValue], 3.0) unless you have some reason to do it the way you did.
 

ArtOfWarfare

macrumors G3
Nov 26, 2007
9,451
5,837
there is some overhead in fetching your value with the accessor method [_Sphereradius floatValue]

More important than the performance hit from calling the accessor method like this is the maintance hit from this. It makes it more difficult to read and more likely that they'll make a mistaken when modifying the code in the future.
 

Partron22

macrumors 68030
Apr 13, 2011
2,655
803
Yes
Your 4/3 is giving an integer result 1, instead of the floating val 1.333333.

392.699... * 1.33333 = 523.5987...
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.