fix: reject -0.0 - #65
Conversation
For IEEE 754 floats `-0.0` is equal to `0.0`. It makes sense to have only a single representation for the same numbers, hence always encode it as `0.0`, i.e. `0x0000000000000000`.
|
@vmx should we just consider clobbering it to +0 to avoid the error? I know it's borderline, maybe the intent was to hold a -0 so an error is appropriate but my (current) guess is that a -0 is mostly an accidental state that you find yourself in and then you get this error as a flake and you have to go add special paths to -0 -> +0 yourself. Or do you think the explicit signal is appropriate? I try not to use floats at all in serialisation so don't tend to think about this but when you're in the land of floats you're thinking about ranges rather than specific states like this. |
|
When serializing -0.0 it is serialized as 0.0 without an error. It errors when deserializing. My reason to do this is triggered by more and more LLM usage. As CBOR is pretty simple to implement (especially subsets like DAG-CBOR), I would expect that LLMs decide to just implement the serialization themselves. When the use a strict serializer as basis, things will be fine. But I could also imagine they "figure it out" themselves and maybe test it against an existing decoder. Hence I'd like to be strict by default. All this may be too constructed and not realistic, but I also don't see much harm in being strict and relaxing it if people start seeing issues. |
| )) | ||
| } else { | ||
| // -0.0 and 0.0 are equal, always encode as 0.0 (0x0000000000000000). | ||
| let v = if v == -0.0 { 0.0 } else { v }; |
There was a problem hiding this comment.
fine, but also "-0.0 and 0.0 are equal" so you don't need the -0.0 in this if cause it's not doing any work here
There was a problem hiding this comment.
You could also write it as if v == 0.0 { 0.0 } else { v }, but I found using -0.0 at least a bit clearer.
|
IEEE 754 is a deep well .. you might get lost in here |
For IEEE 754 floats
-0.0is equal to0.0. It makes sense to have only a single representation for the same numbers, hence always encode it as0.0, i.e.0x0000000000000000.