Double the Beyoncé: Navigating Numbers in JavaScript

A presentation at JSConf.Asia 2019 in June 2019 in Singapore by Meggan Turner

Slide 1

Slide 1

Double the Beyoncé !! Navigating Numbers in JavaScript Meggan Turner - Software Engineer @ Jaxsta @megganeturner

Slide 2

Slide 2

@megganeturner

Slide 3

Slide 3

What’s a number? @megganeturner

Slide 4

Slide 4

Count Measure Label Identify @megganeturner

Slide 5

Slide 5

Classification @megganeturner

Slide 6

Slide 6

Natural 1, 2, 3, 4… Integer -2, -1, 0, 1, 2… Rational ½, ⅓, ¼… Irrational π, √2… Real 1, ½, 0.7, π, √2… @megganeturner

Slide 7

Slide 7

Complex Transcendental Imaginary Infinity (& —infinity) Infinitesimals Surreal … ¯_(ツ)_/¯ @megganeturner

Slide 8

Slide 8

Representation @megganeturner

Slide 9

Slide 9

8 ⼋八 आठ @megganeturner

Slide 10

Slide 10

Base 10 1234567 Binary 0b100101101011010000111 Octal 0o4553207 Hexadecimal 0x12D687 Scientific Notation 1.234567e+06 (or 1.234567 * 106) @megganeturner

Slide 11

Slide 11

What’s a number in JavaScript? @megganeturner

Slide 12

Slide 12

Standards @megganeturner

Slide 13

Slide 13

IEEE-754 IEEE Standard for Floating Point Arithmetic Or why 0.1 + 0.2 !== 0.3 @megganeturner

Slide 14

Slide 14

• Specifies the implementation of floatingpoint arithmetic in JavaScript (how we represent decimal points in binary) • Allows us to represent real numbers as an approximation, to support a trade off between range & precision @megganeturner

Slide 15

Slide 15

Range -9007199254740991 — +9007199254740991 Precision 17 decimal places (e.g. 0.30000000000000004) @megganeturner

Slide 16

Slide 16

01001000 01100101 01101100 01101100 01101111 00101100 00100000 01010011 01101001 01101110 01100111 01100001 01110000 01101111 01110010 01100101 00100001 Hello, Singapore! @megganeturner

Slide 17

Slide 17

We can’t have decimal points in binary 🤔 @megganeturner

Slide 18

Slide 18

Floating-point arithmetic tries to account for this 💪 @megganeturner

Slide 19

Slide 19

1234567 01000001001100101101011010000111 00000000000000000000000000000000 @megganeturner

Slide 20

Slide 20

64 bits 1 bit 11 bits 52 bits 0 10000010011 0010110101101000011100000000000000000000000000000000 Sign Exponent Significand / Mantissa

  • 1.23 * 6 10 @megganeturner

Slide 21

Slide 21

@megganeturner

Slide 22

Slide 22

@megganeturner

Slide 23

Slide 23

Integers Fractions -2, -1, 0, 1, 2 ½, ⅓, ¼, ⅕… 🎉 % @megganeturner

Slide 24

Slide 24

⅓ = 0.333… 0.33 + 0.33 + 0.33 !== 1.0 @megganeturner

Slide 25

Slide 25

0.1 (decimal) 0.00011001100110011001100110011001 10011001100110011001101 (binary) 0.10000000000000000555 (decimal) @megganeturner

Slide 26

Slide 26

How big a problem is this really? @megganeturner

Slide 27

Slide 27

0.00000000000000004cm = 0.0000000003 times as long as a Glucose Molecule 🔬 @megganeturner

Slide 28

Slide 28

0.00000000000000004km = 0.0000000000000000000001 times as long as The Distance from Earth to the Moon 🌏 🌝 @megganeturner

Slide 29

Slide 29

0.00000000000000004ly = 38.38cm, or about the height of a standard bowling pin 🎳 @megganeturner

Slide 30

Slide 30

In most cases - this degree of accuracy is not going to be that important @megganeturner

Slide 31

Slide 31

Size really does matter @megganeturner

Slide 32

Slide 32

@megganeturner

Slide 33

Slide 33

A trade off between range and precision @megganeturner

Slide 34

Slide 34

🤔 @megganeturner

Slide 35

Slide 35

@megganeturner

Slide 36

Slide 36

✨ ✨ ✨ ✨ 3.28 x ✨ ✨ ✨ 80 10 ✨ ✨ ✨ @megganeturner

Slide 37

Slide 37

(or ~9 quadrillion) @megganeturner

Slide 38

Slide 38

@megganeturner

Slide 39

Slide 39

@megganeturner

Slide 40

Slide 40

@megganeturner

Slide 41

Slide 41

https://jaxsta.com/ @megganeturner

Slide 42

Slide 42

@megganeturner

Slide 43

Slide 43

@megganeturner

Slide 44

Slide 44

@megganeturner

Slide 45

Slide 45

https://jaxsta.com/ @megganeturner

Slide 46

Slide 46

⚠ @megganeturner

Slide 47

Slide 47

New Tab Dev Tools @megganeturner

Slide 48

Slide 48

• Correct, unparsed integer in new tab • Parsed integer in dev tools • Parsed integer in new tab ¯_(ツ)_/¯ • Parsed integer in dev tools @megganeturner

Slide 49

Slide 49

Solution: use UUIDs @megganeturner

Slide 50

Slide 50

https://jaxsta.com/ @megganeturner

Slide 51

Slide 51

@megganeturner

Slide 52

Slide 52

Enter BigInt @megganeturner

Slide 53

Slide 53

• New numeric primitive (introduced in 2018) • Available in Chrome, Firefox (68 beta), Edge • We can represent numbers beyond the MAX_SAFE_INTEGER @megganeturner

Slide 54

Slide 54

@megganeturner

Slide 55

Slide 55

@megganeturner

Slide 56

Slide 56

@megganeturner

Slide 57

Slide 57

⚠ No mixed type operations ⚠ @megganeturner

Slide 58

Slide 58

@megganeturner

Slide 59

Slide 59

@megganeturner

Slide 60

Slide 60

@megganeturner

Slide 61

Slide 61

@megganeturner

Slide 62

Slide 62

@megganeturner

Slide 63

Slide 63

@megganeturner

Slide 64

Slide 64

• Not ready for production apps (yet) • Not available in all browsers • Not a lot of documentation available @megganeturner

Slide 65

Slide 65

@megganeturner

Slide 66

Slide 66

, ! @megganeturner

Slide 67

Slide 67

In Summary @megganeturner

Slide 68

Slide 68

🎯 • 64 bit floating points • Precision errors • Round! 🐳 • Max Safe Integer • BigInt @megganeturner

Slide 69

Slide 69

Less @megganeturner

Slide 70

Slide 70

More @megganeturner

Slide 71

Slide 71

Less @megganeturner

Slide 72

Slide 72

More @megganeturner

Slide 73

Slide 73

@megganeturner