Skip to content

floats need special handling when unpacked #3

Open
@dvirsky

Description

@dvirsky

Packing a float value does not necessarily pack it as a float in the underlying protocol you try to optimize for space. This is not a bug, but it makes for very cumbersome code when unpacking floats, because a float can be encoded either a UInt of some length, or an Int of some length.

Unless I'm missing something (I'm pretty new to Go), you have to decode it as such:

switch value.Kind() {
        case reflect.Uint, reflect.Uint64, reflect.Uint32, reflect.Uint16, reflect.Uint8:
            ival := value.Uint()
            fval = *(*float32)(unsafe.Pointer(&ival))
        case reflect.Int, reflect.Int64, reflect.Int32, reflect.Int16, reflect.Int8:
            ival := value.Int()
            fval = *(*float32)(unsafe.Pointer(&ival))
}

Shouldn't this be handled automatically with something like UnpackFloat32/64() ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions