Friday, January 8, 2010

The most arcane bug of them all... Divide by Zero

It's interesting that even in modern times we still encounter the dreaded Divide by Zero error. I stumbled across this error recently in the Scriptaculous slider library.

There it was waiting to pounce from line 153 in the slider.js file upon any unsuspecting code that so happened to call the translateToPx() function. Here it is exposed to the harsh light of day:

  translateToPx: function(value) {
return Math.round(
((this.trackLength-this.handleLength)/(this.range.end-this.range.start)) *
(value - this.range.start)) + "px";
}


There! There is the beating of it's hideous heart! For if a slider is initialized with a range whose start value is the same as its end value a most wicked zero is spawned from hell and possesses the denominator. IN THE NAME OF ALL THAT IS GOOD I CAST YEA BACK INTO THE HELL OF COMPLEX NUMBERS!

  translateToPx: function(value) {
try {
return Math.round(
((this.trackLength-this.handleLength)/(this.range.end-this.range.start)) *
(value - this.range.start)) + "px";
} catch (e) {
return "0px";
}
}


http://prototype.lighthouseapp.com/projects/8887-script-aculo-us/tickets/154-0-range-slider-causes-ie-error

2 comments:

Anonymous said...

Evil HackerMathematican says:

0/0 is not a complex number...
..
.

it is indeterminate

MWHAHAHA!!!!

David Jones said...

Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Front end developer learn from Javascript Training in Chennai . or learn thru Javascript Training in Chennai. Nowadays JavaScript has tons of job opportunities on various vertical industry. JavaScript Training in Chennai