[Atp] Re: Floats support summary

Hi Volker,

Thank you for picking this back up.

I continue to have strong concerns about depending on JSON numeric encoding alone to determine whether a number is intended to be an integer or float. The JSON data model is clear in having only a single overall numeric type, and I think we need a more general and robust solution to this modeling problem than relying on observed implementation behaviors. In many situations and use-cases the encoding and distinction is not important, but because atproto is content-addressed, it is important. I think a wrapper encoding like the ‘$float’ option you mention would be more suitable.

In your analysis of implementation behaviors, you noted JavaScript and Go as language ecosystems where special care and configuration is required to ensure consistent encoding behavior. Those are both popular languages in general, and top languages in the atproto ecosystem today. As noted above I don’t think the behaviors of individual JSON implementations are particularly important for making this decision, but it is concerning to me that these languages would not work reliably “out of the box”.

I’m putting together some more specific examples and a counter-proposal to share with the list. I’d love to get feedback on all this from more folks, both atproto developers and IETF community folks. The upcoming IETF 127 meeting seems like a good time frame for converging on a path forward.

–bryan

I’ve got a new proposal to consider for floats.

Credit for this goes to Daniel Holmgren, though Volker raised a similar idea in a blog post back in May.

The basic idea is to flip things around and make number the core numeric type. The semantics and JSON representation would match JSON numbers: integer or floating point, in whatever formatting. JSON “123” and “123.0” are both allowed, both describe the same number, and both get encoded the same way in CBOR. The CBOR encoding would depend on the value of the number itself: if it is whole number (integer), regardless of JSON formatting, it gets represented as int64, otherwise it gets represented as floats. When decoding CBOR, it would be mandatory to check that float values are not whole numbers (integers), and throw an invalid data error if they are. How numbers get represented in programming languages would be an SDK decision. They could always be floats (like in JavaScript), or match the CBOR encoding, or some other convention. What matters is consistency when encoding and decoding CBOR.

The integer type would become a schema validation (lexicon) concept, and represented in the data model as number. SDKs can use methods like JavaScript’s Number.isInteger() to check if a float is a whole number (integer). Existing data records and deployed atproto software that works with integers would continue to function just as it does today.

A nice property is that common small integer numbers (like “0” and “1”) would be represented more compactly than they would be as full float64.

If specified and implemented carefully, I think this would introduce floats with an ergonomic developer experience, while meeting all the requirements for the data model. Here are a few corner-cases to consider:

  • whether isInteger() implementations exist and are perfectly consistent across languages and machine architectures
  • numbers beyond the “safe” 2^53 range where all integers can be represented as a 64-bit float
  • numbers beyond the 2^63 int64 range (do they encode as floats?)
  • being able to losslessly represent any int64 or any float64 is a bit weird
  • other issues we have not thought of

I’ve also got a longer blog post about requirements for the atproto data model and how to fit floats in:
https://bnewbold.leaflet.pub/3myzwz2tvgk26

–bryan

Hi all,

I think that the new idea would make a lot of sense. Personally, I think that the biggest barrier to so many data/scientific uses on the protocol is just the complexity that comes from having to manually encode and decode floats in strings at the application level; anything that simplifies this is a bonus, and this proposal’s closeness to the JSON number spec is really simple and good in that sense. I don’t really see any major barriers to the use cases I’m familiar with, and the closeness to JSON’s spec means that we wouldn’t be adding any new sharp edges to the protocol, instead just simplifying things compared to before. So this is great!

Eventually, it might be nice to revisit having specific string formats as a way to encode weirder things (like 128 bit floats), but I think that should be much lower priority than this because first-class 64 bit floating point number support will already solve >99% of scientific uses of floats. And anyone can already do that anyway within their own hyper-specific use cases, just without having a string format field that says so.

One aside: it might be good to have an accepted atproto “best practice” for handling inf and NaN values, lest we end up with dozens of different ways of doing so. This may hence be good to define already in the name of long-term interoperability.

I think that “null” is a good stand-in for NaN, and this is often what many JSON libraries do in practice. Whether or not a field is defined as nullable in a lexicon would then act as a guide for users to know what could or couldn’t be a NaN value. For inf values, “null” is an ok option, but +inf and -inf can have separate semantic meanings that can be quite important. This issue isn’t unique to atproto though, there are many cases where “inf” values need to be converted into something else, such as for many databases or older formats. One solution could be to still use “null” to mean NaN, +inf, or -inf, and then to have a best practice that lexicon designers write some additional optional object field that provides key:value pairs of what each null value means numerically, if necessary.

So, e.g. an invalid JSON object that contains an inf (such as those emitted by Python) like
{“value1”: inf, “value2”: 5., “value3”: NaN}

could be written as
{“value1”: null, “value2”: 5., “value3”: null, “nullMeanings”: {“value1”: “+inf”, “value3”: “NaN”}}

on atproto without a loss of meaning of +inf or -inf values, if that happens to be important to some use case. (Sadly, it often is. so much scientific data is yucky)

Best,

Emily

+1 to huge floats being special-cased as strings in user space/lexicon level

+1 to the timeliness of specifying a convention for numeric special-case values that are invalid in JSON and/or in DAG-CBOR. Here too I like the idea of a lexicon/user-space workaround to ensure roundtripping where it’s important to do so​, and just settle for NaN, Inf, and -Inf becoming NULL in DAG-CBOR (and thus in intermediate ATP-JSON) where user-space schema is impossible or round-tripping isn’t needed..

Thanks for all the feedback in this thread.

I don’t see any objections to this overall direction for including floats in the atproto data model. My current feeling is that this is the most likely path forward, and that we can get this in to the Repository and Synchronization text.

I do think there are some details to work out before this gets specified, and it would be good to see some implementations. Are there any volunteers to lead this effort? I am unlikely to take the lead on this until early 2027, though I can respond and review sooner.

Might be a good Hackathon project at the upcoming IETF 127 in San Francisco, though I don’t know if any of the other folks in this thread are attending that.

–bryan

Hi,

Here is the Hackathon page. Please register a project if you want to proceed. You could subscribe the mailing list of the Hackathon and then advocate the project in the mailing list and call for participants from the community.

https://www.ietf.org/meeting/hackathons/127-hackathon/

Shuping

JFYI:
Essentially this proposal has been written up a while ago, called dCBOR.

Play with it at CBOR playground (try setting the dCBOR checkbox).

Grüße, Carsten

Definitely keep floats and ints separate types in the data model - it keeps backwards compatibility for schema already using `integer` as a type, keeps number formatting consistent for CID purposes, and knowing whether you have to care about decimal places or not is always helpful to avoid shooting yourself in the foot. As funny as it may be, you can't exactly give a good response when someone queries `com.atproto.repo.listRecords` asking for 52.375 results. Better to just make the contract of number type explicit.

Even if they're nominally independent, I still think it's for the best for them to keep parity to make it less confusing for developers and implementors.

I agree we should add floats to the data model. If we are going to mess with the model, do it once, do it right. And once you start considering that, we have to think beyond just ATprotocol because we may end up having to interoperate with other systems. Two systems that almost but not quite interoperate can be the very worst. See base64 and base64urlencoded.

I would add the following to the JSON model:

Binary: byte
Float64: IEEE 754 binary64
Decimal64: IEEE 754 decimal64
Float80: Intel 80 bit binary float

These are the formats that are most widely supported with hardware assist. While 754 specified 128 and 256 bit floats, I am not aware of any hardware support. The 64bit binary only makes use of 53 bits of the large adder/multiplier so Intel and most other manufacturers support a mode with a 64 bit mantissa and 15 bit exponent.

Currently, pretty much everyone who uses a schema driven approach has a data model that includes binary as a type and these are serialized as base64url blobs. I suggest doing the same for binary floats to maintain backwards compatibility. Decimal floats can use the existing serialization without loss of precision.

If we use a non JSON serialization, there are three approaches that make sense:

  1. Add hexadecimal encoding of binary floats. This preserves most of the advantages of a text based format. While few of us can read hex floats, this is probably not necessary for debugging purposes. It means that someone can edit a control file in a JSON like format without needing a special editor. (e.g. JSON-A)

  2. Extend the JSON serialization to incorporate binary blobs using code points 128-255 for binary extensions. I did this with my JSON-B encoding which has the advantage that a single decoder can consume either straight JSON or extended editions. (e.g. my JSON-B encoding)

  3. Use ASN.1, CBOR or other serialization of your choice.

I will be supporting all three approaches in my systems. I use a schema format that allows me to emit JSON, JSON-A, JSON-B and so on, ASN.1, DNS RR type encodings, protobuf etc.

The wider industry has pretty much settled on JSON though and I strongly suspect that if there is any change to JSON to support binary floats without loss of precision it will take either the hex approach or attempt the round trip serialization of IEEE754 which exists but is amazingly painful to implement.

While I was using JSON-B in my work, I have found a far more performant approach to avoiding the base64 expansion of ciphertext and signed plainted payloads. I use an envelope format based on QUIC varints. This allows me to create a CMS/PKCS#7 type envelope structure (as used in S/MIME) using JSON/JOSE for all the content metadata, key exchange, signatures etc. without the base64 penalty.

So if there is change in the wider industry, I would expect that hex representation of binary floats will be the way they go.