Jump to content
  • Advertisement
Sign in to follow this  
SomeGuy49

Magic Numbers

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
Share on other sites
Quote:
Original post by SomeGuy49
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.


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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by alvaro
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.


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 this post


Link to post
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 SomeGuy49
And 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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!