Skip to content

feature: don't round float values when decoding #693

@makew0rld

Description

@makew0rld

Is your feature request related to a problem? Please describe.

I'm developing a library related to CBOR. I haven't run into an issue with this yet, but when I noticed that float values can be silently truncated this surprised me. I'm interested in the rationale for this, and whether you would be open to changing this behaviour.

For example, when decoding the float64 value 1.50000000000000022204 (fb3ff8000000000001 in CBOR hex) into a float32, this library rounds the value to the closest float32 value, 1.5.

My main concern with this is that there is silent data loss.

Describe the solution you'd like

The overflowFloat32 function is changed to check that an exact value can be transferred, not just min/max checking. This could be behind a decoding option, but I think mandating it or making it the default is worth considering as well. Let me know what you think.

Describe alternatives you've considered

One alternative is to just always use float64 when decoding. In practice I think this is what most users will do, and so this isn't a major issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions