Standardcore
IEEE 754
Also called IEEE 754-2019, IEEE Standard for Floating-Point Arithmetic, ISO/IEC 60559, IEC 559, binary64, binary32, binary16.
IEEE 754 is the standard for floating-point arithmetic. It defines the binary and decimal formats, the rounding rules, the special values and the exceptions that almost all hardware and languages use.
What it is
IEEE 754 is the IEEE Standard for Floating-Point Arithmetic. The current edition is IEEE 754-2019. Its scope text says: "This standard specifies interchange and arithmetic formats and methods for binary and decimal floating-point arithmetic in computer programming environments."1 The same text adds that "numerical results and exceptions are uniquely determined by the values of the input data, sequence of operations, and destination formats"1. So two conforming systems that do the same operations on the same formats give the same bits.
The standard defines five things2:
- Arithmetic formats. These are sets of binary and decimal numbers: finite numbers (with signed zeros and subnormal numbers), infinities and NaN values.
- Interchange formats. These are the bit strings that store a value and move it between systems.
- Rounding rules. These say which representable value replaces an exact result.
- Operations. These are the arithmetic and other operations on the formats.
- Exception handling. These are the indications of conditions such as division by zero and overflow.
Almost every language that has a float type uses an IEEE 754 format for it. A JavaScript number "is a double-precision 64-bit binary format IEEE 754 value, like double in Java or C#"3. The page Floating-point arithmetic explains the idea of a floating-point number. This page is about the standard itself.
Syntax or structure
Formats
A binary format has three fields: a sign bit, a biased exponent and a trailing significand (the fraction). For binary64, the standard specifies "Sign bit: 1 bit", "Exponent: 11 bits" and "Significand precision: 53 bits (52 explicitly stored)"4. The first bit of the significand is not stored for normal numbers, because it is always 1.
The standard defines "five basic formats". There are "three binary floating-point basic formats (encoded with 32, 64 or 128 bits) and two decimal floating-point basic formats (encoded with 64 or 128 bits)"2. It also defines interchange formats of other widths, such as binary16. The table shows the common formats2:
| Name | Common name | Bits | Significand bits | Exponent bits | Largest finite value | Role |
|---|---|---|---|---|---|---|
| binary16 | half precision | 16 | 11 | 5 | 65504 | interchange |
| binary32 | single precision | 32 | 24 | 8 | about 3.40e38 | basic |
| binary64 | double precision | 64 | 53 | 11 | about 1.80e308 | basic |
| binary128 | quadruple precision | 128 | 113 | 15 | about 1.19e4932 | basic |
| decimal64 | 64 | 16 decimal digits | about 1.0e385 | basic | ||
| decimal128 | 128 | 34 decimal digits | about 1.0e6145 | basic |
The exponent field holds the exponent plus a bias. For binary32, "a value of 127 represents the actual exponent zero", and the field values 0 and 255 "are reserved for special numbers (subnormal numbers, signed zeros, infinities, and NaNs)"5. The bias of binary64 is 10234. The encoding of the special values is the same in every binary format:
| Exponent field | Fraction field | Value |
|---|---|---|
| all zeros | zero | +0 or -0, by the sign bit |
| all zeros | not zero | a subnormal number |
| neither all zeros nor all ones | any | a normal number |
| all ones | zero | +infinity or -infinity |
| all ones | not zero | a NaN |
A decimal format stores its significand in one of two encodings. The standard allows "a compressed sequence of decimal digits using densely packed decimal or, alternatively, as a binary integer"2.
Rounding
The standard defines five rounding rules. "Round to nearest, ties to even" is the default for binary formats2. The other rules are round to nearest with ties away from zero, and the three directed roundings toward 0, toward +infinity and toward -infinity2. The page Rounding modes shows them in each language.
The basic operations are correctly rounded. In the words of the summary of the standard, the result "is determined by applying the rounding function on the infinitely precise (mathematical) result", and "Such an operation is said to be correctly rounded"2.
Operations
The required operations of each format include conversions to and from integers, the next and previous values, "add, subtract, multiply, divide, square root, fused multiply–add, remainder, minimum, maximum", conversions to and from strings, sign operations, comparisons and total ordering, classification, and the status flags2.
Two parts of this list surprise programmers:
- Comparisons. "Any comparison with a NaN is treated as unordered", and "−0 and +0 compare as equal"2. The page NaN and infinity shows the consequences.
- totalOrder. This predicate gives a total order. In it, "Negative zero is treated as smaller than positive zero", and NaN sorts after the infinities2.
Exceptions
"The standard defines five exceptions, each of which returns a default value and has a corresponding status flag that is raised when the exception occurs"2:
| Exception | Example | Default result |
|---|---|---|
| invalid operation | the square root of a negative number | a quiet NaN |
| division by zero | 1/0, or log(0) | +infinity or -infinity |
| overflow | a result above the largest finite value | +infinity or -infinity, in the round-to-nearest modes |
| underflow | a result below the smallest normal value | a subnormal number or zero |
| inexact | 1/3 | the correctly rounded result |
By default, an exception does not stop the program. The operation gives the default result, sets a flag and continues. Traps are optional2.
Conversion to text
Conversion between a binary format and a decimal string is a required operation. A conversion to text must be such that the conversion back "will recover the original number"2. The number of significant decimal digits that always recovers the value is 5 for binary16, 9 for binary32, 17 for binary64 and 36 for binary1282.
Examples
The program below reads the fields of three numbers in binary16, binary32 and binary64. The module struct packs a float into each format. For the codes f, d and e, "the packed representation uses the IEEE 754 binary32, binary64 or binary16 format", whatever the native format of the platform is6. The program then unpacks the same bytes as an unsigned integer and cuts the integer into its fields.
import struct
# name: (float code, integer code, exponent bits, fraction bits)
FORMATS = {
"binary16": ("e", "H", 5, 10),
"binary32": ("f", "I", 8, 23),
"binary64": ("d", "Q", 11, 52),
}
def fields(x, name):
code, ucode, ebits, fbits = FORMATS[name]
bits = struct.unpack(">" + ucode, struct.pack(">" + code, x))[0]
sign = bits >> (ebits + fbits)
exponent = (bits >> fbits) & ((1 << ebits) - 1)
fraction = bits & ((1 << fbits) - 1)
return bits, sign, exponent, fraction
print("format value bits s exp fraction")
for name, (_, _, ebits, fbits) in FORMATS.items():
bias = (1 << (ebits - 1)) - 1
digits = (1 + ebits + fbits) // 4
for x in [1.0, -2.5, 0.1]:
bits, s, e, f = fields(x, name)
print(f"{name:8} {x:7} {bits:0{digits}x} {s:>{18 - digits}} {e - bias:3} {f:x}")
print("special values in binary16:")
for x in [65504.0, 2.0 ** -24, float("inf"), -0.0, float("nan")]:
print(f" {x!r:>21} {fields(x, 'binary16')[0]:04x}")
try:
struct.pack(">e", 65520.0)
except OverflowError as err:
print(" 65520.0 does not fit:", type(err).__name__)
format value bits s exp fraction
binary16 1.0 3c00 0 0 0
binary16 -2.5 c100 1 1 100
binary16 0.1 2e66 0 -4 266
binary32 1.0 3f800000 0 0 0
binary32 -2.5 c0200000 1 1 200000
binary32 0.1 3dcccccd 0 -4 4ccccd
binary64 1.0 3ff0000000000000 0 0 0
binary64 -2.5 c004000000000000 1 1 4000000000000
binary64 0.1 3fb999999999999a 0 -4 999999999999a
special values in binary16:
65504.0 7bff
5.960464477539063e-08 0001
inf 7c00
-0.0 8000
nan 7e00
65520.0 does not fit: OverflowError
The output shows the structure of the formats:
- The value 1.0 has the exponent 0 and the fraction 0 in each format. Only the bias changes, so the stored exponent field is 15, 127 or 1023.
- The value 0.1 has no exact binary form. Each format stores the fraction 0x999... rounded to its width, so the last hex digit is
6,dora. The page Floating-point rounding error explains why. - In binary16, 65504 is the largest finite value, 2 to the power -24 is the smallest subnormal value, and infinity, -0 and NaN have the encodings of the table above.
- 65520 is exactly halfway between 65504 and the next power of two, 65536. Ties to even rounds it up to 65536, which does not fit, so the result is infinity. The function
struct.packgives an error instead of infinity.
The example files of this page read the same fields in the other tier-1 languages. Each uses the function of its language that gives the bits of a float.
#include <bit>
#include <cstdint>
#include <cstdio>
#include <initializer_list>
#include <limits>
int main() {
for (double x : {1.0, -2.5, 0.1}) {
const auto b32 = std::bit_cast<std::uint32_t>(static_cast<float>(x));
const auto b64 = std::bit_cast<std::uint64_t>(x);
std::printf("%4.1f binary32 %08x binary64 %016llx\n", x, static_cast<unsigned>(b32),
static_cast<unsigned long long>(b64));
}
const auto yes = [](bool b) { return b ? "yes" : "no"; };
std::printf("is_iec559: float %s, double %s\n", yes(std::numeric_limits<float>::is_iec559),
yes(std::numeric_limits<double>::is_iec559));
std::printf("digits: float %d, double %d\n", std::numeric_limits<float>::digits,
std::numeric_limits<double>::digits);
std::printf("max_exponent: float %d, double %d\n", std::numeric_limits<float>::max_exponent,
std::numeric_limits<double>::max_exponent);
std::printf("max_digits10: float %d, double %d\n", std::numeric_limits<float>::max_digits10,
std::numeric_limits<double>::max_digits10);
}
1.0 binary32 3f800000 binary64 3ff0000000000000
-2.5 binary32 c0200000 binary64 c004000000000000
0.1 binary32 3dcccccd binary64 3fb999999999999a
is_iec559: float yes, double yes
digits: float 24, double 53
max_exponent: float 128, double 1024
max_digits10: float 9, double 17
- Verdict: native. With GCC on x86-64,
floatanddoubleare binary32 and binary64, andis_iec559says so. std::bit_castgives the bits of a float as an unsigned integer of the same size. It is the safe form of areinterpret_castbetween pointers.max_digits10is the number of decimal digits that always gives back the same value: 9 for binary32 and 17 for binary64, the numbers of the standard.- The header
<stdfloat>names the formats directly asstd::float16_t,std::float32_tandstd::float64_t, if the implementation has them.
public class Main {
public static void main(String[] args) {
for (double x : new double[] {1.0, -2.5, 0.1}) {
short b16 = Float.floatToFloat16((float) x);
int b32 = Float.floatToRawIntBits((float) x);
long b64 = Double.doubleToRawLongBits(x);
System.out.printf("%4s binary16 %04x binary32 %08x binary64 %016x%n", x, b16, b32, b64);
}
long b = Double.doubleToRawLongBits(-2.5);
System.out.printf("-2.5: sign %d, exponent %d, fraction %x%n",
b >>> 63, ((b >>> 52) & 0x7ff) - 1023, b & 0xfffffffffffffL);
for (float f : new float[] {0.1f, 65504f, 65520f}) {
System.out.println("binary16 of " + f + ": " + Float.float16ToFloat(Float.floatToFloat16(f)));
}
}
}
1.0 binary16 3c00 binary32 3f800000 binary64 3ff0000000000000
-2.5 binary16 c100 binary32 c0200000 binary64 c004000000000000
0.1 binary16 2e66 binary32 3dcccccd binary64 3fb999999999999a
-2.5: sign 1, exponent 1, fraction 4000000000000
binary16 of 0.1: 0.099975586
binary16 of 65504.0: 65504.0
binary16 of 65520.0: Infinity
- Verdict: native. Java
floatis binary32 anddoubleis binary64. Float.floatToRawIntBitsandDouble.doubleToRawLongBitsgive the bits. The "raw" forms keep the bits of a NaN as they are.- Java has no binary16 type.
Float.floatToFloat16rounds afloatto binary16 with ties to even and stores the 16 bits in ashort.Float.float16ToFloatconverts the bits back. - The format
%xprints a negativeshortas an unsigned 16-bit value, so the bits of -2.5 print asc100.
#include <float.h>
#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
static uint32_t bits32(float x) {
uint32_t b;
memcpy(&b, &x, sizeof b);
return b;
}
static uint64_t bits64(double x) {
uint64_t b;
memcpy(&b, &x, sizeof b);
return b;
}
int main(void) {
const double xs[] = {1.0, -2.5, 0.1};
for (int i = 0; i < 3; i++) {
printf("%4.1f binary32 %08" PRIx32 " binary64 %016" PRIx64 "\n",
xs[i], bits32((float)xs[i]), bits64(xs[i]));
}
uint64_t b = bits64(-2.5);
printf("-2.5: sign %" PRIu64 ", exponent %d, fraction %" PRIx64 "\n",
b >> 63, (int)((b >> 52) & 0x7ff) - 1023, b & ((UINT64_C(1) << 52) - 1));
printf("FLT_RADIX %d, FLT_MANT_DIG %d, DBL_MANT_DIG %d\n", FLT_RADIX, FLT_MANT_DIG, DBL_MANT_DIG);
printf("FLT_MAX_EXP %d, DBL_MAX_EXP %d\n", FLT_MAX_EXP, DBL_MAX_EXP);
printf("DBL_DECIMAL_DIG %d\n", DBL_DECIMAL_DIG);
return 0;
}
1.0 binary32 3f800000 binary64 3ff0000000000000
-2.5 binary32 c0200000 binary64 c004000000000000
0.1 binary32 3dcccccd binary64 3fb999999999999a
-2.5: sign 1, exponent 1, fraction 4000000000000
FLT_RADIX 2, FLT_MANT_DIG 24, DBL_MANT_DIG 53
FLT_MAX_EXP 128, DBL_MAX_EXP 1024
DBL_DECIMAL_DIG 17
- Verdict: native. On common platforms, C
floatis binary32 anddoubleis binary64. The C standard allows other formats, and the macros of<float.h>describe the format that the compiler uses. memcpycopies the bytes of the float into an integer of the same width. A pointer cast betweendouble *anduint64_t *breaks the aliasing rules, butmemcpyis safe.FLT_MANT_DIGandDBL_MANT_DIGcount the significand bits with the hidden bit, so they are 24 and 53.DBL_MAX_EXPis the largest integer e such that 2 to the power e - 1 is a finitedouble, so it is 1024, one more than the largest exponent of binary64.DBL_DECIMAL_DIGis 17, the number of decimal digits that always gives back the samedouble.
using System;
using System.Globalization;
CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
foreach (double x in new[] { 1.0, -2.5, 0.1 })
{
ushort b16 = BitConverter.HalfToUInt16Bits((Half)x);
uint b32 = BitConverter.SingleToUInt32Bits((float)x);
ulong b64 = BitConverter.DoubleToUInt64Bits(x);
Console.WriteLine($"{x,4} binary16 {b16:x4} binary32 {b32:x8} binary64 {b64:x16}");
}
ulong b = BitConverter.DoubleToUInt64Bits(-2.5);
Console.WriteLine($"-2.5: sign {b >> 63}, exponent {(int)((b >> 52) & 0x7ff) - 1023}, fraction {b & ((1UL << 52) - 1):x}");
Console.WriteLine($"Half.MaxValue: {Half.MaxValue}, as double: {(double)Half.MaxValue}");
Console.WriteLine($"(Half)0.1 as double: {(double)(Half)0.1}");
Console.WriteLine($"(Half)65520.0: {(Half)65520.0}");
1 binary16 3c00 binary32 3f800000 binary64 3ff0000000000000
-2.5 binary16 c100 binary32 c0200000 binary64 c004000000000000
0.1 binary16 2e66 binary32 3dcccccd binary64 3fb999999999999a
-2.5: sign 1, exponent 1, fraction 4000000000000
Half.MaxValue: 65500, as double: 65504
(Half)0.1 as double: 0.0999755859375
(Half)65520.0: Infinity
- Verdict: native. C#
floatanddoubleare binary32 and binary64, andSystem.Halfis binary16. BitConverter.HalfToUInt16Bits,SingleToUInt32BitsandDoubleToUInt64Bitsgive the bits as unsigned integers.Half.MaxValueprints as 65500, becauseToStringgives the shortest text that converts back to the sameHalf. As adouble, the value is 65504.- The program sets the invariant culture, so the text of infinity is the same on every machine.
function bits32(x) {
const view = new DataView(new ArrayBuffer(4));
view.setFloat32(0, x);
return view.getUint32(0);
}
function bits64(x) {
const view = new DataView(new ArrayBuffer(8));
view.setFloat64(0, x);
return view.getBigUint64(0);
}
for (const x of [1.0, -2.5, 0.1]) {
const b32 = bits32(x).toString(16).padStart(8, "0");
const b64 = bits64(x).toString(16).padStart(16, "0");
console.log(`${String(x).padStart(4)} binary32 ${b32} binary64 ${b64}`);
}
const b = bits64(-2.5);
const exponent = Number((b >> 52n) & 0x7ffn) - 1023;
const fraction = (b & ((1n << 52n) - 1n)).toString(16);
console.log(`-2.5: sign ${b >> 63n}, exponent ${exponent}, fraction ${fraction}`);
console.log("Math.fround(0.1):", Math.fround(0.1));
console.log("Math.f16round(0.1):", Math.f16round(0.1));
console.log("Math.f16round(65504):", Math.f16round(65504));
console.log("Math.f16round(65520):", Math.f16round(65520));
1 binary32 3f800000 binary64 3ff0000000000000
-2.5 binary32 c0200000 binary64 c004000000000000
0.1 binary32 3dcccccd binary64 3fb999999999999a
-2.5: sign 1, exponent 1, fraction 4000000000000
Math.fround(0.1): 0.10000000149011612
Math.f16round(0.1): 0.0999755859375
Math.f16round(65504): 65504
Math.f16round(65520): Infinity
- Verdict: native. A JavaScript number is a binary64 value.
DataViewwrites a number into bytes as binary32 or binary64, and reads the same bytes back as an unsigned integer. The default byte order ofDataViewis big-endian, so the hex digits are in the order of the standard: sign, exponent, fraction.Math.froundandMath.f16roundround a number to binary32 and binary16, and return the result as a binary64 number again. The value of 0.1 in binary16 has only 11 significant bits, so it is 0.0999755859375.- 65520 is halfway between the largest binary16 value, 65504, and 65536. Ties to even goes to 65536, which is too large, so the result is
Infinity.
package main
import (
"fmt"
"math"
)
func main() {
for _, x := range []float64{1.0, -2.5, 0.1} {
fmt.Printf("%4.1f binary32 %08x binary64 %016x\n", x, math.Float32bits(float32(x)), math.Float64bits(x))
}
b := math.Float64bits(-2.5)
fmt.Printf("-2.5: sign %d, exponent %d, fraction %x\n", b>>63, int(b>>52&0x7ff)-1023, b&(1<<52-1))
fmt.Println("MaxFloat32:", math.MaxFloat32)
fmt.Println("MaxFloat64:", math.MaxFloat64)
fmt.Println("FMA(0.1, 10, -1):", math.FMA(0.1, 10, -1))
}
1.0 binary32 3f800000 binary64 3ff0000000000000
-2.5 binary32 c0200000 binary64 c004000000000000
0.1 binary32 3dcccccd binary64 3fb999999999999a
-2.5: sign 1, exponent 1, fraction 4000000000000
MaxFloat32: 3.4028234663852886e+38
MaxFloat64: 1.7976931348623157e+308
FMA(0.1, 10, -1): 5.551115123125783e-17
- Verdict: native. Go
float32andfloat64are binary32 and binary64. math.Float32bitsandmath.Float64bitsgive the bits, andmath.Float32frombitsandmath.Float64frombitsdo the reverse.math.FMAis the fused multiply-add of the standard. It rounds0.1*10 - 1one time, so the result shows the error of the stored 0.1, which is about 5.55e-17.
fn main() {
for x in [1.0f64, -2.5, 0.1] {
println!("{:>4} binary32 {:08x} binary64 {:016x}", x, (x as f32).to_bits(), x.to_bits());
}
let b = (-2.5f64).to_bits();
println!(
"-2.5: sign {}, exponent {}, fraction {:x}",
b >> 63,
((b >> 52) & 0x7ff) as i64 - 1023,
b & ((1u64 << 52) - 1)
);
println!("MANTISSA_DIGITS: f32 {}, f64 {}", f32::MANTISSA_DIGITS, f64::MANTISSA_DIGITS);
println!("MAX_EXP: f32 {}, f64 {}", f32::MAX_EXP, f64::MAX_EXP);
println!("0.1.mul_add(10.0, -1.0): {:e}", 0.1f64.mul_add(10.0, -1.0));
}
1 binary32 3f800000 binary64 3ff0000000000000
-2.5 binary32 c0200000 binary64 c004000000000000
0.1 binary32 3dcccccd binary64 3fb999999999999a
-2.5: sign 1, exponent 1, fraction 4000000000000
MANTISSA_DIGITS: f32 24, f64 53
MAX_EXP: f32 128, f64 1024
0.1.mul_add(10.0, -1.0): 5.551115123125783e-17
- Verdict: native. Rust
f32andf64are binary32 and binary64. - The method
to_bitsgives the bits, andfrom_bitsdoes the reverse. MANTISSA_DIGITScounts the hidden bit, so it is 24 and 53.MAX_EXPuses the same convention as C, so it is one more than the largest exponent.mul_addis the fused multiply-add of the standard, with one rounding.
Versions
The standard has three editions21:
| Edition | Published | Main changes |
|---|---|---|
| IEEE 754-1985 | 1985 | binary formats only: single, double and extended precision |
| IEEE 754-2008 | August 2008 | decimal formats, binary16 and binary128, fused multiply-add, the merge of IEEE 854-1987 |
| IEEE 754-2019 | July 2019 | clarifications, defect fixes and new recommended operations |
Before 1985, each computer maker had its own float format. The summary of the history says: "IBM used a hexadecimal floating-point format with seven bits always used for the exponent regardless of precision", and "Exception processing from divide-by-zero was different on different computers"2. The Intel 8087, "which was announced in 1980, was the first chip to implement the draft standard"7. William Kahan of the University of California drew up the floating-point specifications for the 8087. He then put forward a draft for the IEEE working group "based on his work for their coprocessor", written with Jerome Coonen and Harold Stone7. This "Kahan-Coonen-Stone proposal" brought infinities, NaN values and denormal (subnormal) numbers into the standard7.
The 2008 edition "added decimal arithmetic and formats" and "merged in IEEE 854 (the radix-independent floating-point standard)"8. Its new operations "include fused multiply–add (FMA), explicit conversions, classification predicates (isNan(x), etc.), various min and max functions, a total ordering predicate, and two decimal-specific operations"8. The binary16 type also comes from this edition: it "was introduced in the 2008 revision of the IEEE 754 standard"6. It has a sign bit, 5 exponent bits and 11 significand bits, and its largest value is 655049. "Hardware and software for machine learning and neural networks often use half-precision arithmetic to accelerate computations"9.
The 2019 edition is "a minor revision of the previous version, incorporating mainly clarifications, defect fixes and new recommended operations"2. The IEEE board approved it on 2019-06-13, and the IEEE published it on 2019-07-221. Its status is "Superseding 754-2008"1. The international standard ISO/IEC 60559:2020 has the same content as IEEE 754-20192. Older language standards cite the earlier names. For example, the C# documentation says that "The Double type complies with the IEC 60559:1989 (IEEE 754) standard for binary floating-point arithmetic"10.
Pitfalls
- A short decimal string does not always give back the same float. Only 17 significant digits are always sufficient for binary642. With 15 digits, the value of 0.1 + 0.2 changes. The last line shows that
reprgives the 17 digits for this value:
This code runs but gives a wrong answer on purpose.
x = 0.1 + 0.2
print(f"{x:.15g}", float(f"{x:.15g}") == x)
print(f"{x:.17g}", float(f"{x:.17g}") == x)
print(repr(x))
0.3 False
0.30000000000000004 True
0.30000000000000004
- Conformance is not the same in every library. The basic operations are correctly rounded, but functions such as
sinandexpare only recommended. A summary of the standard notes that the C mathematical functions are usually not correctly rounded2. So the same program can print different last digits on two systems. - Extended precision changes results. Some compilers keep intermediate results in a wider format, such as the x87 80-bit format. The result is still IEEE 754 arithmetic, but it differs from the result in binary642.
- The decimal type of a language is often not an IEEE 754 decimal format. The C#
decimalis "128-bits consisting of a 96-bit integer number, and a 32-bit set of flags"11. This is not decimal128. The Python moduledecimalhas the functionIEEEContext, which returns a context "initialized to the proper values for one of the IEEE interchange formats"12. - A NaN payload is not portable. The standard has "no requirement to preserve the payload of a quiet NaN or signaling NaN" in a conversion to text2. Do not store data in the bits of a NaN.
Tools and libraries
Each tier-1 language has a function that gives the bits of a float. These functions are the base of the example files:
- Python: the module
struct, with the codese,fandd6. - JavaScript:
DataViewand the typed arrays, andMath.f16round, which "returns the nearest 16-bit half precision float representation of a number"13. - Java:
Float.floatToRawIntBits,Double.doubleToRawLongBits, andFloat.floatToFloat16, which "Returns the floating-point binary16 value, encoded in a short", that is nearest to the argument14. - C and C++:
memcpyorstd::bit_castto an integer of the same width. In C++,std::numeric_limits<T>::is_iec559"is true for all floating-point types T which fulfill the requirements of IEC 559 (IEEE 754) standard"15. The header<stdfloat>addsstd::float16_t,std::float32_t,std::float64_tandstd::float128_tif the implementation supports them16. - C#:
BitConverter.DoubleToInt64Bits, and the typeSystem.Half, which "is an IEEE 754-compliant float16 type"17. - Go:
math.Float32bitsandmath.Float64bits, which return "the IEEE 754 binary representation"18. - Rust: the method
to_bits. The typef64is "the “binary64” type defined in IEEE 754-2008"19. - SQL: in PostgreSQL, the types
realanddouble precision"are implementations of IEEE Standard 754 for Binary Floating-Point Arithmetic (single and double precision, respectively)" on the supported platforms20.
References
- IEEE 754-2019, IEEE Standard for Floating-Point Arithmetic, IEEE Standards Association, the scope text, Status, PAR Approval, Board Approval and History
- IEEE 754, Wikipedia, the lead; History; Basic and interchange formats; Rounding rules; Required operations; Comparison predicates; Total-ordering predicate; Exception handling; Character representation
- Number, MDN Web Docs, Number encoding
- Double-precision floating-point format, Wikipedia, IEEE 754 double-precision binary floating-point format, binary64; Exponent encoding
- Single-precision floating-point format, Wikipedia, IEEE 754 standard, binary32
- struct, Interpret bytes as packed binary data, Python Software Foundation, Format Characters, notes 6 and 7
- IEEE 754-1985, Wikipedia, History
- IEEE 754-2008 revision, Wikipedia, the lead; Summary of the revisions; Clause 5 Operations
- Half-precision floating-point format, Wikipedia, IEEE 754 half-precision binary floating-point format, binary16; Uses of half precision
- Double Struct, Microsoft Learn, Remarks
- Decimal Struct, Microsoft Learn, Remarks
- decimal, Decimal fixed-point and floating-point arithmetic, Python Software Foundation, the function IEEEContext
- Math.f16round(), MDN Web Docs, Description; Examples
- Float (Java SE 27 and JDK 27), Oracle, the methods floatToRawIntBits, floatToFloat16 and float16ToFloat
- std::numeric_limits<T>::is_iec559, cppreference.com, the description
- Fixed width floating-point types, cppreference.com, the table of types; Notes
- Half Struct, Microsoft Learn, Remarks
- math package, The Go Authors, func Float32bits, func Float64bits, and func FMA
- Primitive Type f64, The Rust Project, the description; the constant MANTISSA_DIGITS; the method to_bits
- PostgreSQL Documentation, 8.1. Numeric Types, The PostgreSQL Global Development Group, 8.1.3 Floating-Point Types
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W3 standard |
|
Tag 4TZD91 Edit this page History Report an error Markdown