Repository navigation
0.5 Numerics
Range • Granularity • Representation
Pick numeric types that match the phenomenon (units, scale, error) and your tooling (Pandas). The right choice prevents silent rounding bugs, invalid stats, and plotting weirdness.
-
- Admissible values
0..360°- non-negative
- money
- Admissible values
-
- smallest meaningful step
- cents
- arc-seconds
- Plank Length
- smallest meaningful step
-
- floating-point (relative error)
- fixed-point (absolute error).
We can encode these as a binary range, with simple 'translation' rules to the real number.
type My_Fixed_Point_Type is delta N range Start .. End;type Angle is 1.0 / (60 * 60) range 0.0 .. 360.0;The Ada view (semantics first)
Ada encourages you to declare problem-specific numeric types with explicit constraints. Core families:
-
Signed integers:
type T is range Start .. End;(bounded, discrete). (Juan Cruz Viotti) -
Modular integers:
type T is mod N;(wraparound, bitwise ops). -
Floating-point:
type T is digits N [range …];(min significant digits, relative error). -
Ordinary fixed-point:
type T is delta Δ range …;(Δ ≈ power-of-two step; max abs error ≤ Δ/2). -
Decimal fixed-point:
type T is delta 0.01 digits 10;(Δ must be 10^−k; exact decimal scaling).
Examples
-- Money with 2 decimals (decimal fixed-point)
type Money is delta 0.01 digits 12; -- exact cents, 10-based scale
-- Angle with arc-second resolution (ordinary fixed-point)
type Angle is delta 1.0 / (60 * 60) range 0.0 .. 360.0;-
Built-ins:
int(arbitrary precision),float(IEEE-754; relative error). -
Exact decimal:
decimal.Decimalwithquantizeto enforce Δ at boundaries of compute (e.g., cents). (Python documentation) -
Pandas dtypes:
Minimal patterns
# A) Decimal with Δ (e.g., cents)
from decimal import Decimal, ROUND_HALF_UP
def to_cent(x): return Decimal(str(x)).quantize(Decimal("0.01"), rounding=ROUND_HALF_UP)
# B) Pandas: nullable ints, Arrow decimal
import pandas as pd, pyarrow as pa
df = pd.DataFrame({"qty": [1, 2, None], "price": ["12.345", "8.1", "0.5"]})
df["qty"] = df["qty"].astype("Int64") # nullable integer
df["price"] = df["price"].map(to_cent).astype(pd.ArrowDtype(pa.decimal128(12, 2))) # precision=12, scale=2Note: arithmetic on Arrow decimals propagates precision/scale; audit downstream ops and re-
quantizeif needed. (Stack Overflow)
| Concept | Ada declaration | Python / Pandas idiom |
|---|---|---|
| Bounded count | type Count is range 0 .. 1_000_000; |
Int64 with validation (e.g., 0 ≤ x ≤ 1e6) |
| Money (2 dp) | delta 0.01 digits N |
Decimal + quantize("0.01"); store as ArrowDtype(pa.decimal128(p,2))
|
| Angle (arc-sec) | delta 1.0/3600 range 0.0 .. 360.0 |
Decimal + quantize("0.0002777778") or float + final rounding; prefer Decimal if exactness matters |
| Bit-masked ints | mod 2**k |
numpy.uint* or int + bit ops; document wrap semantics |
| Scientific real | digits N |
float (IEEE-754), track tolerance; don’t store IDs as float |
-
Choose Δ at the boundary. Apply
quantizeafter multiplies/divides to avoid drift. (Python documentation) -
Use nullable dtypes (
Int64, Arrow decimals) to avoid silent float coercion for missing values. (Pandas) - Make scale explicit in schema (precision/scale for decimals; units for ratio data).
- Don’t average identifiers (never compute means of codes; treat as nominal/ordinal).
# declare schema intent next to the data
NUMERICS = {
"qty": {"dtype": "Int64", "range": (0, None)},
"price": {"arrow": ("decimal128", 12, 2)}, # (type, precision, scale)
"angle": {"decimal_step": "0.0002777778"} # arc-second
}- Ada numeric families and declarations (integers, modular, floating, ordinary & decimal fixed-point).
- Python
decimalandquantizeusage. (Python documentation) - Pandas + PyArrow dtypes (Arrow-backed decimals; nullable integers). (Pandas)
If you like, I can add a tiny validator block (pandera) so your Minion can scream when someone tries to sneak a float into price or a negative into qty.
Legend:
- 🔰 Start here
- 🏵️ Important Insight
- ⚙️ Technical and applications
- 💻 Some Code
- ⚜️ Advanced Topics
- 00.1 What Is Data?
-
00.2 Three Models of Reality (planned)
- 00.2.1 The Linguistic
- 00.2.2 The Symbolic
- 00.3.3 The Mathematical
- 00.3 Maps and Reality
- 00.4 Aside: Hossack, data as structure
- 00.5 Aside: Chomsky, data as evidence
- 01.1 🔰 Sets and Logic (planned)
- 01.2 Sample Space
- 01.3 Randomness and Pattern
- 01.4 Outcomes and Events
- 01.5 Finite and Infinite Sample Spaces
- 02.1 🔰 Functions (planned)
- 02.2 Types Are Alphabets
- 02.3 Random Variables: X : Ω → 𝒳 (planned)
- 02.4 Measurement Scales
- 02.5 Numerics
- 03.1 🔰 Fractions and Proportions (planned)
- 03.2 Randomness vs Uncertainty
- 03.3 Probability: Events, Conditioning, Bayes
- 03.4 Bayes' Theorem, Step by Step
- 03.5 Expected Value and Variance (planned)
- 03.6 Bernoulli (ES)
- 03.7 Discrete Uniform (ES)
- 03.8 🧗 Kolmogorov: Ω, 𝒜, ℙ
- 04.1 🔰 Powers and Logarithms (planned)
- 04.2 Rarity
- 04.3 Information Theory (Shannon)
- 04.4 Entropy: Three Views
- 05.1 🔰 Sums and Averages (planned)
- 05.2 Data, Measured
- 05.3 Sampling (ES)
- 05.4 Population and Sample (ES)
- 05.5 Types of Sampling (ES)
- 05.6 Choosing Summaries & Visuals
- 05.7 Visualization
- 05.8 Plots
- 06.1 What Is Statistics?
- 06.2 🔰 Curves and Area (planned)
- 06.3 Statistical Thinking
- 06.4 Variation
- 06.5 Uncertainty
- 06.6 Parameters vs Estimators (ES)
- 06.7 Distributions
- 06.8 Statistical Modeling
- 06.9 Statistical Methods
- 07.1 Data Storage: Databases (stub)
- 07.2 ER Diagrams
- 07.3 Chen Diagrams
- 07.4 SQL (planned)
- 07.5 Formal Languages and Regular Expressions (planned)
- 07.6 Reproducibility (planned)
08 · μ(x) — Fuzzy - When data is flexible
- 08.1 Fuzzy Logic (planned)
- 08.2 Fuzzy Numbers (planned)
- 08.3 Possibility vs Probability (planned)
09 · x(t) — Signals and systems - Where patterns live
- 09.1 Signals (stub)
- 09.2 Sampling and Aliasing (planned)
- 09.3 Convolution (planned)
- 09.4 Frequency (planned)
- 10.1 🔰 Vectors and Matrices (planned)
- 10.2 Mathematical Modelling (planned)
- 11.1 🔰 Derivatives (planned)
- 11.2 Convex Optimisation (planned)
- 12.1 Supervised Learning (planned)
- 12.2 Data in Language Models
- 13.1 Cause and Effect (planned)
- 13.2 Capstone Guide (planned)