Skip to content

fix: reject -0.0 - #65

Merged
vmx merged 1 commit into
masterfrom
reject-float-minus-0
Jul 30, 2026
Merged

fix: reject -0.0#65
vmx merged 1 commit into
masterfrom
reject-float-minus-0

Conversation

@vmx

@vmx vmx commented Jul 23, 2026

Copy link
Copy Markdown
Member

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.

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`.
@rvagg

rvagg commented Jul 24, 2026

Copy link
Copy Markdown
Member

@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.

@vmx

vmx commented Jul 24, 2026

Copy link
Copy Markdown
Member Author

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.

Comment thread src/ser.rs
))
} else {
// -0.0 and 0.0 are equal, always encode as 0.0 (0x0000000000000000).
let v = if v == -0.0 { 0.0 } else { v };

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@rvagg rvagg left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fine by me

@rvagg

rvagg commented Jul 30, 2026

Copy link
Copy Markdown
Member

IEEE 754 is a deep well .. you might get lost in here

@vmx
vmx merged commit a8fa998 into master Jul 30, 2026
6 checks passed
@vmx
vmx deleted the reject-float-minus-0 branch July 30, 2026 08:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants