Pages: [1]   Go Down
Author Topic: Magic numbers  (Read 1016 times)
0 Members and 1 Guest are viewing this topic.
nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 129
Posts: 8589
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

We all know that so-called magic numbers are bad in a program. They are sometimes needed though and the usual technique is to create a human-readable #define so at least you know what the number represents, for example

Code:
if (x < 3600) do_something();

What the heck is 3600? But

Code:
#define SECONDS_PER_HOUR  3600
...
if (x < SECONDS_PER_HOUR) do_something();

Makes some sense. So I had call to revisit some old code for my digital speedo today and found this

Code:
#define PULSES_PER_K (2089 - 21)
#define PULSES_PER_100M (PULSES_PER_K / 10)
#define MAGIC_NUMBER (16604 * 1.12)

Lucky I had that last define or I'd never know that the heck (16604 * 1.12) represents.

______
Rob
Logged

Rob Gray aka the GRAYnomad www.robgray.com

Cape Town South Africa
Offline Offline
Edison Member
*
Karma: 19
Posts: 1217
A newbie with loads of posts, and still so much to learn !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Is that the number of magic mushrooms you can grow on your smallholding ?


(  its so nice to be able to talk of such things now I am not a God member anymore :-)  )
Logged

With my mobile phone I can call people and talk to them -  how smart can you get ?

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 129
Posts: 8589
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

smiley

If we had any mushrooms they would have been washed away in the floods. 450mm of rain the other day. No rain all year so our friends bought 10,000 litres, the truck was barely down the drive and the rain started. Life's a bitch.

_____
Rob
Logged

Rob Gray aka the GRAYnomad www.robgray.com

Cape Town South Africa
Offline Offline
Edison Member
*
Karma: 19
Posts: 1217
A newbie with loads of posts, and still so much to learn !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


I hear the flood warnings are very worrying down the East Coast again, the pictures of the Bundaberg area are frightening.

Fresh water in the truck could be valuable in a flood ?  but no doubt they dumped it in his tank...
Logged

With my mobile phone I can call people and talk to them -  how smart can you get ?

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 95
Posts: 4089
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
#define PULSES_PER_K (2089 - 21)
#define PULSES_PER_100M (PULSES_PER_K / 10)
#define MAGIC_NUMBER (16604 * 1.12)

Ha! Love it. Just remember, nothing is ever a complete failure, it can always be used as a bad example smiley-wink
Logged

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17301
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

We all know that so-called magic numbers are bad in a program. They are sometimes needed though and the usual technique is to create a human-readable #define so at least you know what the number represents, for example

Code:
if (x < 3600) do_something();

What the heck is 3600? But

Code:
#define SECONDS_PER_HOUR  3600
...
if (x < SECONDS_PER_HOUR) do_something();

Makes some sense. So I had call to revisit some old code for my digital speedo today and found this

Code:
#define PULSES_PER_K (2089 - 21)
#define PULSES_PER_100M (PULSES_PER_K / 10)
#define MAGIC_NUMBER (16604 * 1.12)

Lucky I had that last define or I'd never know that the heck (16604 * 1.12) represents.

______
Rob


What, no furlongs per fortnight factor macro?

Lefty
Logged

Central MN, USA
Online Online
Tesla Member
***
Karma: 74
Posts: 7250
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

So my question is: if a programmer doesn't use MAGIC_NUMBER but (16604*1.12) instead, how highly do you rate him/her [edit] work[/edit]?
« Last Edit: February 01, 2013, 04:15:35 pm by liudr » Logged


Cape Town South Africa
Offline Offline
Edison Member
*
Karma: 19
Posts: 1217
A newbie with loads of posts, and still so much to learn !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
if a programmer doesn't use MAGIC_NUMBER but (16604*1.12) instead, how highly do you rate him/her?

18,596.48  ?   
Logged

With my mobile phone I can call people and talk to them -  how smart can you get ?

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 129
Posts: 8589
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
nothing is ever a complete failure, it can always be used as a bad example
Very true.

Quote
furlongs per fortnight
The speedo was for my truck which is not very fast, that may have been an appropriate measure but I didn't think of it at the time smiley

Quote
the pictures of the Bundaberg area are frightening.
Largest recorded flood ever in Bundy, we have land about 65k away up in the hills so don't worry about floods although I know the roads has washed away not far from us.

______
Rob
Logged

Rob Gray aka the GRAYnomad www.robgray.com

Pages: [1]   Go Up
Jump to: