Scientific Notation
Scientific notation writes very large and very small numbers as "a number between 1 and 10, times a power of ten". It keeps the important digits up front, makes the size obvious at a glance, and is exactly the 1.5e9 style that every programming language accepts.
The form
A number in scientific notation looks like this:
| Ordinary | Scientific | Code |
|---|---|---|
| 4,700,000 | 4.7e6 |
|
| 299,792,458 | 2.99792458e8 |
|
| 0.00052 | 5.2e-4 |
|
| 0.000000001 | 1e-9 |
The exponent counts how many places the decimal point moves. A positive exponent means a big number (move right); a negative one, a small number (move left).
Converting by moving the decimal point
Big number → scientific. Move the point left until one nonzero digit is in front of it, and count the moves:
Small number → scientific. Move the point right until it sits after the first nonzero digit:
Back again. : move the point 5 places right, filling with zeros: 306,000. And : 3 places left: 0.0071.
Python's e format does the conversion for you:
for x in [4_700_000, 299_792_458, 0.00052, 0.000000001]:
print(f"{x:>14} -> {x:.3e}")
print(float("3.06e5"), float("7.1e-3"))
print(2.5e3 == 2500) 4700000 -> 4.700e+06
299792458 -> 2.998e+08
0.00052 -> 5.200e-04
1e-09 -> 1.000e-09
306000.0 0.0071
True
The .3e format rounds the mantissa to 3 decimal places. Notice that Python itself switched 0.000000001 to 1e-09 when printing it plainly: floats that small or large are shown in scientific form automatically.
Calculating in scientific notation
The exponent laws make products and quotients easy: multiply (or divide) the mantissas, add (or subtract) the exponents, then fix the mantissa if it left the range 1 to 10.
Adding needs the same exponent first, just like adding fractions needs a common denominator:
a = 3e4 * 5e7
b = 8e9 / 2e3
c = 3.2e5 + 4.1e4
print(f"{a:.2e} {b:.2e} {c:.3e}")
# How many seconds in a year, and how many nanoseconds?
seconds = 365 * 24 * 60 * 60
print(f"{seconds:,} s = {seconds:.3e} s = {seconds * 1e9:.3e} ns")1.50e+12 4.00e+06 3.610e+05
31,536,000 s = 3.154e+07 s = 3.154e+16 ns
Tip: "A year is about seconds" () is a memorable approximation that is within 0.4% of the real value.
Order of magnitude
The exponent alone tells you the order of magnitude: the rough size of a number. and are both "millions". Engineers compare orders of magnitude to sanity-check designs: if one approach handles requests per second and another , the difference is "three orders of magnitude", a factor of 1000.
In software
- Float literals:
1e-9,6.02e23and1e6work in Python, JavaScript, C, Rust, Java and JSON. They are always floats, so1e6is1000000.0, not the integer1000000. - Tolerances: numerical code compares floats with a small epsilon such as
1e-9instead of==(see Floating Point). - Latency numbers: an L1 cache hit takes about , a disk seek about , a gap of seven orders of magnitude. Rough "numbers every programmer should know" tables are written in this style.
- Machine learning: learning rates like
3e-4and model sizes like7e9parameters are written the same way.
Exercises
Exercise 1: To scientific
Write in scientific notation: 52,000; 0.0009; 6,371,000; 0.0000403.
Answer
, , , .
Solution
Exercise 2: Back to ordinary
Write as ordinary numbers: , , .
Answer
8250, 0.016, 7.
Solution
: move the point 3 right → 8250. : move it 2 left → 0.016. , so .
Exercise 3: Multiply and divide
Compute and in scientific notation.
Answer
and .
Solution
print(f"{2.5e6 * 4e-2:.1e} {9e4 / 3e7:.1e}")1.0e+05 3.0e-03
Exercise 4: Data volume
A service logs events per day, and each event is bytes. How many bytes per day? About how many per year (365 days)?
Answer
bytes per day; about bytes per year.
Solution
day = 1.2e6 * 5e2
print(f"{day:.2e} {day * 365:.3e}")6.00e+08 2.190e+11
Test yourself
Which is 0.00031 in scientific notation?
What type is the Python literal 1e6?
- int
- str
- Decimal
- float
What is ?