CINXE.COM
IPLD ♦
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="/css/layout.css?1732633144785"> <link rel="stylesheet" href="/css/nav.css?1732633144785"> <link rel="stylesheet" href="/css/style.css?1732633144785"> <link rel="stylesheet" href="/css/prismjs@1.24-themes-prism.css"> <title>IPLD ♦ </title> </head> <body> <header> <div class="sidebar-button" onclick="document.getElementById('sidebar').classList.toggle('sidebar-open')"> <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"> <path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path> </svg> </div> <a href="/" class="logo">IPLD</a> <aside id=breadcrumbs> <ul> <li><a href="/design">design</a></li> <li><a href="/design/tricky-choices">tricky-choices</a></li> <li><a href="/design/tricky-choices/numeric-domain/">numeric-domain</a></li> </ul> </aside> </header> <aside id=sidebar> <nav> <ul> <li> <a href="/docs/">Docs</a><ul> <li> <a href="/docs/intro/">Intro</a><ul> <li> <a href="/docs/intro/hello-world/">Hello, World</a></li> <li> <a href="/docs/intro/primer/">The Brief Primer</a></li> <li> <a href="/docs/intro/ecosystem/">InterPlanetary Ecosystem Overview</a></li> <li> <a href="/docs/intro/community/">Finding Community</a></li></ul></li> <li> <a href="/docs/motivation/">Motivation</a><ul> <li> <a href="/docs/motivation/benefits-of-content-addressing/">Benefits of Content Addressing</a></li> <li> <a href="/docs/motivation/data-to-data-structures/">From Data to Data Structures</a></li></ul></li> <li> <a href="/docs/codecs/">Codecs</a><ul> <li> <a href="/docs/codecs/known/">Known Codecs</a><ul> <li> <a href="/docs/codecs/known/dag-cbor/">DAG-CBOR</a></li> <li> <a href="/docs/codecs/known/dag-json/">DAG-JSON</a></li> <li> <a href="/docs/codecs/known/dag-pb/">DAG-PB</a></li></ul></li></ul></li> <li> <a href="/docs/data-model/">Data Model</a><ul> <li> <a href="/docs/data-model/node/">Nodes</a></li> <li> <a href="/docs/data-model/kinds/">Kinds</a></li> <li> <a href="/docs/data-model/pathing/">Pathing</a></li> <li> <a href="/docs/data-model/traversal/">Traversal</a></li></ul></li> <li> <a href="/docs/advanced-data-layouts/">Advanced Data Layouts</a><ul> <li> <a href="/docs/advanced-data-layouts/intro/">Intro to ADLs</a></li> <li> <a href="/docs/advanced-data-layouts/naming/">ADL Naming</a></li> <li> <a href="/docs/advanced-data-layouts/signalling/">Signalling ADLs</a></li> <li> <a href="/docs/advanced-data-layouts/dynamic-loading/">Dynamic Loading</a></li> <li> <a href="/docs/advanced-data-layouts/known/">Known ADLs</a></li></ul></li> <li> <a href="/docs/schemas/">Schemas</a><ul> <li> <a href="/docs/schemas/intro/">Introduction</a><ul> <li> <a href="/docs/schemas/intro/compare/">compare</a></li> <li> <a href="/docs/schemas/intro/goals/">Goals</a></li> <li> <a href="/docs/schemas/intro/feature-summary/">Feature Summary</a></li></ul></li> <li> <a href="/docs/schemas/features/">Features</a><ul> <li> <a href="/docs/schemas/features/typekinds/">Type Kinds</a></li> <li> <a href="/docs/schemas/features/representation-strategies/">Representation Strategies</a></li> <li> <a href="/docs/schemas/features/links/">Links</a></li> <li> <a href="/docs/schemas/features/indicating-adls/">Using ADLs in Schemas</a></li></ul></li> <li> <a href="/docs/schemas/using/">Using Wisely</a><ul> <li> <a href="/docs/schemas/using/authoring-guide/">Authoring Guide</a></li> <li> <a href="/docs/schemas/using/migrations/">Migrations</a></li></ul></li></ul></li> <li> <a href="/docs/synthesis/">Synthesis</a><ul> <li> <a href="/docs/synthesis/gtd/">Getting Things Done</a></li> <li> <a href="/docs/synthesis/building-in-alignment/">Building in Alignment</a></li> <li> <a href="/docs/synthesis/how-ipfs-web-gateways-work/">How IPFS Web Gateways Work</a></li> <li> <a href="/docs/synthesis/encryption/">Working With Encryption</a></li></ul></li></ul></li> <li> <a href="/specs/">Specs</a><ul> <li> <a href="/specs/about/">About the Specifications</a></li> <li> <a href="/specs/codecs/">Codecs</a><ul> <li> <a href="/specs/codecs/dag-cbor/">DAG-CBOR</a><ul> <li> <a href="/specs/codecs/dag-cbor/spec/">Spec</a></li> <li> <a href="/specs/codecs/dag-cbor/fixtures/">DAG-CBOR Test Fixtures</a><ul> <li> <a href="/specs/codecs/dag-cbor/fixtures/cross-codec/">cross-codec</a></li></ul></li></ul></li> <li> <a href="/specs/codecs/dag-cosmos/">DAG-COSMOS</a><ul> <li> <a href="/specs/codecs/dag-cosmos/basic_types/">basic_types</a></li> <li> <a href="/specs/codecs/dag-cosmos/cosmos_state/">cosmos_state</a></li> <li> <a href="/specs/codecs/dag-cosmos/crypto_types/">crypto_types</a></li> <li> <a href="/specs/codecs/dag-cosmos/tendermint_chain/">tendermint_chain</a></li> <li> <a href="/specs/codecs/dag-cosmos/typed_protobuf/">typed_protobuf</a></li></ul></li> <li> <a href="/specs/codecs/dag-eth/">DAG-ETH</a><ul> <li> <a href="/specs/codecs/dag-eth/basic_types/">basic_types</a></li> <li> <a href="/specs/codecs/dag-eth/chain/">chain</a></li> <li> <a href="/specs/codecs/dag-eth/convenience_types/">convenience_types</a></li> <li> <a href="/specs/codecs/dag-eth/state/">state</a></li></ul></li> <li> <a href="/specs/codecs/dag-jose/">DAG-JOSE</a><ul> <li> <a href="/specs/codecs/dag-jose/spec/">Spec</a></li> <li> <a href="/specs/codecs/dag-jose/fixtures/">fixtures</a></li></ul></li> <li> <a href="/specs/codecs/dag-json/">DAG-JSON</a><ul> <li> <a href="/specs/codecs/dag-json/spec/">Spec</a></li> <li> <a href="/specs/codecs/dag-json/fixtures/">DAG-JSON Test Fixtures</a><ul> <li> <a href="/specs/codecs/dag-json/fixtures/cross-codec/">cross-codec</a></li></ul></li></ul></li> <li> <a href="/specs/codecs/dag-pb/">DAG-PB</a><ul> <li> <a href="/specs/codecs/dag-pb/spec/">Spec</a></li> <li> <a href="/specs/codecs/dag-pb/fixtures/">DAG-PB Test Fixtures</a><ul> <li> <a href="/specs/codecs/dag-pb/fixtures/cross-codec/">cross-codec</a></li></ul></li></ul></li></ul></li> <li> <a href="/specs/advanced-data-layouts/">Advanced Data Layouts</a><ul> <li> <a href="/specs/advanced-data-layouts/fbl/">FBL ADL</a><ul> <li> <a href="/specs/advanced-data-layouts/fbl/spec/">spec</a></li></ul></li> <li> <a href="/specs/advanced-data-layouts/hamt/">HAMT ADL</a><ul> <li> <a href="/specs/advanced-data-layouts/hamt/spec/">spec</a></li> <li> <a href="/specs/advanced-data-layouts/hamt/fixture/">HashMap (HAMT) Test Fixtures</a><ul> <li> <a href="/specs/advanced-data-layouts/hamt/fixture/alice-words/">alice-words</a></li></ul></li></ul></li></ul></li> <li> <a href="/specs/schemas/">Schemas</a><ul> <li> <a href="/specs/schemas/prelude/">prelude</a></li></ul></li> <li> <a href="/specs/transport/">Transports</a><ul> <li> <a href="/specs/transport/car/">CAR</a><ul> <li> <a href="/specs/transport/car/carv1/">CARv1 Specification</a></li> <li> <a href="/specs/transport/car/carv2/">CARv2 Specification</a></li> <li> <a href="/specs/transport/car/fixture/">CAR Test Fixtures</a><ul> <li> <a href="/specs/transport/car/fixture/carv1-basic/">carv1-basic</a></li> <li> <a href="/specs/transport/car/fixture/carv2-basic/">carv2-basic</a></li></ul></li></ul></li> <li> <a href="/specs/transport/graphsync/">Graphsync</a><ul> <li> <a href="/specs/transport/graphsync/known_extensions/">known_extensions</a></li></ul></li> <li> <a href="/specs/transport/trustless-pathing/">Trustless Pathing</a><ul> <li> <a href="/specs/transport/trustless-pathing/fixtures/">Trustless Pathing Fixtures</a><ul> <li> <a href="/specs/transport/trustless-pathing/fixtures/unixfs_20m_variety/">unixfs_20m_variety</a></li></ul></li></ul></li></ul></li> <li> <a href="/specs/selectors/">Selectors</a><ul> <li> <a href="/specs/selectors/fixtures/">fixtures</a><ul> <li> <a href="/specs/selectors/fixtures/selector-fixtures-1/">selector-fixtures-1</a></li> <li> <a href="/specs/selectors/fixtures/selector-fixtures-adl/">selector-fixtures-adl</a></li> <li> <a href="/specs/selectors/fixtures/selector-fixtures-recursion/">selector-fixtures-recursion</a></li></ul></li></ul></li> <li> <a href="/specs/patch/">Patch</a><ul> <li> <a href="/specs/patch/fixtures/">IPLD Patch Test Fixtures</a><ul> <li> <a href="/specs/patch/fixtures/fixtures-1/">fixtures-1</a></li></ul></li></ul></li></ul></li> <li> <a href="/libraries/">Libraries</a><ul> <li> <a href="/libraries/golang/">Golang</a></li> <li> <a href="/libraries/javascript/">JavaScript</a></li> <li> <a href="/libraries/python/">Python</a></li> <li> <a href="/libraries/rust/">Rust</a></li></ul></li> <li> <a href="/design/">Design</a><ul> <li> <a href="/design/objectives/">Objectives</a></li> <li> <a href="/design/concepts/">Concepts</a><ul> <li> <a href="/design/concepts/type-theory-glossary/">type-theory-glossary</a></li></ul></li> <li> <a href="/design/libraries/">Libraries</a><ul> <li> <a href="/design/libraries/nodes-and-kinds/">nodes-and-kinds</a></li></ul></li> <li> <a href="/design/tricky-choices/">Tricky Choices</a><ul> <li> <a href="/design/tricky-choices/dag-pb-forms-impl-and-use/">dag-pb-forms-impl-and-use</a></li> <li> <a href="/design/tricky-choices/map-key-domain/">map-key-domain</a></li> <li class="active-page"> <a href="/design/tricky-choices/numeric-domain/">numeric-domain</a></li> <li> <a href="/design/tricky-choices/ordering/">ordering</a></li> <li> <a href="/design/tricky-choices/string-domain/">string-domain</a></li></ul></li> <li> <a href="/design/open-research/">Open Research</a><ul> <li> <a href="/design/open-research/ADL-autoexecution/">ADL autoexecution</a></li></ul></li></ul></li> <li> <a href="/tools/">Tools</a></li> <li> <a href="/glossary/">Glossary</a></li> <li> <a href="/media/">Media</a></li> <li> <a href="/FAQ/">FAQ</a></li></ul> </nav> </aside> <main> <div class=content> <h1>Numeric Domains: Size and Precision</h1> <p>This document is part of a series on <a href="..">tricky choices</a> in IPLD design.</p> <h2 id="integers" tabindex="-1"><a class="header-anchor" href="#integers">Integers</a></h2> <p>In principle, we consider the range of integers to be infinite. In practice, many libraries may choose to implement things in such a way that numbers may have limited sizes.</p> <p>We require that IPLD libraries support integers up to at least size 2^53 in order to be considered a full-featured core-compliant IPLD library.</p> <p>We love IPLD libraries that support arbitrarily large numbers. But 2^53 is the critical minimum.</p> <h3 id="why" tabindex="-1"><a class="header-anchor" href="#why">Why?</a></h3> <p>Because.</p> <p>But let's ask more detailed questions, and answer those:</p> <h3 id="why-have-size-limits-at-all" tabindex="-1"><a class="header-anchor" href="#why-have-size-limits-at-all">Why have size limits at all?</a></h3> <p>Most programming languages and compilers already have size limits when working with numbers.</p> <p>Being in denial about this when we describe IPLD is unconstructive: It's important for us to be able to provide concrete recommendations about what we expect from IPLD libraries -- and if that guidance is "always use a bigint type, regardless of whether your language and ecosystem provides a usable and widely adopted one", then that guidance will be frequently ignored, regardless of how principled and well-intentioned it may be.</p> <h3 id="why-2-53" tabindex="-1"><a class="header-anchor" href="#why-2-53">Why 2^53?</a></h3> <p>This 2^53 number is chosen because it's reasonably high (e.g., you can use it for timestamps), and also because it's reasonably practical (it happens to be the number above which JavaScript's handling of numbers gets Interesting).</p> <p><strong>Above this number, it's likely that you'll want to consider application-level and language-level numeric compatibility issues which are bigger in scope than IPLD <em>anyway</em>.</strong></p> <p>32-bit signed, 32-bit unsigned, 64-bit signed, and 64-bit unsigned integers are also all common numeric sizes to consider, because those are often the well-supported numeric types in programming languages. When writing a new IPLD library, we suggest you pick "64-bit signed" if these are your options.</p> <p>(32-bit numbers are definitely small enough to get you into trouble; the <a href="https://en.wikipedia.org/wiki/Year_2038_problem">2038 problem</a> is coming up <em>very</em> soon now. By contrast, a 53-bit integer used to represent a second-granularity timestamp should get you to about the year 285,618,384. So... it should suffice.)</p> <h3 id="why-not-2-64" tabindex="-1"><a class="header-anchor" href="#why-not-2-64">Why not 2^64?</a></h3> <p>See <a href="#why-2-53">Why 2^53</a>.</p> <p>(It's desirable to avoid conflict with JavaScript's numeric domain limitations.)</p> <h3 id="what-if-i-want-to-write-an-ipld-library-that-supports-arbitrarily-large-ints" tabindex="-1"><a class="header-anchor" href="#what-if-i-want-to-write-an-ipld-library-that-supports-arbitrarily-large-ints">What if I want to write an IPLD library that supports arbitrarily large ints?</a></h3> <p><strong>Go for it.</strong></p> <p>If it is possible to support arbitrary "BigInt" in your library, that's fantastic. Do it.</p> <p>We just don't <em>mandate</em> this as part of the minimum core feature checklist for IPLD libraries, because we understand that it's impractical in some programming languages, either because the "BigInt" types have different performance characteristics, or aren't widely agreed upon in the community, or are otherwise simply syntactically or ergonomically clunky to handle, etc. But if you see it as easy to support: <em>go for it</em>.</p> <h3 id="what-if-i-ship-an-ipld-library-that-only-supports-2-47" tabindex="-1"><a class="header-anchor" href="#what-if-i-ship-an-ipld-library-that-only-supports-2-47">What if I ship an IPLD library that only supports 2^47?</a></h3> <p>(... or some other completely arbitrary number.)</p> <p>Fine.</p> <p>Please be very clear about that in your documentation.</p> <p>We won't list your library in our own docs as being a full-featured core-compliant IPLD library.</p> <p>But go nuts; nobody's going to stop you. Your library may run into hard times processing data that's produced by other IPLD libraries, but that's a choice you're free to make.</p> <h3 id="what-if-my-ipld-library-encounters-serialized-numbers-that-are-bigger-than-it-supports" tabindex="-1"><a class="header-anchor" href="#what-if-my-ipld-library-encounters-serialized-numbers-that-are-bigger-than-it-supports">What if my IPLD library encounters serialized numbers that are bigger than it supports?</a></h3> <p>Then it must error. Clearly, one would hope.</p> <p>IPLD libraries must not quietly round down to their max (or up to their min) supported values -- they must error.</p> <p>Fortunately, this rule gets us pretty far, pretty easily -- because we don't <em>do math</em> in IPLD, these errors can really only arise during deserialization, and should fit naturally into the error reporting flow that deserialization already naturally needs.</p> <h2 id="floating-point" tabindex="-1"><a class="header-anchor" href="#floating-point">Floating Point</a></h2> <p>Floating point numbers are a tricky topic.</p> <p>IEEE 754 establishes fairly common expectations for floating point numbers, and we'll lean gently on that in how we imagine floats to work in IPLD. (In particular, the "double64" format is the most widely used format that IEEE 754 specifies; we'll be discussing that format in particular in the rest of this section.)</p> <p>However, IEEE 754 is a format which is defined primarily around the constraints of CPU design, and as a result, there are a lot of practical details in how <em>serialization formats</em> have to map IEEE 754 numbers. Additionally, IEEE 754 definitions are oriented around <em>speed</em> in computations, and as a result, tend to take considerable liberty with canonicalization. Both of these can create issues when working with floating point numbers in or around IPLD, since IPLD is inherently concerned with both serialization and canonicalization.</p> <p>In the next couple of sections, we'll talk specifically about several known sources of issues. However, the general takeaway is: <strong>it is recommended that Float values be avoided when developing systems on IPLD</strong>. We aim to describe float values just well enough to make interoperability possible with codecs that include such a concept, but do not recommend that new protocols be produced which use these concepts.</p> <h3 id="the-base-problem" tabindex="-1"><a class="header-anchor" href="#the-base-problem">The base problem</a></h3> <p>Be aware that base-2 numbers and base-10 numbers are different.</p> <p>For whole numbers, the bases are freely convertible without loss. For decimal numbers, <em>they are not</em>.</p> <p>IEEE 754 floating point numbers are base-2.</p> <p>Codecs which are binary tend to also be able to use base-2 numbers naturally, and so for these codecs there are typically no issues. (CBOR and DAG-CBOR are examples of this.)</p> <p>Codecs which are textual and human readable <em>tend to use base-10 numbers</em>, and so far these codecs <em>there <strong>are</strong> typically conversion issues</em>. (JSON and DAG-JSON are examples of this!)</p> <h3 id="fractions" tabindex="-1"><a class="header-anchor" href="#fractions">Fractions</a></h3> <p>Floating point numbers are not meant to contain fractions (or other non-rational numbers).</p> <p>Values such as <code>1/3</code> are not representable in floating point numbers. If you need to handle fractions in an application, you should not use floating point numbers: you should design a way to handle fractions. (Often, this can be as simple as tracking the numerator and denominator as two distinct integers.)</p> <p>See <a href="https://en.wikipedia.org/wiki/Floating-point_arithmetic#Representable_numbers,_conversion_and_rounding">Representable numbers, conversion and rounding</a> on Wikipedia for more discussion.</p> <h3 id="precision-limits" tabindex="-1"><a class="header-anchor" href="#precision-limits">Precision limits</a></h3> <p>IEEE 754 double64 numbers are composed of 11 exponent bits and 53 significand bits.</p> <p>Since neither of those two numbers is "infinity", we can say that IEEE 754 double64 numbers will have some precision limits.</p> <p>Very large numbers are unrepresentable.</p> <p>Some very <em>small</em> numbers are also unrepresentable.</p> <p>The granularity of gaps between numbers which are representable also varies, due to how the exponent bits are used. Roughly speaking, the distribution of representable floating point numbers is denser nearer to zero.</p> <p>(Bonus Fun fact: did you know the {the distribution of floating point numbers} and {the distribution of the <em>sums</em> of floating point numbers} are different distributions? <a href="https://link.springer.com/article/10.1007/BF02278709">Indeed they are</a>.)</p> <h3 id="special-values" tabindex="-1"><a class="header-anchor" href="#special-values">Special values</a></h3> <p>IEEE 754 specifies several special values such as <code>Infinity</code>, <code>-Infinity</code>, and <code>NaN</code>.</p> <p>We generally exclude these from being considered floats in IPLD. Partly, this is a principled choice (we suggest that if a protocol needs sentinel values, it is better to be explicit about them, separately from numbers). Partly, this is a practical choice (because this is in keeping with the same choices made by popular and ubiquitous codecs such as JSON).</p> <h3 id="nan-bits" tabindex="-1"><a class="header-anchor" href="#nan-bits">NaN bits</a></h3> <p>"Not a Number" (NaN) can be encoded in <em>many distinct ways</em> in IEEE 754 floats.</p> <p>All of the following values, represented in hexadecimal, are considered acceptable binary representations of NaN:</p> <ul> <li><code>7FF0 0000 0000 0001</code></li> <li><code>7FF8 0000 0000 0001</code></li> <li><code>7FFF FFFF FFFF FFFF</code></li> </ul> <p>Nor are the above an exhaustive list of options: NaNs contain a range of bits which are not particularly well specified at all, and may be considered a "payload"... even though most applications (and indeed, most programming languages) disregard this.</p> <p>This creates considerable questions for how to canonicalize floating point numbers for serialization purposes.</p> <p>We simply state that NaN is not part of IPLD's definition of floating point numbers to avoid these issues.</p> <p>See <a href="https://en.wikipedia.org/wiki/NaN#Encoding">Wikipedia for more information on NaN and its encoding</a>.</p> <h3 id="extended-precision" tabindex="-1"><a class="header-anchor" href="#extended-precision">Extended Precision</a></h3> <p>(It's not a good thing.)</p> <p>Be aware that some environments (whether exotic programming languages, or <em>hardware itself</em>) offers "<a href="https://en.wikipedia.org/wiki/Extended_precision">extended precision</a>" when doing floating point math. In short, this just means it uses more bits when in the middle of the mathematical operation, and then rounds or truncates the data back down to double64 bit sizes when it's done.</p> <p>This is not a problem for IPLD itself, per se; but it's something you should be aware of if designing applications in a space where you care about reproducibility of computations, or about convergence and canonicalization in data, because it may affect you.</p> <p>Calculations which are done using extended precision can (and will) have <em>different results</em> than if the same input numbers were used in a calculation performed without extended precision. This can be problematic if unexpected.</p> <p>And it's especially problematic because <em>it's often unexpected</em>. Many programming environments do not make this choice apparent to a programmer; most environments pass the question off to the hardware directly; and hardware can (and does) make its own choices in ways that are not necessarily reproducible on other hardware. (See Java's <a href="https://en.wikipedia.org/wiki/Strictfp">strictfp keyword</a> for a rare counter-example of this.)</p> <p>Issues with reproducibility in computation is one of the reasons that <strong>it is recommended that Float values be avoided when developing systems on IPLD</strong>.</p> <h3 id="avoid-floats" tabindex="-1"><a class="header-anchor" href="#avoid-floats">Avoid floats</a></h3> <p>(Please.)</p> <p>Content-addressing works best where the content being addressed has a stable meaning for the address it produces. Alternative methods for representing this meaning, or for encoding fractional numbers with greater precision and less variability, should be considered where possible.</p> </div> </main> </body> </html>