JavaScript and Decimal Places, A Shortcut to Insanity

Uhhhh, who invited Gottfried to the Party?

Uhhhh, who invited Gottfried to the Party?

 

So after spending a couple of days trying to work through the freeCodeCamp algorithm script Exact Change I came across a rather unexpected issue.

In JavaScript (0.1 + 0.2 === 0.3) is false.

After running around in circles crying foul and threatening my laptop with a number of things that would break the Geneva Convention I had a look around the internet to see if anyone else had experienced the same problem.

It turns out that my anger was misdirected at my laptop (apologies); in fact it should have been aimed at a young man by the name of Gottfried Lebiniz, the inventor of binary.

The problem is that computers speak in the language called binary (a sequence of Zeros and Ones) meaning that numbers have to be stored in a format called “binary floating point”. This format is designed in such a way that it is unable to actually represent the true value of some decimal places.

In JavaScript 0.1 + 0.2 actually equals 0.3000000000000004.

Confused? Yeah me too, until I chanced across this video, it’s about 30 mins long but if you can get through it, you’ll no longer be preparing to hang your computer’s toes over some burning coals, but quickly googling good old Gottfried in order to put a curse upon him (he died in 1716 just fyi, I guess someone else got to him first). Also you might just learn a thing or two about how binary floating point works.

 

 

If you’d like some more information on the floating point format I’d recommend the website http://floating-point-gui.de.

As for the freeCodeCamp’s Exact Change … move over Ben Affleck, Math.round() has just become my new best friend.

 

I Built This!

 

See the Pen Free Code Camp Weather App by Luke (@escrew7) on CodePen.

This is my version of the Free Code Camp Weather App written up on Codepen. I’m pretty happy with how it has turned out. A couple of things I am particularly stoked about are the toggle on the degrees <div> where you can click to change from Farenheight to Celsius; and the minimal design of the app, which I think is suited to its purpose.

Free Code Camp along with Khan Academy have, to me, been two of the better free web development learning platforms. Both boast strong challenges along with great support networks for when you run into coding problems. Free Code Camp has really pushed me to my limits with JavaScript and on more than one occasion I’ve stormed off complaining about how the site is cheating me, only to realise that I’ve spelt “function” wrong.

One feature I’ve really enjoyed about Free Code Camp is that you can enter into a “contract” with the Camp, where each month you donate a sum of money to a choice of charities until you finish your selected certificate. It’s really rewarding knowing that you are achieving your goals of learning code whilst also giving others less fortunate a leg up in achieving their own dreams. I decided to donate monthly to the http://www.blackgirlscode.com/ school and have been 9 months.

freeCodeCamp is no walk in the park by any means, but if you really push yourself, the rewards that come from completing algorithms and projects is legit.

https://www.freecodecamp.com/

Give it a shot.