The Power of Point Five – Learn More About Your Numbers

I’ve been doing daily coding challenges on a couple of websites. It’s been great to keep my skills fresh and find where I can improve my programming skills.

Now the problem that I was working on yesterday was a simple one. I looked at it and thought to my self, “this is a little bit of simple math, I’ll bang this out nice and quickly!”. I write my code, give it a check, run the test case and at that point I think I’m golden. Then I run the submission tests, Pass, Pass, Pass, Fail….. wait, what just happened?
I check my code and run the test case again, Pass. I run a custom test case, Pass. I run the submission tests, Pass, Pass, Pass, Fail.
Ok, my first thought is that there’s an edge case that I missed. Or I might need to run a special case to clean up and irregular input? I come up with a method for filtering out two possible edge cases, one to clean input values and another to deal with zero values.
I check my code and run the test case again, Pass. I run a custom test case, Pass. I run the submission tests, Pass, Pass, Pass, Fail.

It’s time to move on to my next step in problem solving, find out what I can learn from other people. I head to the discussion forums and start looking through issues that other people have run into and how they were able to solve them. I’m doing the challenge in Javascript and a lot of the discussion is around Python and Java. I’m seeing plenty of comments around the types of numbers that are being used but I start to notice a pattern, .5.
Now I thought that I had handled my rounding properly. I tested my code with a number of inputs and tried to force rounding issues, even with trying to force rounding issues and confirming correct output. What I hadn’t taken into account with my code was the floating point rounding errors in Javascript. On a very small scale I was missing a rounding error, eg. Math.round(1.005*100)/100; // 1 instead of 1.01.
That’s where .5 comes in. If it’s added before the round it will account for the floating point rounding error.
Now I check my code and run the test case again, Pass. I run a custom test case, Pass. I run the submission tests, Pass, Pass, Pass, Pass.

In the future I’ll dig into more details on floating point numbers and ways to work with them. For now a couple of things to remember.
•Numbers still need context. Be aware of how they’re stored, rounded and the level of precision. Small errors can be compounded to significant errors.
•Don’t assume that you know what errors to look for. Always include outside information when possible. Find resources from people that have faced similar challenges, or are experts in a subject, and leverage their experience.
•Test, adapt, test, adapt. Keep at it, some of your solutions won’t work, some will. The way that we get to many good solutions is through trying many times and keeping the things that work.

Additional Reading
What Every Developer Should Know About Floating Points
What Every Javascript Developer Should Know About Floating Point Numbers
What Every Computer Scientist Should Know About Floating-Point Arithmetic
Rounding Decimals in Javascript

Leave a Reply

Your email address will not be published. Required fields are marked *