-5

Can any one here help me out to understand how the below floating point directives work in Ruby with packing() and unpacking() method?

  • D and d
  • F and f
  • g and G
  • e and E

I have tried below:

irb(main):001:0> [2,44,43].pack('D')
=> "\x00\x00\x00\x00\x00\x00\x00@"
irb(main):002:0> [2,44,43].pack('d')
=> "\x00\x00\x00\x00\x00\x00\x00@"
irb(main):004:0> [2,44,43].pack('ddd')
=> "\x00\x00\x00\x00\x00\x00\x00@\x00\x00\x00\x00\x00\x00F@\x00\x00\x00\x00\x00\
x80E@"
irb(main):005:0> [2,44,43].pack('fff')
=> "\x00\x00\x00@\x00\x000B\x00\x00,B"
irb(main):006:0> [2,44,43].pack('FFF')
=> "\x00\x00\x00@\x00\x000B\x00\x00,B"
irb(main):007:0> [2,44,43].pack('ggg')
=> "@\x00\x00\x00B0\x00\x00B,\x00\x00"
irb(main):008:0> [2,44,43].pack('GGG')
=> "@\x00\x00\x00\x00\x00\x00\x00@F\x00\x00\x00\x00\x00\x00@E\x80\x00\x00\x00\x0
0\x00"
irb(main):009:0>

How the output is coming? what the logic of such computation?

Thanks in advance!

Arup Rakshit
  • 116,827
  • 30
  • 260
  • 317
  • 2
    What part of the [documentation](http://ruby-doc.org/core-1.9.3/String.html#method-i-unpack) are you having trouble with? – mu is too short Jan 12 '13 at 18:49
  • Yes, there they just shown some of the directives with examples, but not all of my list. So as a newbie to this platform of `ruby` I would like to really know how exactly these works,what the logic are incurred with them? – Arup Rakshit Jan 12 '13 at 18:56
  • 1
    Have you tried to use them in combination with a hex dumper? They work the same way as they do in Python and Perl: they convert native types to and from raw bits. – mu is too short Jan 12 '13 at 19:06
  • Can you please help me here with some tiny codes. I am not educated with perl or python too.your help can give a good start to explore with them more! – Arup Rakshit Jan 12 '13 at 19:07
  • 2
    Why don't you try these out in `irb`? Most people you are asking don't know how these work either. The trick to find out is using `irb` to test them. So what have you tried, and what is it specifically you do not understand about these methods? Your question has too broad a scope. – Casper Jan 12 '13 at 19:14
  • Don't close this post, I just updated my description! hope people around `SO` will now understand what I am trying to understand! please don't vote the post for close! – Arup Rakshit Jan 12 '13 at 19:36
  • What the bad making people to vote it as down? if any issue to understand what i am looking for to have here,please ask me. not to try the bad habit `DOWN VOTE`. – Arup Rakshit Jan 12 '13 at 19:45
  • 1
    Briefly: floats have a binary representation that the computer, C, and C++ understand and another representation that Ruby understands; `pack` and `unpack` are used to convert between those two. They're generally used for reading/writing binary files and speaking binary network protocols. – mu is too short Jan 12 '13 at 20:15

1 Answers1

1

Those directives convert the numbers in the array to a floating point byte representation.

If you want to learn what is happening you can watch this video:

Floating Point Representation: Example
https://www.youtube.com/watch?v=t-8fMtUNX1A

And read this article:
http://www.geeksforgeeks.org/floating-point-representation-basics/

Through Google I found the above answers for you by searching for "floating point representation".


Just to give you an example:

> [2.0].pack('f').bytes.map(&:to_i)
=> [0, 0, 0, 64]

This is a floating point number which is represented in hexadecimal as:

0x40000000

Where you can see that 64 is 0x40 in hexadecimal.

In order to understand why 2.0 becomes 0x40000000 you have to understand how computers convert floating point numbers into binary representations.

You can use this page to test it out:
http://www.h-schmidt.net/FloatConverter/IEEE754.html

For example here is what is looks like for 2.0:

http://i.imgur.com/qWqFM.png

Casper
  • 33,403
  • 4
  • 84
  • 79
  • No,I am giving you one post,see how someone answered to that post. and i didn't find any answers of my above post,thus posted. have look at the explanation. Please dont down vote me! [post](http://stackoverflow.com/questions/14294988/how-the-x-x-directives-work-with-ruby-pack-unpack-method). – Arup Rakshit Jan 12 '13 at 19:54
  • 1
    @PythonLikeYOU I read that answer. This cannot be answered that simply. Please watch the video (10 minutes) and you will see it is not that simple. – Casper Jan 12 '13 at 19:56
  • okay!let it opened then,hope anyone surely can answer here,don't down vote, its made my post bad to see! BTW I am looking into the video! – Arup Rakshit Jan 12 '13 at 20:02
  • @PythonLikeYOU - Watch the video then see my edited answer. – Casper Jan 12 '13 at 20:09