floating-point number
A number with a fractional part stored in binary, which can only approximate most decimal values.
In more detail
Most languages store floats in 64 bits using one common international standard. Because values such as 0.1 have no exact binary form, 0.1 + 0.2 is not exactly 0.3, so compare floats with a tolerance and keep money in whole cents.
Examples
Counting and radix sort do not apply directly to float keys.echo "scale=2; 10 / 3" | bcdouble bad = 0.1 + 0.2; // 0.30000000000000004float64(used) / float64(total) * factorJavaScript numbers are 64-bit floating-point numbers; Float64Array stores them in raw memory.0.1 + 0.2 == 0.3 # FalseSQLite calls this storage class REAL.SELECT 0.1 + 0.2 = 0.3; -- 0 (false)In this course: Because 1.178 cannot be stored exactly, 7 * 1.178 comes out as 8.245999999999999 rather than 8.246. Comparing floats with == is therefore fragile; use pytest.approx in tests, and keep money in whole pence or a decimal type in production code.Related terms
- counting sort — A non-comparison sort for small non-negative integers that counts how many times each value occurs and then writes the values back out in order.
- integer arithmetic — Maths on whole numbers only, which is all bash can do itself: 7 / 2 gives 3, with the remainder dropped.
- bc — A calculator program with arbitrary precision that does the decimal maths bash itself cannot.
- decimal — The C# number type that stores base-10 digits exactly, with 28 to 29 significant digits, which makes it the right type for money.
- significand — The part of a floating-point or decimal number that holds its significant digits, as opposed to the exponent that places the decimal point.
- int — The common integer type in many languages, holding whole numbers within a fixed range set by its size in bits.
- type conversion — Turning a value of one type into another, such as the string "23" into the integer 23.
- integer division — Dividing one whole number by another and keeping only the whole part of the result, so 7 / 2 is 3.
- TypedArray — An array-like view, such as Uint8Array, that reads and writes numbers of one fixed type in an ArrayBuffer.
- sample — One value of a time series at one moment, stored as a timestamp and a floating-point number.
- Prometheus exposition format — The plain-text format a metrics page uses: one sample per line with name, optional labels in braces and a value, plus comment lines for help text and type.
- integer — A whole number with no fractional part, such as 7, 0 or -12, which Python calls int.
- true division — Division with the / operator, which always gives a float, even when the answer is whole: 6 / 3 is 2.0.
- floating-point rounding error — The tiny inaccuracy in float results caused by storing decimal fractions in binary, such as 0.1 + 0.2 giving 0.30000000000000004.
- format specifier — The instructions after a colon inside a replacement field, such as .2f for two decimal places, that control how a value is shown.
- money in cents — The practice of storing money as a whole number of the smallest unit, such as cents, so sums are exact.
- INTEGER — The column type for whole numbers, used in this course for keys, quantities and money stored in cents.
- storage class — In SQLite, the actual kind of a stored value: NULL, INTEGER, REAL, TEXT or BLOB.
- pytest.approx — A pytest helper that compares numbers as equal if they are within a small tolerance of each other.
- decimal module — Python's standard library module for exact decimal arithmetic, used where binary floats would round wrongly, such as money.
- rounding — Turning a number with a fractional part into a whole number by a chosen rule, such as nearest, towards zero or half to even.
- number (type) — The type of ordinary numeric values in JavaScript, both whole numbers and decimals, stored as 64-bit floating-point numbers.
- bigint — A primitive type for whole numbers of any size, written with an n suffix such as 9007199254740993n.
Taught in
- Arrays: Contiguous Memory And Constant-Time Access — Algorithms & Data Structures for Working Engineers
- Counting And Radix Sort: Beating N Log N — Algorithms & Data Structures for Working Engineers
- Arithmetic: Double Parentheses, let And bc — Bash Scripting & Command Line Automation
- The C# Language: Types, Variables, and Flow Control — Modern .NET Core, C# & Enterprise Microservices
- Variables, Typing, and Zero Values — Concurrent Systems Programming with Go
- Binary Data Manipulation: TypedArrays & Endianness — Node.js Internals & Backend Services
- Why A Histogram Beats An Average — Full-Stack Observability: Metrics, Tracing & Logging
- The Prometheus Exposition Format — Full-Stack Observability: Metrics, Tracing & Logging
- Calculating Percentiles With histogram_quantile — Full-Stack Observability: Metrics, Tracing & Logging
- OpenTelemetry: The Vendor Neutral Path — Full-Stack Observability: Metrics, Tracing & Logging
- Types And Functions, A Whirlwind Tour — Hands-on Python: Complete Video Course & Book
- Division, Quotients And Remainders — Hands-on Python: Complete Video Course & Book
- Input, And Numbers Versus Digits — Hands-on Python: Complete Video Course & Book
- Floats, Division And Mixed Types — Hands-on Python: Complete Video Course & Book
- Exponents, Roots And Float Formats — Hands-on Python: Complete Video Course & Book
- Chapter One In One Sitting — Hands-on Python: Complete Video Course & Book
- Data Types And What To Choose — PostgreSQL, Indexing & Relational Data Modeling
- Making Failures Explain Themselves — Automated Testing, TDD & Quality Engineering
- Primitive Types: String, Number, Boolean, Bigint And Symbol — Production TypeScript
- Thirty Questions, One Sneaky Print — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Literals, Names And Four Number Bases — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Operators And Who Binds First — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Print, Input And The Casts Between — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Fundamentals Sprint — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Conditions The Exam Loves — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Dictionaries: Keys, Values And Items — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Twelve Traps, One Shape Each — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Mock, Review And Exam Day — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- math, random And platform — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Modules Sprint — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Methods And The self Parameter — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
References
- Python tutorial: floating-point arithmetic
- GNU bc manual
- Floating-point numeric types
- IEEE 754 standard
- The Go spec: numeric types
- MDN: Number
- Prometheus: exposition formats
- Built-in types: numeric types
- PostgreSQL: floating-point types
- MDN: Number