Why JavaScript is Bad At Math

Why JavaScript is Bad At Math

In this tutorial, we will learn why JavaScript is Bad At Math and how to work with its limitations.

JavaScript isn’t alone here; if you performed the same addition in several other programming languages you would get similarly slightly incorrect answers. So why is it that for all the complex operations you can perform in code, certain simple calculations can go awry? The answer lies in how JavaScript stores numerical values in memory.

Before we can get into how these values are stored in memory, we need to talk about numbers, and the different ways we can represent them.

In arguably their most simple form, there are whole numbers (integers), e.g. 5. This type of number has no fractional values.

Then there are fractions, e.g. 1/5. This implies that 1 is being divided into 5 pieces, and is usually represented with the value 0.2. We consider this to be a finite representation, meaning that it stops. If we consider instead 1/3, this is an infinite representation, as we could write 0.3333333333 and so on infinitely in our attempt to represent this fraction.

There’s a distinction which needs to be made here. Our representation of 1/5 as 0.2 is a decimal _number, meaning that it’s written in _base 10. We’ll come back to this.

There’s a further way that we can represent numbers, known as ‘standard form’, or ‘scientific notation’. In standard form we write numbers out in two parts: first, the number itself, with one number ahead of a radix point and the other numbers behind it, and then a value to multiply it by, comprising a base and an exponent, which will return that initial number to its full form.

You’ll likely be familiar with this from very large or small numbers. For example, the earth’s mass can be written as 5.9724 x 10²⁴ kg, which is a handy way of avoiding having to write out 5,972,400,000,000,000,000,000,000 kg in full. Here the 10 is the base, and the exponent is 24, i.e. the number the base is raised to the power of. The numbers multiplied by the base raised to the power of the exponent are known as the significand, or the mantissa, and the amount of these is known as the precision, so in the above example we have a precision of 5.

We can write any number out in this manner, not just extremely large or small ones; if we wanted to represent the number 1 this way we can write it as 1.0 x 10⁰. And we can also represent fractions this way if we use a negative exponent, so if we want to, we can represent 1/5 as 2.0 x 10-¹.

Numbers written in this way are known as ‘floating point numbers’, referring to the moving radix point.

javascript

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Hire Dedicated JavaScript Developers -Hire JavaScript Developers

Hire dedicated JavaScript Developers who are proficient in AngularJS, ReactJS, NodeJS, & VueJS frameworks. Get flexible hiring models as per your business requirements.

JavaScript Shopping Cart - Javascript Project for Beginners

JavaScript Shopping Cart - javascript shopping cart tutorial for beginnersBuy me a coffee 🍺 https://www.paypal.com/paypalme/ziddahSource Code: https://bit....

The essential JavaScript concepts that you should understand

The essential JavaScript concepts that you should understand - For successful developing and to pass a work interview

Data Types In JavaScript

JavaScript data types are kept easy. While JavaScript data types are mostly similar to other programming languages; some of its data types can be unique. Here, we’ll outline the data types of JavaScript.

Introduction With Basic JavaScript

Introduction With Basic JavaScript - Unlike most programming languages, the JavaScript language has no concept of input or output. It is designed to run as a scripting language in a host environment, and it is up to the host environment to provide mechanisms for communicating with the outside world.