Skip to main content

Kinds of numbers and the number line

Nearly every number in machine learning is a point on one line. Meet the natural numbers, integers, fractions and real numbers, convert between fractions, decimals and percentages by hand, measure distance with absolute value, and see why Python’s 0.1 + 0.2 is not quite 0.3.
M01.1.1L0 Foundation25 minIn review

After this lesson you can

  • Place natural numbers, integers, fractions and real numbers on one number line, and say which of these collections a given number belongs to.
  • Convert a number between fraction, decimal and percentage by hand, in both directions.
  • Find the absolute value of a number and the distance between two numbers on the line.
  • Explain why a computer stores 0.1 only approximately, and compare two decimals in Python with math.isclose.

In review. The maths and code on this page are checked by automated tests on every change, but an independent reader has not signed it off yet. Spot a mistake or a confusing step? Tell us.

The question

A model answers 4 test questions and gets 3 right. One report gives its accuracy as 3/4, another as 0.75 and a third as 75%. Are these the same number? And why does Python, asked for 0.1 + 0.2, print 0.30000000000000004?

Intuition

The numbers of this lesson, and nearly every number in machine learning, are points on a line. Pick a point and call it 0. Pick a length and call it 1. Then 1, 2, 3 sit at equal steps to the right of 0, and \(-1\), \(-2\), \(-3\) sit at the same steps to the left. This picture is the number line.

Different jobs need more and more of the line. Counting needs only 1, 2, 3 and so on. Zero and the negative numbers fill in the left-hand side. Fractions fill in the gaps between whole numbers. And even the fractions leave gaps.

The same number line from minus 3 to 3 drawn four times, one above the other. Top, natural numbers N, 1, 2, 3 and so on: blue dots at 1, 2 and 3 only. Second, integers Z, adding 0 and the negative whole numbers: blue dots at every whole number from minus 3 to 3. Third, rational numbers Q, adding every fraction: the whole numbers plus many smaller dots between them, with minus 5 over 2, minus 2 over 3, 3 over 4 and 7 over 3 labelled. Bottom, real numbers R, every point of the line: a solid blue band along the whole line, labelled no gaps, with black dots at the square root of 2, about 1.4142, and pi, about 3.1416.
M01.1.1-F1

Four collections of numbers on one line. Each row keeps every point of the row above and adds more. Only the last row, the real numbers, covers the line with no gaps at all.

Read the picture from the top. The natural numbers are for counting: 1, 2, 3, and so on. The integers add 0 and the negative whole numbers, which you need for a temperature below zero or a score that went down. The rational numbers add every fraction, such as halves and thirds. The real numbers are every point of the line, including points that no fraction reaches, such as \(\sqrt{2}\) and \(\pi\).

Data science meets all four. A count of spam messages is a whole number. A change in a count can be negative. An accuracy score, 3 right out of 4, is a fraction. A height or a temperature is a measurement, a real number.

Build it up

Step 1. Order on the line. Right means bigger. We write \({a < b}\), read “\(a\) is less than \(b\)”, when \(a\) lies to the left of \(b\). So \({-5 < -2}\), because \(-5\) is further left, even though 5 is more than 2.

Step 2. Counting numbers. The natural numbers are \(1, 2, 3, \dots\), and this course does not count 0 among them. A count can still be 0, so counts are the natural numbers together with 0. In Python, range(4) gives 0, 1, 2, 3: code starts counting at 0.

Step 3. Whole numbers on both sides. The integers are \({\dots, -2, -1, 0, 1, 2, \dots}\), every whole number to the left of 0 and to the right of it.

Step 4. Fractions crowd the line. A rational number is one whole number divided by another, such as 3/4 or \(-5/2\). Every integer is one too, since 5 = 5/1. Between any two fractions there is always another one, their average, so fractions crowd every stretch of the line.

Step 5. Some points are not fractions. Draw a square whose four corners sit on the middles of the sides of a 2-by-2 block of unit squares. Its sides cut each unit square in half, and four of those halves make up the tilted square, so its area is 2.

A 2 by 2 block of unit squares standing on the number line between minus 1 and 1. A vermillion square tilted on one corner fills four half squares, each marked one half, with the note four halves of a unit square: area 2. One side of the tilted square runs from 0 to the corner above 1, labelled side, and the note beside it says side times side equals 2. A green arc swings that side down onto the line, where a vermillion dot marks the square root of 2, about 1.4142, between 1 and 2.
M01.1.1-F2

A square of area 2 drawn on a grid. Swing one side down onto the line: it lands at the square root of 2, the number that multiplied by itself gives 2.

Its side is the number that, multiplied by itself, gives 2. We write it \(\sqrt{2}\), read “the square root of 2”; lesson M01.1.2 returns to this sign. Swing the side down onto the line and it lands near 1.4142. It is a point of the line, so it is a real number. Is it a fraction? Try a few:

Fraction Its square Misses 2 by
7/5 49/25 = 1.96 1/25, too small
17/12 289/144 ≈ 2.0069 1/144, too big
41/29 1681/841 ≈ 1.9988 1/841, too small
99/70 9801/4900 ≈ 2.0002 1/4900, too big

The fractions close in, but each square misses 2. No fraction ever lands on it: lesson M01.6.4 proves that \(\sqrt{2}\) is not a fraction. A real number that is not a fraction is called irrational; \(\pi\) is another.

Step 6. Three spellings of one point. A fraction can also be written as a decimal, by dividing, and as a percentage, which means “per hundred”: 75% is 75/100. So 3/4, 0.75 and 75% are one point of the line, spelled three ways.

The formal statement

NoteDefinition: four collections of numbers
  • The natural numbers are \({\mathbb{N} = \{1, 2, 3, \dots\}}\).
  • The integers are \({\mathbb{Z} = \{\dots, -2, -1, 0, 1, 2, \dots\}}\).
  • The rational numbers \(\mathbb{Q}\) are the numbers that can be written as \(p/q\), where \(p\) and \(q\) are integers and \(q\) is not 0.
  • The real numbers \(\R\) are all the points of the number line.

Each collection holds the one before it. A natural number is an integer. An integer \(n\) is the fraction \(n/1\). A fraction is a point of the line. The curly brackets list what a collection holds; lesson M01.6.1 treats such collections, called sets, properly.

The decimal of a fraction either stops, as 3/4 = 0.75 does, or repeats for ever, as 2/3 = 0.666… does. The decimal of an irrational number, such as \({\sqrt{2} = 1.41421356\dots}\), does neither. This is why we write \({\sqrt{2} \approx 1.4142}\) with the sign \({\approx}\), “approximately equals”: the four decimals are a rounding, not the number.

Go deeper: why the decimal of a fraction must stop or repeat

Divide \(p\) by \(q\) by long division. Each step takes the remainder from the step before, multiplies it by 10, divides by \(q\), and writes down one digit and a new remainder. A remainder is always one of \({0, 1, \dots, q - 1}\). If it is ever 0, the decimal stops. If not, there are only \({q - 1}\) possible remainders, so within \(q\) steps one must come round a second time. Each step depends only on the remainder it starts from, so from then on the same digits come back in the same order. Exercise 3 watches this happen for 1/7.

NoteDefinition: absolute value

The absolute value of a number \(x\), written \(\abs{x}\), is its distance from 0 on the number line:

\[ \abs{x} = \begin{cases} x & \text{if } x \geq 0, \\ -x & \text{if } x < 0. \end{cases} \]

The distance between two numbers \(a\) and \(b\) is \({\abs{a - b}}\).

For a negative \(x\), the number \(-x\) is positive: \({\abs{-3} = -(-3) = 3}\). So an absolute value is never negative.

Two number lines from minus 4 to 4. On the top line, titled distance from 0, blue dots at minus 3 and 3 and a black dot at 0, with a vermillion bracket from minus 3 to 0 labelled absolute value of minus 3 equals 3 and a bracket from 0 to 3 labelled absolute value of 3 equals 3. On the bottom line, titled distance between two numbers, blue dots a at minus 2 and b at 3, joined by one vermillion bracket labelled absolute value of 3 minus minus 2 equals absolute value of minus 2 minus 3 equals 5.
M01.1.1-F4

Top: \(-3\) and 3 are both 3 steps from 0. Bottom: the distance from \({a = -2}\) to \({b = 3}\) is 5, whichever of the two is subtracted from the other.

Why \({\abs{a - b}}\) is the distance. If \({a \geq b}\), walking from \(b\) up to \(a\) takes \({a - b}\) steps. That number is 0 or more, so it equals \({\abs{a - b}}\). If \({a < b}\), the walk takes \({b - a}\) steps, and

\[ b - a = -(a - b) = \abs{a - b}, \]

because \({a - b}\) is negative and the absolute value of a negative number is minus that number.

Worked example

Model A gets 3 of 4 questions right. Model B gets 2 of 3 right. Write both accuracies as decimals and percentages, and find how far apart they are.

Two rows of three boxes joined by green arrows. Top row, 3 right out of 4: a blue box 3 over 4, an arrow labelled divide 3 by 4 to a vermillion box 0.75, and an arrow labelled times 100 to a vermillion box 75 percent. Under it the long division: 3 is 3.00, and 30 divided by 4 is 7 remainder 2, then 20 divided by 4 is 5 remainder 0, with the highlighted note remainder 0: it stops. Bottom row, 2 right out of 3: 2 over 3, divided to 0.666 and so on, times 100 to about 66.67 percent. Under it: 20 divided by 3 is 6 remainder 2, then 20 divided by 3 is 6 remainder 2 again, with the highlighted note same remainder: the 6s repeat.
M01.1.1-F3

The two conversions as a trace. Division turns a fraction into a decimal; times 100 turns a decimal into a percentage. The remainders of the long division decide whether the decimal stops.

Model A. Divide 3 by 4. Write 3 as 3.00. Then 30 ÷ 4 is 7 with remainder 2, and 20 ÷ 4 is 5 with remainder 0. The remainder 0 means the decimal stops: 3/4 = 0.75. Times 100 gives 75%.

Model B. Divide 2 by 3. Now 20 ÷ 3 is 6 with remainder 2, and the next step is the same again. The 6s never stop: 2/3 = 0.666…. Times 100 gives 66.666…%, which rounds to 66.67%.

Back again. A percentage is a fraction over 100. Dividing the top and the bottom of 75/100 by 25 gives 3/4, where we started.

How far apart. Put both fractions over 12, the smallest number that 4 and 3 both divide:

\[ \begin{aligned} \frac{3}{4} - \frac{2}{3} &= \frac{9}{12} - \frac{8}{12} \\ &= \frac{1}{12} \approx 0.0833 . \end{aligned} \]

Model A is ahead by \(1/12\), about 8.33 percentage points. On the line the two accuracies are \(1/12\) apart, whichever we subtract from which.

Code

First the conversions by hand. Each step is explained in the comment above it.

# answers the model got right
correct = 3
# questions it was asked
total = 4
# dividing turns the fraction 3/4 into a decimal
decimal = correct / total
# a percentage counts per hundred
percent = decimal * 100
print("fraction:", correct, "/", total)
print("decimal:", decimal)
print("percent:", percent)
fraction: 3 / 4
decimal: 0.75
percent: 75.0

Next, absolute value written with an if, exactly as the definition reads, and distance built on top of it.

def absolute(x):
    """The distance from x to 0 on the number line."""
    # a negative number: flip its sign
    if x < 0:
        return -x
    # zero or positive: it is already a distance
    return x

def distance(a, b):
    """The distance between a and b on the number line."""
    return absolute(a - b)

print(absolute(-3), absolute(3), absolute(0))
print(distance(-2, 3), distance(3, -2))
print(distance(3 / 4, 2 / 3))
3 3 0
5 5
0.08333333333333337

The last line printed 0.08333333333333337, not the 0.08333… of the worked example: the final digits are wrong. Python stores a decimal in binary, with about 16 significant digits. In binary the digits of 2/3, of 1/12 and even of 1/10 never stop, just as the digits of 1/3 never stop in decimal, so Python keeps the nearest number it can hold. Two tools help. The fractions module keeps fractions exact, and math.isclose compares two decimals with a tolerance.

# exact fractions
from fractions import Fraction
# math.isclose lives here
import math

# model A's accuracy, kept exact
a = Fraction(3, 4)
# model B's accuracy
b = Fraction(2, 3)
# the gap is exactly 1/12
print(a, b, a - b)
# Python reduces a fraction to its lowest terms
print(Fraction(75, 100))
# the built-in absolute value
print(abs(-3), abs(3 - 7))

# two decimals that binary cannot store exactly
added = 0.1 + 0.2
print(added)
print(added == 0.3, math.isclose(added, 0.3))
print(Fraction(1, 10) + Fraction(2, 10) == Fraction(3, 10))
3/4 2/3 1/12
3/4
3 4
0.30000000000000004
False True
True

So 0.1 + 0.2 == 0.3 is False, while math.isclose says True: by default it asks only that the two agree to about 9 significant digits, and these two agree to about 16. With exact fractions the sum is exactly 3/10. Finally the table of Step 5, checked with exact fractions:

for p, q in [(7, 5), (17, 12), (41, 29), (99, 70)]:
    # a fraction near the square root of 2
    guess = Fraction(p, q)
    # the fraction, its square, and how far the square is from 2
    print(guess, guess * guess, guess * guess - 2)
7/5 49/25 -1/25
17/12 289/144 1/144
41/29 1681/841 -1/841
99/70 9801/4900 1/4900

Every square misses 2 by exactly 1 over the bottom number squared. Never by 0.

Explore

This module’s interactive, the equation balance, lives in lesson M01.1.3, where equations are solved on a balance. Before you go, try this on paper: mark 3/4, 0.7, \(-1/2\) and 66% on one number line, then put them in order from left to right. (The order is \(-1/2\), 66%, 0.7, 3/4.)

Pitfalls

Two panels, each a number line from 0 to 1 labelled 0, 0.25, 0.5, 0.75 and 1 below and 0, 25, 50, 75 and 100 percent above. The left panel, marked with a cross and titled 0.75 read as 0.75 percent, has a purple dot almost at 0, with the note 0.75 percent is 0.0075, almost 0: not even 1 in 100 right. The right panel, marked with a tick and titled 0.75 is 75 percent, has a vermillion dot at 0.75, under 75 percent, with the note 0.75 and 75 percent are one point: 3 in every 4 right.
M01.1.1-F5

Wrong beside right. A score of 0.75 sits three quarters of the way along; 0.75% sits almost at 0.

“The accuracy is 0.75, so the model is 0.75% right.”

0.75 is 75%. A percentage is the decimal times 100, and 0.75% is 0.0075. Libraries such as scikit-learn report accuracy as a decimal between 0 and 1, so read 0.75 as 75%.

“Accuracy rose from 75% to 80%, so it improved by 5%.”

It rose by 5 percentage points. Measured against where it started, the rise is 5/75, about 6.67%. Say which you mean, because reports of models mix the two all the time.

“\(-5\) is bigger than \(-2\), because 5 is bigger than 2.”

On the line \(-5\) lies to the left of \(-2\), so \({-5 < -2}\). What is bigger is its distance from 0: \({\abs{-5} = 5}\) and \({\abs{-2} = 2}\). A change of \(-5\) is a bigger drop than a change of \(-2\).

“Python says 0.1 + 0.2 == 0.3 is False, so Python cannot add.”

Python adds the stored numbers correctly, but the stored numbers are only the nearest that binary can hold. Never test two decimals with ==; use math.isclose, or Fraction when the answer must be exact. Later courses compare losses and scores the same way.

Check yourself

Recall

1. Which of \(\mathbb{N}\), \(\mathbb{Z}\), \(\mathbb{Q}\) and \(\R\) contain the number \(-3\)?

Show answer

\(\mathbb{Z}\), \(\mathbb{Q}\) and \(\R\). It is not a natural number, because those start at 1.

2. Write 3/4 as a decimal and as a percentage.

Show answer

0.75 and 75%. Divide 3 by 4 to get 0.75, then multiply by 100.

3. What is \({\abs{-7}}\), and what does it measure?

Show answer
  1. It is the distance from \(-7\) to 0 on the number line.

4. Why is \(\sqrt{2}\) a real number but not a rational number?

Show answer

It is a point of the number line, the side of a square of area 2, so it is real. But no fraction multiplied by itself gives exactly 2, so it is not rational. Lesson M01.6.4 proves this.

5. What does 0.1 + 0.2 == 0.3 give in Python, and how should two decimals be compared?

Show answer

False, because both sides are stored only approximately. Compare them with math.isclose.

Exercises

1. ★ Convert 7/8 to a decimal and a percentage by long division. Then write 45% as a fraction in lowest terms.

Show solution

Write 7 as 7.000. Then 70 ÷ 8 is 8 remainder 6, 60 ÷ 8 is 7 remainder 4, and 40 ÷ 8 is 5 remainder 0. The remainder 0 stops the division: 7/8 = 0.875, and times 100 this is 87.5%.

45% is 45/100. Both 45 and 100 divide by 5, which gives 9/20. Nothing larger than 1 divides both 9 and 20, so 9/20 is in lowest terms.

2. ★★ Find the distance between \(-3.5\) and 2, and between 1/3 and 1/2. Then find every number whose distance from 1 is 2.

Show solution

The distance between \(-3.5\) and 2 is \({\abs{2 - (-3.5)} = \abs{5.5} = 5.5}\).

For 1/3 and 1/2, put both over 6: 1/2 is 3/6 and 1/3 is 2/6, so the distance is \({\abs{3/6 - 2/6} = 1/6}\).

A number 2 away from 1 is either 2 steps to the right, at 3, or 2 steps to the left, at \(-1\). Check: \({\abs{3 - 1} = 2}\) and \({\abs{-1 - 1} = \abs{-2} = 2}\). There are exactly two such numbers.

3. ★★★ Divide 1 by 7 by long division and write down every remainder. Find the block of digits that repeats, and explain why it must repeat.

Show solution

Write 1 as 1.000…. The steps are: 10 ÷ 7 is 1 remainder 3; 30 ÷ 7 is 4 remainder 2; 20 ÷ 7 is 2 remainder 6; 60 ÷ 7 is 8 remainder 4; 40 ÷ 7 is 5 remainder 5; 50 ÷ 7 is 7 remainder 1. The remainder 1 is where we started, so the same six steps come round again: 1/7 = 0.142857142857…, with the block 142857 repeating.

It must repeat because a remainder on division by 7 can only be 0, 1, 2, 3, 4, 5 or 6. It is never 0 here, so after at most six steps a remainder comes back. Each step depends only on the remainder it starts from, so from that point the digits come back in the same order.

Summary card

Every real number is a point on one line, and each kind of number fills in more of it.

The anchor figure again: the same number line four times, with the natural numbers, then the integers, then many fractions, and finally the real numbers as a solid band with the square root of 2 and pi marked.
M01.1.1-F1

Counting, whole, fraction, every point.

  • \(\mathbb{N}\), then \(\mathbb{Z}\), then \(\mathbb{Q}\), then \(\R\): each holds the one before.
  • 3/4 = 0.75 = 75%: three spellings of one point.
  • \(\abs{x}\) is the distance from \(x\) to 0, and \({\abs{a - b}}\) the distance between \(a\) and \(b\).
  • \({\sqrt{2} \approx 1.4142}\) is real but not a fraction. Decimals in a computer are approximations: compare them with math.isclose.

Sources and next steps

Sources. Each was opened and checked against this lesson on 12 November 2026.

  • Jay Abramson and others, Algebra and Trigonometry 2e, OpenStax, Section 1.1, Real Numbers: Algebra Essentials. The natural numbers as 1, 2, 3, …, the whole numbers as those with 0, the integers, the rational numbers as quotients \(m/n\) of integers with \(n\) not 0, and the statement that a rational number is a terminating or a repeating decimal while an irrational number is neither. Its Section 2.7 reads absolute value as a distance.
  • Richard Hammack, Book of Proof, third edition. Section 1.1 writes \({\mathbb{N} = \{1, 2, 3, 4, 5, \dots\}}\) without 0, defines \(\mathbb{Q}\) as the fractions \(m/n\) with \({n \neq 0}\) and notes that \(\sqrt{2}\) is real but not rational. Section 6.1 proves that \(\sqrt{2}\) is irrational.
  • Python Software Foundation, Floating-Point Arithmetic: Issues and Limitations, The Python Tutorial, chapter 15 (Python 3.12). It explains that 1/10 cannot be stored exactly as a binary fraction, as 1/3 cannot be written exactly in decimal, and recommends math.isclose for comparing inexact values and the fractions module for exact ones. The fractions and math pages document Fraction, which keeps its numerator and denominator in lowest terms, and isclose, whose default tolerance keeps about 9 decimal digits.
  • scikit-learn developers, sklearn.metrics.accuracy_score. By default it returns the fraction of correctly classified samples, a number between 0 and 1, which is why a reported accuracy of 0.75 means 75%.

Next. M01.1.2 Expressions and the rules of algebra lets a letter stand for a number, and draws the rules of algebra as rectangles.

This lesson unlocks

M01.1.2 Expressions and the rules of algebra

Your progress is saved in this browser only. No account, no tracking.
Back to top