Skip to content
Numbers and Math
step 1/5

Reading — step 1 of 5

Learn

~2 min readGetting Started

Numbers and Math

TypeScript inherits JavaScript's number model unchanged, and it surprises people coming from C, Java, or Python: there is exactly one number type, called number, and it is a 64-bit IEEE 754 floating-point value. There is no separate integer type — 3, 3.5, and -0.001 are all just number.

The operators

const a: number = 17;
const b: number = 5;

console.log(a + b);    // 22
console.log(a - b);    // 12
console.log(a * b);    // 85
console.log(a / b);    // 3.4  <-- real division, never truncated!
console.log(a % b);    // 2    (remainder)
console.log(2 ** 10);  // 1024 (exponentiation)

The line that bites newcomers: / never does integer division. 17 / 5 is 3.4, full stop. If you want the whole-number part, you must ask for it explicitly:

Math.floor(17 / 5);  // 3 — rounds DOWN (toward negative infinity)
Math.trunc(17 / 5);  // 3 — chops the decimals (toward zero)
Math.round(17 / 5);  // 3 — nearest integer
Math.ceil(17 / 5);   // 4 — rounds UP
(17 / 5) | 0;        // 3 — bitwise trick; only safe for smallish non-negative values

Math.floor and Math.trunc agree for positive numbers but split on negatives: Math.floor(-3.5) is -4, while Math.trunc(-3.5) is -3. Choose deliberately.

Floats are approximate

console.log(0.1 + 0.2);         // 0.30000000000000004
console.log(0.1 + 0.2 === 0.3); // false

This is IEEE 754 floating point, not a TypeScript bug — 0.1 has no exact binary representation, the same way 1/3 has no exact decimal one. Two practical consequences:

  • Never use number for currency. Store cents as integers, or use a decimal library.
  • Integers ARE exact up to Number.MAX_SAFE_INTEGER (2^53 − 1). For exact integers beyond that, JavaScript has the bigint primitive (an ES2020 feature TypeScript types as bigint, written with an n suffix: 123n). You will rarely need it — number covers 99% of daily work.

Parsing strings into numbers

Input from stdin always arrives as a string. Convert it:

Number("42");          // 42
Number("42abc");       // NaN — strict: the whole string must be numeric
parseInt("42abc", 10); // 42  — lenient: parses the leading digits

Prefer Number(...) and treat NaN as a validation failure — Number.isFinite(x) is the clean check. NaN is contagious: any arithmetic involving it produces more NaN, so catch it early.

Your exercise

The starter reads three integers from stdin, one per line, using the platform's standard pattern:

const lines: string[] = require('fs').readFileSync(0, 'utf-8').trim().split('\n');

Each line is converted with Number(...). Your job: print the average of the three numbers, rounded down, i.e. Math.floor((a + b + c) / 3).

The exact mistake the grader will catch: forgetting Math.floor. For the hidden input 1, 1, 2 the raw average is 4 / 3 = 1.3333333333333333 — printing that fails the test, which expects exactly 1. Print one integer and nothing else; console.log supplies the newline.

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…

Numbers and Math — TypeScript Fundamentals