-
Notifications
You must be signed in to change notification settings - Fork 122
Possibility to write 32-bit single precision floats to MessagePack #101
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
Conversation
|
Now also working with rubinius and JRuby :) |
|
Wouldn't it be sufficient to just add |
|
That's pretty much all I did + overwriting equality methods for testing purposes. If you add comment to the code where you think it's better placed somewhere else or left out, I would be happy to review it. |
|
I meant: why do you need In my opinion this does not belong in the MessagePack library. I don't mean that as a discouragement, I'm very happy that you want to contribute, and it might be the case that the other maintainers disagree with me. I would much rather see something on the lines of my original JRuby implementation did: https://github.com/msgpack/msgpack-ruby/blob/master/ext/java/org/msgpack/jruby/Encoder.java#L186-L191 (i.e. detect when a floating point number can be represented using 32 bits). It's the wrapper class ( |
|
Really appreciate your input. I agree that the additional class feels a bit weird but maybe wrapper class isn't the good word here. It's actually just an implementation of the missing single precison float type in Ruby and that's how you should see it. I don't see how I could force the serialization to the 32 bit format by simply implementing The automatic detection is certainly a nice optimization, assuming we can solve the platform dependency problems mentioned in #6 but it does not solve the problem that users cannot easily create single precision float values at the first place. Maybe a combination of automatic detection and explicit typing is the right way to go. Assume the automatic detection works. All we needed then was something like a |
|
I agree with @iconara. Introducing What do you want to do?
|
|
@tagomoris Option 2 is what I want. Then def to_msgpack(pk = nil)
return MessagePack.pack(self, pk) unless pk.class == MessagePack::Packer
buffer = pk.buffer
buffer.write("\xCA")
buffer.write([numeric].pack('g'))
pk
endUnfortunately the detour via def to_msgpack(pk = nil)
return MessagePack.pack(self, pk) unless pk.class == MessagePack::Packer
pk.write_float32(numeric)
end |
|
@ojundt The fastest implementation is to implement extension library in C for you own class, and use |
|
I might be doing something wrong but if I try to return an already serialized float32 in I just pushed the new proposal without SinglePrecisionFloat btw. Got an error in the random test on travis, though. Is this a known issue? |
|
After a good night sleep I really think that With Then you are only left with the problem that Ruby does not have a 32-bit float type itself. So what type do we pass to the method? |
|
@tagomoris I saw you had the same trouble with the rubinius tests so I adapted your commit to temporarily disable them. Any thoughts on the new proposal? |
|
IMO, adding @frsyuki How do you think about this idea? |
|
Adding |
Possibility to write 32-bit single precision floats to MessagePack
|
I merged. |
|
This change is released as |
|
Awesome. Thank you guys, too! |
I was missing a way to pass 32-bit single precision floats to MessagePack since ruby only has 64-bit double precision floats.
With this change it is possible by simply wrapping any float (or in fact any numeric) ruby value in a new MessagePack::SinglePrecisionFloat instance. Thoughts?
The JRuby implementation is missing for now. Unfortunately I don't have time/experience for that part :/