Notes
  • Notes
  • JavaScript
    • URL processing
    • Numbers
  • Python
    • python random notes
    • Python Resources
  • Setup
    • Mac Setup
  • Command Line
    • Basics
    • Linux basics
    • Bash script
    • Create temp files
    • awk
    • sed
    • make
    • ssh
    • gzip
    • Command line tools
    • ffmpeg
    • at and crontab - scheduling commands
  • Web Developement
    • Chrome Dev Tools
    • HTML
    • Markdown
    • CSS
    • Rails
    • Hugo
    • REST APIs
  • Soft Skills
    • Listening Skills
    • Public Speaking
  • Containers
  • Career
    • Resume
    • Interview
    • Promotion
    • Keeping Track of Your Work
    • Decide What to Work On
  • Ergonomics
    • Work Env Setup
    • Pain Relieve
  • Digest / Writing Ideas
    • Books
      • Antifragile
      • Anti-Intellectualism in American Life 美国的反智传统
    • Economy / Society
    • How to spend your time
    • Life
    • Higher education
  • Misc
    • Regex
    • Don't Make Me Think
    • Microsoft Excel
    • AdTech 101
  • Resources
    • web
    • Vim
    • Tools
    • System Design
    • Design Pattern
    • Load Balancer
    • References
    • Hardware
    • Algorithm Resources
    • Command Line Resources
  • Git
    • Pro Git
  • Non-Tech
    • 化学科普 - 拿破仑的纽扣
    • 人生经验 - If I Knew Then
    • 哲学
      • Harvard - Justice
    • 宗教
      • Introduction to the New Testament History and Literature
      • 蔡志忠 - 漫画东方圣经
    • 人文
      • Open Yale Course - 心理学导论
  • Spark
  • VS Code
Powered by GitBook
On this page
  • Convert Strings to Number
  • NaN: Not a number
  • Bitwise trick to convert Float to Int
  • Scientific Number Literals
  • Interesting Arithmetics
  • 9999999999999999.0 - 9999999999999998.0

Was this helpful?

  1. JavaScript

Numbers

Convert Strings to Number

parseInt('123', 10); // 10 is the base. In old browsers, strings start with 0 will be converted in octal (base 8).
// parseFloat() always uses base 10
+ '42';   // 42
+ '010';  // 10
+ '0x10'; // 16. 0x is 16-based.
parseInt('123abc', 10); // 123
+ '123abc'; // NaN
// Number(str) is the same as + str
// for +, if both perands are numbers, add them. otherwise, convert to strings and concat.
+'3' + (+'4') // 7

NaN: Not a number

typeof NaN; // number
NaN === NaN; // false
NaN !== NaN; // true
isNaN(NaN); // true
isNaN("Hello"); // true. Try to convert "Hello" to a number and results in NaN
isFinite(NaN); // false
isFinite(-Infinity); // false
~~(5.423451) === 5 // Double not
5.423451 | 0 === 5 // Or
5.423451 << 0 === 5 // Right shift
5.423451 >> 0 === 5 // Left shift

Scientific Number Literals

1.234e3; // 1234
1.234E3; // 1234
12340e-1; // 1234

Interesting Arithmetics

0.1 + 0.2; // 0.30000000000000004
(0.3 - 0.2) === (0.2 - 0.1); // false
Math.sin(Math.PI); // 1.2246467991473532e-16. should be 0
Number.POSITIVE_INFINITY; // Infinity
Number.POSITIVE_INFINITY * 2; // Infinity
Number.MAX_VALUE; // 1.7976931348623157e+308
Number.MAX_VALUE + 1; // 1.7976931348623157e+308
Number.MAX_VALUE * 2; // Infinity
Number.MIN_VALUE; // 5e-324
Number.MAX_VALUE / 2; // 8.988465674311579e+307
Number.MAX_SAFE_INTEGER; // 9007199254740991
Number.MAX_SAFE_INTEGER + 1; // 9007199254740992
Number.MAX_SAFE_INTEGER + 2; // 9007199254740992
Infinity === Infinity + 1; // true
NaN === NaN + 1; // false

The answer is 2 in JavaScript (same as a lot of other programming languages), which is obviously wrong. This is due to the inaccurate nature of floating point numbers. Actually if you type 9999999999999999.0 in the console, it would return 10000000000000000. Since 9999999999999999.0 is larger than Number.MAX_SAFE_INTEGER (9007199254740991), we really should not be surprised at any weird arithmetic results.

PreviousURL processingNextPython

Last updated 5 years ago

Was this helpful?

Note that for negative numbers, ~~(-5.423451) === -5 but Math.floor(-5.423451) === -6.

Bitwise trick to convert Float to Int
This is faster than Math.floor.
9999999999999999.0 - 9999999999999998.0