Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

floats need special handling when unpacked #3

Open
dvirsky opened this issue Oct 2, 2012 · 0 comments
Open

floats need special handling when unpacked #3

dvirsky opened this issue Oct 2, 2012 · 0 comments

Comments

@dvirsky
Copy link

dvirsky commented Oct 2, 2012

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() ?

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

No branches or pull requests

1 participant