Skip to content
0:00 / 0:00

#floating-point#ieee-754#numbersinjavascript

I ❤ #javascript: 100.000.000.000.000.000 + 8 = ? in JavaScript

Styled JavaScript

1.7K subscribers • 7 videos on Hallway

YouTube
  • Recommended by 2 people: 2 in engineering communities.
  • Presented by Anatol

Intermediate. Assumes working knowledge.

488 views7 years ago2 recommendations

Programming languagesComputer scienceJavaScriptMath for programmers

Anatol explains how JavaScript represents numbers as 64-bit floating-point values and why large integer addition can produce unexpected results.

100.000.000.000.000.000 + 8 = ? Do you know what would be the result of this equation in JavaScript? Don't jump into conclusion. Take a deep breath.. The answer is bazillion. 100.000.000.000.000.000 + 8 = 100.000.000.000.000.000 100.000.000.000.000.000 + 9 = ? How do you feel about the following equation in JavaScript? The answer is bazillion twenty. 100.000.000.000.000.000 + 9 = 100.000.000.000.000.020 Why is that? Let's clear it up. You can read about Number representation in JavaScript be the following link: 2ality.com/2012/04/number-encoding.html Hi let's talk today about Integer representation in JavaScript. I'm Anatol and you're watching the Good Parts of the FrontEnd development. If this is your first time here and you wanna learn #Frontend and #JavaScript, start right now by subscribing and don’t forget to turn on the subtitles. JavaScript numbers are all 64-bit floating point values (or double precision), stored according to the IEEE 754 standard. The fraction occupies bits 0 to 51, the exponent occupies bits 52 to 62, and the sign occupies bit 63. According to that JavaScript can only safely represent numbers within the range: -(2^53 - 1) and 2^53 - 1. developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Globa… Safe in this context refers to the ability to represent integers exactly and to correctly compare them. All previous numbers can be represented: Math.pow(2, 53) 9007199254740992 Math.pow(2, 53) - 1 9007199254740991 Math.pow(2, 53) - 2 9007199254740990 But this integer value cannot be represented: Math.pow(2, 53) + 1 9007199254740992 Numbers higher than 2^53 can be represented Math.pow(2, 53) 9007199254740992 Math.pow(2, 53) * 2 18014398509481984 because the exponent can be used. Each multiplication by 2 simply increments the exponent by 1 and does not affect the fraction. Hence, the initial sample is evaluated as following: 99999999999999970+8 99999999999999980 99999999999999980+8 100000000000000000 100000000000000000+9 100000000000000020 It's because the limiting resource at the high end of the integer range is the fraction, meanwhile the exponent still has room to grow. That's it. So easy. Used: looperman.com: Rosita Lofi Beat by Fanto8BC, Progress by nightingale, Chiptune Melody by L0neW0lf, Film Noir by LankFrampard, Russian Boogie by nightingale

Why it's here

2 people recommend this

u/ms-maria-main r/Frontend • Recommends

I ❤ #javascript: 100.000.000.000.000.000 + 8 = ? in JavaScript

u/r-wabbitin r/node • Recommends

I ❤ #javascript: 100.000.000.000.000.000 + 8 = ? in JavaScript