Home / Docs / Specification / 29. Numerics
29.7 Text
-
Rendering.
to_text, interpolation, the Una form of spec/11 §5, traces and--jsonoutputs write anF64as follows:- NaN is
nan, and the infinities areinfand-inf; - zero is
0.0, and negative zero-0.0; - another finite value starts with
-when its sign bit is set; its digits are the shortest decimal digit string thatparse_f64reads back to the same bits, and of those, the one nearest the exact value; - with
kthe decimal exponent of its first digit, it is written positionally when-6 <= k <= 20, with at least one digit after the point (1.0,0.1,0.000001,100000000000000000000.0), and otherwise in scientific form, one digit before the point, at least one after it,e, and the exponent with-when negative (1.0e21,1.5e-7,5.0e-324); - no
_and no+.
Rendering is injective on
F64, and every rendering of a finite value is a float literal after its sign. The Una form of a structure writesP(x: nan),P(x: inf),P(x: -inf). - NaN is
-
Parsing.
parse_f64(t)reads exactly one of a number of thenumberproduction of RFC 8259 §6 (an optional-, an integer part without a leading zero, an optional fraction, an optional exponent witheorEand an optional sign),nan,infor-inf. It returns the value nearest the exact decimal value, ties to even, with the sign of the text, so-0gives-0.0. A number beyond the range rounds to an infinity, and one below it to a subnormal or a zero. It reads any number of digits and any exponent correctly. Space is not trimmed. Its failure is labeled asparse_int's is (spec/22 §5). -
Failures.
parse_f64fails withParseError(spec/22 §4):Emptyfor an empty text,NoDigitsfor-alone, and otherwiseNotADigit(at: i), withithe first position, in scalar values, at which no accepted text can continue; a text that ends where an accepted text could still continue fails at its length:Text Failure ""Empty"-"NoDigits"1.","1e","in"NotADigit(at: 2)"1.5e-","-inf "NotADigit(at: 5),NotADigit(at: 4)"+1",".5"," 1"NotADigit(at: 0)"01","--1","-nan"NotADigit(at: 1)"1.0x","nan ","1_0"NotADigit(at: 3),NotADigit(at: 3),NotADigit(at: 1)"Infinity","NaN"NotADigit(at: 0) -
Round trip.
parse_f64(to_text(x)) == xfor everyF64, the canonical NaN included.