Become a MacRumors Supporter for \$25/year with no ads, private forums, and more!

# MacCalculating Volume Of sphere

#### 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
{

[_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
Staff member
Apr 2, 2006
5,645
2,637
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
{

[_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

Comment

#### 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

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

Comment

#### Dranix

##### macrumors 65816
Feb 26, 2011
1,024
422
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)".

Comment

#### Senor Cuete

##### macrumors 6502
Nov 9, 2011
329
14
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.

Comment

#### ArtOfWarfare

##### macrumors G3
Nov 26, 2007
9,014
4,891

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.

Comment

#### Partron22

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

392.699... * 1.33333 = 523.5987...

Comment
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.
{"extraction_info": {"found_math": false, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.8490239977836609, "perplexity": 5786.75475811271}, "config": {"markdown_headings": true, "markdown_code": false, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2021-04/segments/1610704828358.86/warc/CC-MAIN-20210127152334-20210127182334-00740.warc.gz"}