pub struct Float { /* private fields */ }Expand description
A floating point number in a given format.
A finite value is significand * 2^(exponent - precision + 1). A normal number has its
leading significand bit set, a subnormal does not and has the format’s minimum exponent.
Implementations§
Source§impl Float
impl Float
Sourcepub const fn is_negative(self) -> bool
pub const fn is_negative(self) -> bool
Whether the number is negative, which a zero can be.
Sourcepub const fn is_infinite(self) -> bool
pub const fn is_infinite(self) -> bool
Whether the number is an infinity.
Sourcepub fn parse(text: &str, format: Format) -> Result<(Float, Status), ParseError>
pub fn parse(text: &str, format: Format) -> Result<(Float, Status), ParseError>
Converts a decimal or hexadecimal spelling into the nearest number in format, rounding
to nearest with ties to even.
The spelling is the number alone: no suffix, because the suffix is what chose the format, and no infinity or nan, because C has no spelling for those. A sign is accepted even though a C constant never has one, since the value the constant evaluator folds does. C23 digit separators are stripped here.
§Errors
ParseError, for a spelling that is not a number at all.
Sourcepub fn to_bits(self) -> u128
pub fn to_bits(self) -> u128
The bits of the encoding, in the low Format::width bits.
The x87 format keeps its leading significand bit, so its eightieth bit is the sign and its sixty fourth is the one every other format leaves implied.
Sourcepub fn from_bits(format: Format, bits: u128) -> Float
pub fn from_bits(format: Format, bits: u128) -> Float
Reads a number back out of its encoding, which is what makes Float::to_bits testable
and what a constant folded in the IR is stored as.
A signalling or quiet nan comes back as an infinity, because nothing here makes one and nothing here has anywhere to put the payload yet.
Sourcepub fn to_hex(self) -> String
pub fn to_hex(self) -> String
A hexadecimal spelling that Float::parse turns back into exactly this number.
Hexadecimal rather than decimal, because a hexadecimal constant is exact by construction
and a decimal one is not: printing a number in decimal so that it reads back unchanged
needs a shortest-round-trip algorithm, and printing it in decimal without one silently
changes the program. A printer that changes a constant is worse than a printer whose
output is unfamiliar, so this is 0x1p+0 where a reader would rather see 1.0.
The significand is written as an integer and the exponent scales it, so the spelling is
significand * 2^exponent with no leading digit to argue about. Trailing zero digits are
taken off, which is what makes a round number short.
An infinity has no spelling in C at all. What comes back for one is an exponent past the top of the format, which converts back to an infinity with the overflow that a constant only ever became an infinity by.