Arithmetic JavaScript, Overflow/Underflow in JavaScript, JavaScript 64-bit floating point format

Usually programming languages doesn’t make any kind of distinction between different types of mathematical numbers(like integers, floats etc.) but JavaScript treat these numbers in separate categories – Integer values and Floating-point values.

In order to deal with numbers, internally javaScript uses 64-bit floating point format, which implies that JavaScript can easily deal with numbers ranging from -9007199254740992 X 253 to 9007199254740992 X 253.

Moreover most of mathematical operations(addition, multiplication …..) uses 64-bit floating point format. But in some cases like Array indexing, Bitwise Operations uses 32-bit floating point format.

Note that JavaScript could deal with numbers outside this range, but in that case it have to ignore trailing digits after 64-bits. Which would decrease Mathematical precision.

JavaScript internally converting numbers to 64-bit

JavaScript just internally uses 64-bit floating point format, it doesn’t mean that you have to write numbers in 64-bit format while writing code. Even if you write a number simply, JavaScript would automatically take care of it and change it to 64-bit format for dealing with it internally.

3
Number 3 internally would be treated as 3.0 X e0

So sit back in some relaxing position and write amazing JavaScript code without worrying about little details related to numbers.

Arthimetic in JavaScript

JavaScript is one of only language except Python, which I’ve learned is super focused on getting things going or building a product. Like JavaScript could be used for sending requests across a network(from client to server or server to client) or it could be used for building up amazing online web browser based games.

JavaScript support simple Mathematical operations(like addition) just with use of symbols as is done in actual Maths.

Operators in JavaScript

3 + 2 = 5
23 - 1 = 22
34 * 2 = 68
100/4 = 25

Not only these simple Mathematical stuff, JavaScript does have Math object having many methods which could be invoked on numbers.

JavaScript Math Object Examples

Math.pow(4, 3)  //4 to the power of 3 (64)
Math.round(1.3) //round to nearest integer (1)
Math.ceil(.8) //round up to nearest integer (1)
Math.floor(.8) //round down to nearest integer (0)
Math.max(1, 40, 20) //maximum number out of all (40)
Math.log(10) //natural logarithm of 10 (2.3025)
Math.log(100)/Math.LN10 //base 10 logarithm of 100 (2)
Math.log(8)/Math.LN2 //base 2 logarithm of 8 (3)

There are tonne of many more Methods in Math Object, which you could use inside your JavaScript Code. In case, your interested in knowing more about other methods. Read Math Object Official Documentation at Mozilla – Math Object.

Overflow, Underflow in Arithmetic(JavaScript)

As I’ve already stated above that JavaScript deals with numbers ranging from -9007199254740992 X 253 to 9007199254740992 X 253, but what if number is either below or above this?

  • Overflow in JavaScript

If number > 9007199254740992 X 253 then => Overflow If number > -9007199254740992 X 253 then => Overflow

Usually this occurred in case, some Arithmetical Operation on numbers either become quite large or small. This could be quite problematic, so JavaScript Developer should carefully examine code to see if there’s any possibility of Overflow or Underflow.

Moreover, whenever Overflow occurs JavaScript doesn’t retain actual number rather it just drops it and instead treat it as Infinity.

If number > 9007199254740992 X 253 then JavaScript changes it to => Infinity If number > -9007199254740992 X 253 then JavaScript changes it to => -Infinity

  • Underflow in JavaScript

In JavaScript underflow occurs when result of an Arithmetic Operation becomes closer to -9007199254740992 X 253 then JavaScript would great it as => 0

Conclusion

Regarding arithmetic operations in JavaScript, you as a Developer doesn’t need to worry much about as most of things are taken care of internally by language itself. While writing JavaScript code just keep in mind that there does exist certain objects, which could be used and you don’t need to write a 1000’s of lines of code.

If you finds this articles useful and your learning JavaScript Development, then do check out other articles about JavaScript on this website – JavaScript Development Computer Science Hub.

Related Posts

Leave a Reply

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