TOML 08 ๐ High-Precision Numbers, Infinity, NaN, and Time Zones
TOML’s numeric and temporal types are precise by design, but the precision has limits that are easy to miss. Floats are recommended to be IEEE 754 binary64, but the specification uses “SHOULD,” which means a parser may offer higher precision through a different representation . Infinity and NaN are lowercase-only literals that map to special IEEE values. Dates and times come in four flavors, and only one of them carries a timezone offsetโthe other three are wall-clock values with no location.
This chapter covers the float precision boundary, the inf/nan literals, and the four date/time types with their timezone rules. It explains why tomllib recommends decimal.Decimal for financial data, why +nan and -nan are both just NaN, and why a local date-time is not an instant. The sections that follow walk through numbers and special values, the date/time taxonomy, and the offset rules that distinguish an absolute time from a wall-clock reading.
Key point: TOML floats are binary64 by recommendation, not requirement; inf and nan are lowercase literals; and only offset date-times carry a timezoneโlocal date, local time, and local date-time are unanchored wall-clock values.
a. Float precision and the binary64 recommendation
TOML’s specification says floats “should be implemented as IEEE 754 binary64 values” . The word “should” is the escape hatch. A parser may use a higher-precision representation, and Python’s tomllib exposes this through the parse_float argument, which lets the caller pass a callable that converts the float’s string representation into a different type.
import tomllib
from decimal import Decimal
# Default: binary64 float (may lose precision)
data = tomllib.loads("price = 0.982492")
# Higher precision: parse as Decimal
data = tomllib.loads("price = 0.982492", parse_float=Decimal)
# data["price"] == Decimal("0.982492")
The tomli documentation notes that Decimal is “a practical choice for use cases where float inaccuracies can not be tolerated,” particularly in scientific and financial applications . The precision limit exists because binary64 cannot represent every decimal fraction exactly. A value like 0.1 is stored as an approximation, and arithmetic on approximations accumulates error. Parsing to Decimal preserves the decimal string the author wrote and performs exact decimal arithmetic.
The specification also states that if a value contains greater precision than the implementation can support, the additional precision must be truncated, not rounded . This is a strict rule: a parser that rounds 0.12345678901234567890 to the nearest binary64 instead of truncating is non-compliant. The truncation rule applies to the conversion from decimal text to the implementation’s internal representation, and it ensures that the parsed value never exceeds the author’s stated precision.
b. Infinity and NaN
TOML supports three special float values: inf (positive infinity), +inf (same), -inf (negative infinity), and nan (not a number). The +nan and -nan forms are also accepted . All of these are lowercase only. Inf, NaN, and NAN are not special values; they are bare keys or strings, and a bare key in value position produces a parse error.
# Valid special values
positive_inf = inf
explicit_inf = +inf
negative_inf = -inf
not_a_number = nan
explicit_nan = +nan
signed_nan = -nan
# INVALID: capitalized
# bad = Inf
# bad = NaN
The specification notes that the actual bit pattern of nan (whether it is a signaling NaN or a quiet NaN) is implementation-specific . The +nan and -nan forms are accepted but their expansion is also implementation-specific. This means a TOML file cannot rely on a specific NaN payload surviving a round trip.
c. The four date/time types and timezone rules
TOML defines four temporal types, distinguished by which components are present and whether a timezone offset is included. The offset is the only thing that makes a date-time an absolute instant; without it, the value is a wall-clock reading.
| Type | Example | Timezone |
|---|---|---|
| Offset date-time | 1979-05-27T07:32:00Z | Yes (Z or ยฑHH:MM) |
| Local date-time | 1979-05-27T07:32:00 | No |
| Local date | 1979-05-27 | No |
| Local time | 07:32:00 | No |
The timezone offset is written as Z (or z, case-insensitive) for UTC, or +HH:MM / -HH:MM for a positive or negative offset from UTC . An offset without a sign is invalid; 02:00 is not a timezone. The T separator between date and time may be replaced by a space, and fractional seconds are permitted with millisecond precision required and further precision implementation-specific .
# Offset date-times: absolute instants
utc = 1979-05-27T07:32:00Z
pacific = 1979-05-27T00:32:00-07:00
fractional = 1979-05-27T00:32:00.999999-07:00
# Local date-time: wall clock, no timezone
local_dt = 1979-05-27T07:32:00
# Local date: a day
local_date = 1979-05-27
# Local time: a time of day
local_time = 07:32:00
The Python mapping is direct: offset date-time becomes datetime.datetime with a timezone, local date-time becomes datetime.datetime with tzinfo=None, local date becomes datetime.date, and local time becomes datetime.time. A parser that attaches a timezone to a local date-time is wrong; the absence of an offset is the specification’s way of saying “this is not an instant.”
Complete Example Session
# ============================================
# PART 1: FLOAT PRECISION
# ============================================
pi = 3.14159265358979323846 # truncated to binary64 by parser
price = 0.982492 # parse with Decimal for exactness
# ============================================
# PART 2: SPECIAL VALUES
# ============================================
max_wait = inf
min_wait = -inf
undefined = nan
# ============================================
# PART 3: OFFSET DATE-TIME (ABSOLUTE)
# ============================================
created = 2026-05-05T10:00:00Z
updated = 2026-05-05T03:00:00-07:00
# ============================================
# PART 4: LOCAL DATE-TIME (WALL CLOCK)
# ============================================
alarm = 2026-05-05T07:30:00
# This is 7:30 AM wherever the reader is, not a fixed instant
# ============================================
# PART 5: LOCAL DATE AND TIME
# ============================================
birthday = 1990-01-15
opening = 09:00:00
# ============================================
# PART 6: PYTHON TYPE MAPPING
# ============================================
# created โ datetime(2026, 5, 5, 10, 0, tzinfo=timezone.utc)
# updated โ datetime(2026, 5, 5, 3, 0, tzinfo=timezone(-7h))
# alarm โ datetime(2026, 5, 5, 7, 30) # tzinfo=None
# birthday โ date(1990, 1, 15)
# opening โ time(9, 0)
The parts covered float truncation, the special values, offset date-times as absolute instants, local date-times as wall-clock values, the date and time types, and the Python type mapping.
Quick Reference
Float Rules
| Rule | Detail |
|---|---|
| Recommended type | IEEE 754 binary64 |
| Higher precision | Via parse_float in Python |
| Excess precision | Truncated, not rounded |
| Special values | inf, +inf, -inf, nan |
Date/Time Types
| Type | Components | Timezone |
|---|---|---|
| Offset date-time | Date + time + offset | Yes |
| Local date-time | Date + time | No |
| Local date | Date | No |
| Local time | Time | No |
Offset Syntax
| Form | Meaning |
|---|---|
Z or z | UTC |
+HH:MM | Positive offset from UTC |
-HH:MM | Negative offset from UTC |
| No offset | Local (wall clock) |
Best Practices
โ Do This:
# Use offset date-time for absolute instants
created = 2026-05-05T10:00:00Z # โ
# Use local date-time for recurring wall-clock schedules
alarm = 07:30:00 # โ
# Parse floats as Decimal for financial data
# tomllib.loads(..., parse_float=Decimal) # โ
# Use lowercase special values
timeout = inf # โ
โ Don’t Do This:
# Assume local date-time is UTC
alarm = 2026-05-05T07:30:00 # no timezone, not an instant # โ
# Use capitalized special values
# bad = Inf # โ
# Expect exact decimal precision from binary64
price = 0.1 # binary64 approximation # โ
# Attach a timezone to a local value in code
# (parser should return tzinfo=None) # โ
Common Pitfalls
| Pitfall | Why It Happens | Fix |
|---|---|---|
| Financial rounding errors | binary64 approximation | Parse as Decimal |
Inf rejected | Capitalized | Use inf |
| Local time treated as UTC | No offset in file | Add Z or offset for instants |
| Date comparison fails | Local date is date, not datetime | Check the type after parsing |
Real-World Examples
1. Binary64 float
pi = 3.1415
2. Decimal parse in Python
tomllib.loads("price = 0.982492", parse_float=Decimal)
3. Positive infinity
max = inf
4. Negative infinity
min = -inf
5. NaN
undefined = nan
6. Offset date-time UTC
created = 2026-05-05T10:00:00Z
7. Offset date-time with zone
created = 2026-05-05T03:00:00-07:00
8. Local date-time
alarm = 2026-05-05T07:30:00
9. Local date
birthday = 1990-01-15
10. Local time
opening = 09:00:00
Visual
Float Precision Boundary
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ TOML FLOAT โ
โ โ
โ Text: "0.982492" โ
โ โ โ
โ โผ โ
โ Default parse: binary64 float โ
โ โ approximation (tiny error possible) โ
โ โ
โ parse_float=Decimal โ
โ โ exact decimal "0.982492" โ
โ โ
โ Spec: binary64 is SHOULD, not MUST โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Date/Time Taxonomy
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ DATE/TIME TYPES โ
โ โ
โ Has offset? โ
โ โโโโโโโโโโโโโโโโ โ
โ โ 2026-05-05T โ โ
โ โ 10:00:00Z โโโโโโบ OFFSET DATE-TIME (absolute) โ
โ โโโโโโโโโโโโโโโโ โ
โ โ
โ No offset, has date and time? โ
โ โโโโโโโโโโโโโโโโ โ
โ โ 2026-05-05T โโโโโโบ LOCAL DATE-TIME (wall clock) โ
โ โ 10:00:00 โ โ
โ โโโโโโโโโโโโโโโโ โ
โ โ
โ Date only? โ
โ โโโโโโโโโโโโโโโโ โ
โ โ 2026-05-05 โโโโโโบ LOCAL DATE โ
โ โโโโโโโโโโโโโโโโ โ
โ โ
โ Time only? โ
โ โโโโโโโโโโโโโโโโ โ
โ โ 10:00:00 โโโโโโบ LOCAL TIME โ
โ โโโโโโโโโโโโโโโโ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Timezone Offset Syntax
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ VALID OFFSETS โ
โ โ
โ Z or z โ UTC โ
โ +00:00 โ UTC (explicit) โ
โ +05:30 โ India Standard Time โ
โ -07:00 โ Pacific Daylight Time โ
โ โ
โ INVALID โ
โ 02:00 โ no sign, not a timezone โ
โ +2:00 โ hour must be two digits โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Summary
| Item | Value |
|---|---|
| Float recommendation | IEEE 754 binary64 |
| Higher precision | parse_float=Decimal |
| Excess precision | Truncated |
| Special values | inf, -inf, nan (lowercase) |
| Offset date-time | Has Z or ยฑHH:MM |
| Local date-time | No offset, wall clock |
| Local date/time | Date or time only |
| Offset without sign | Invalid |
Key takeaways:
- Binary64 is a recommendation, not a requirement. The specification uses “SHOULD,” and Python’s
tomllibexposesparse_floatto parse intoDecimalfor exact decimal arithmetic . - Excess float precision must be truncated, not rounded. A parser that rounds a decimal string to the nearest binary64 instead of truncating is non-compliant .
infandnanare lowercase-only. Capitalized forms are not special values. The+nanand-nanforms are accepted but their expansion is implementation-specific .- Only offset date-times are absolute instants. Local date-times, local dates, and local times have no timezone; they are wall-clock values that require context to become instants.
- The offset requires a sign.
ZorยฑHH:MMis a timezone;02:00without a sign is not .
Remember: TOML’s numeric and temporal types are precise in their syntax and bounded in their precision. Floats approximate unless parsed as decimals; infinity and NaN are lowercase literals with implementation-defined bit patterns; and a date-time without an offset is not an instant. When the value mattersโmoney, scientific measurements, cross-timezone schedulingโchoose the representation that preserves it: Decimal for floats, offset date-times for instants, and local types only for values that are genuinely local.
Stop using slow, ad-bloated tool sites! ๐คฎ
๐ Search “KandZ Tools” on Google to use many professional utilities for free.
KandZ.me is the ultimate minimalist hub for:
โ
Finance (Mortgage, Interest, Inflation)
โ
Tech (Base64, JSON, Dev Suite, IP)
โ
Health (BMI, BMR, TDEE)
โ
Productivity (Timer, Workspace, QR)
โก๏ธ Fast & Private
๐ No data leaves your device
๐ 100% Free
๐ Use it now: https://tools.kandz.me
๐ Bookmark itโyouโll need it later!