Connecting Tech Pros Worldwide Help | Site Map

Floating point internal representation..

Newbie
 
Join Date: Jun 2008
Posts: 2
#1: Jun 27 '08
In perl, how can I get the actual internal floating point data for a variable? In C, I can do something like this
Expand|Select|Wrap|Line Numbers
  1. {
  2.     float f = 5.88;
  3.  
  4.     unsigned char a[4];
  5.  
  6.     memcpy(a, (void *)&f, 4)
  7.  
  8.     printf("0x%x  %x  %x  %x\n", a[3], a[2], a[1], a[0]);
  9. }
This prints "0x40 bc 28 f6" -> the IEEE floating point representation for 5.88..

I need to do the same with a statement like

my $f = 5.88;

Thanks,
Doug
Newbie
 
Join Date: Jun 2008
Posts: 2
#2: Jun 27 '08

re: Floating point internal representation..


Well, that was as clear as mud, but it looks like the answer is to pack/unpack.

Expand|Select|Wrap|Line Numbers
  1.     my $f = 5.88;
  2.     unpack('L', pack('f',  $f));
Reply