Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

An easy mistake to make

Status
Not open for further replies.

jpadie

Technical User
Nov 24, 2003
10,094
0
0
FR
I've been developing a huge plugin for wordpress recently. well over 200000 lines of code so far. so much for holidays...

this is a post that may help others. For the last day I have been debugging a MATHS problem. Yes, simply adding up various invoices, calculating some interest etc. I thought i had it cracked and started a batch job to print 400 legal letters (i am a lawyer). i was bundling them into envelopes when i thought to check the output for a random sample.

not cracked at all - the maths was wrong. very wrong, the invoices were not being totalled correctly leading to requests for x rather than many times x....

another day of footprinting and tracking through the code and genuinely getting very grumpy indeed. It turns out that the error was the difference between

Code:
+=
//and
=+

both are perfectly valid code, neither throw errors.

the difference is this
Code:
$a = 5;

$a += 3;
//$a is 8
whereas
Code:
$a = 5;
$a =+ 3;

//$a is 3.

the reason is because in the second code i was simply assigning the value of +8 to the variable.

A simple transposition of an otherwise commutative operator has a huge effect.

In hindsight the long form I feel is always better
Code:
$a=3;

$a = $a + 5;

a salutary lesson that the littlest thing can have an enormous influence.
 
I wouldn't view the operator as commutative, but in math (not php operator math) it is.

As you've found, $a =+ 3 is assigning a positive 3 to $a; just as $a =- 3 would assign a negative 3.
 
I always knew that it should be +=. The mistake was typographical but because no error was thrown and because the transposition did not look 'wrong' it took me a very long time to track and fix it.
 
Long long ago I decided I wasn't clever enough to use these C like idioms (or really other people whose code I was looking at weren't clever enough to use these idioms !). Very good to look at but sorting out a bug at 3am it's a different ball game. At least you know where you are with x=x+3.
An example I often quote is the problem they had with one of the space shuttles. Apparently 3 computers all had to agree on an answer before take off. All software was developed by different software houses and they weren't allowed to use standard libraries or text books but had to calculate formulas as there was a chance that the same error existed in many books, or they might used the same book. They all had to implement their own functions from first principles.One day the 3 computers disagreed. One of the implementations was written in FORTRAN. The line of code was
Code:
for10=1.10
Which looks fine if you want to assign the value 1.10 to the variable for10. Now this is where FORTRAN gets good. You don't have to have spaces between keywords which gives us the real problem. Consider this code
Code:
for 10 = 1,10
Which is FORTRAN speak for set me up a loop until a label of 10 (bit like BASIC) 1 for 10 times. Now if I take out the spaces it looks like
Code:
for10=1,10
Which is remarkably similar to the first line of code except for the , (comma). Without looking up the exact story (and I can't remember much of my university FORTRAN) I can't be exact but it does show how easy it is to make a mistake.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top