Public Group

Magic Numbers

This topic is 3035 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Okay, I made a program today that, when given a formula name (like pyramid volume) it asks for the parameters (or arguments, I'm not a (pro)grammar Nazi (pun intended)) and then gives the answer. Simple, right? Well, I have a bit of a program "ethics"/"good practice" problem.

Say I have a simple function giving out the volume of a cylinder when given the parameters, like this:

return (4 / 3) * pi * pow(radius, 3);

So.. would 4, 3, and 3 count as magic numbers, even though they are being used as constants in a well-known equation? This is really bothering me for some reason.

Share on other sites
Not if they are ALWAYS used, just hard code it in.

in that equation

(4/3) * PI  * (radius^3)

the only variable you need is Radius.

so e.g.

Find the Volume of Cylinder Radius : '<user Enters>'

Share on other sites
Quote:
 Original post by SomeGuy49Okay, I made a program today that, when given a formula name (like pyramid volume) it asks for the parameters (or arguments, I'm not a (pro)grammar Nazi (pun intended)) and then gives the answer. Simple, right? Well, I have a bit of a program "ethics"/"good practice" problem.Say I have a simple function giving out the volume of a cylinder when given the parameters, like this:return (4 / 3) * pi * pow(radius, 3);So.. would 4, 3, and 3 count as magic numbers, even though they are being used as constants in a well-known equation? This is really bothering me for some reason.

The first thing that should concern you is correctness: That's the formula for the volume of a sphere, not a cylinder. Also, (4 / 3)' is 1 in many programming languages (you didn't say which one you are using), so be careful.

The next thing that should concern you is clarity. To me the code containing those constants is perfectly clear. I am not sure what alternative you had in mind, but I think the code would be made less clear by giving them names and declaring them somewhere else.

Share on other sites
Yeah, literal constants that are part of a sensible equation are fine. They're not really 'magic numbers'.

If I saw code like the following, I'd be quite upset with the author:
float CylinderVolume( float radius, float height ){  const static float radiusPower = 2.0f;  const static float radiusMultiplier = M_PI;  return radiusMultiplier * pow( radius, radiusPower ) * height;  //UNDONE: The below version isn't maintainable.   //        If the number of radians in a circle is updated, this this code will break.  //        The above is a new, maintainable version.  //return M_PI*radius*radius * height;}

Share on other sites
Quote:
 Original post by alvaroThe first thing that should concern you is correctness: That's the formula for the volume of a sphere, not a cylinder. Also, (4 / 3)' is 1 in many programming languages (you didn't say which one you are using), so be careful.

Oh, I screwed that one up. I think I was planning on writing the cylinder formula and wrote the sphere one.

And I'm using Microsoft Visual C++ 2010 Express.

Share on other sites
Quote:
 Original post by alvaro[...] Also, (4 / 3)' is 1 in many programming languages (you didn't say which one you are using), so be careful.

Quote:
 Original post by SomeGuy49And I'm using Microsoft Visual C++ 2010 Express.

Yup, that would make (4 / 3)' equal to 1, and mess up your formula. Use `4.0/3.0' instead.

1. 1
2. 2
Rutin
20
3. 3
4. 4
A4L
15
5. 5
khawk
14

• 13
• 26
• 10
• 11
• 9
• Forum Statistics

• Total Topics
633738
• Total Posts
3013617
×