3

I read Stack Overflow question How do I convert a binary string to a number in Perl? on how to convert binary integers to decimal or vice versa in Perl. But how do I do this for float as well?

For example, conversion from 5.375 to 101.011 and vice versa.

3
  • I think you're looking for the pack function. Take a look at perldoc -f pack.
    – user554546
    Commented Oct 5, 2011 at 23:58
  • @Jack: right, I have looked aththat function, but still could not figure this out. please help out with some explicit working code. Thanks.
    – Qiang Li
    Commented Oct 6, 2011 at 0:06
  • 1
    This sounds like a homework question.
    – Ether
    Commented Oct 6, 2011 at 0:37

4 Answers 4

3
sub number_to_binary_string {
    my $in = shift;
    my $sign = $in < 0 and $in = abs $in;
    my $out = sprintf "%b.", int $in;
    substr $out, 0, 0, '-' if $sign;
    $in -= int $in;
    do {
        if ($in >= .5) {
            $out .= '1';
            $in -= .5;
        }
        else {
            $out .= '0';
        }
        $in *= 2;
    } while $in > 0;
    return $out;
}

sub binary_string_to_number {
    my $in = shift;
    my ($int,$frac) = split /\./, $in;
    my $sign = $int =~ s/^-//;
    my $out = oct "0b$int";
    my $mult = 1;
    for my $digit (split //, $frac) {
        $mult *= .5;
        $out += $mult * $digit;
    }
    $out = -$out if $sign;
    return $out;
}
2
  • Cool, +1. I did find a few small bugs though: number_to_binary_string appends ".0" to integers and doesn't handle numbers larger than 232-1; binary_string_to_number emits warnings on numbers that lack a decimal point or that are greater than 232-1. Commented Oct 6, 2011 at 0:47
  • from the problem statement I was assuming everything had/needed a decimal, but you're right about >=2**32. replacing the oct and sprintf with loops is left as an exercise for the student
    – ysth
    Commented Oct 6, 2011 at 1:08
2

Below is a machine- and build-specific implementation (NV = little-endian double).

It returns the number stored exactly, and it supports NaN, Infinity, -Infinity and -0 and subnormals. It trims leading zeros and trailing decimal zeroes.

sub double_to_bin {
   my ($n) = @_;
   my ($s, $e, $m) = unpack 'a a11 a52', unpack 'B64', "".reverse pack 'F', $n;
   $s = $s ? '-' : '';
   $e = oct("0b$e");

   if ($e == 0x7ff) {
      return ($m =~ /1/) ? 'NaN' : $s . 'Infinity'
   } elsif ($e == 0x000) {
      $m = "0$m";  $e -= 52;
   } else {
      $m = "1$m";  $e -= 1075;
   }

   if ($e >= 0) {
      $m .= ('0' x $e);
   } elsif ($e >= -52) {
      substr($m, $e+53, 0, '.');
   } else {
      $m = '0.' . ('0' x (-$e-53)) . $m;
   }

   $m =~ s/^0+(?!\.)//;
   $m =~ s/(?:\..*1\K|\.)0+\z//;
   return $s . $m;
}
1

Here's a sketch of an interesting "portable" implementation. It doesn't handle any of the interesting edge-cases like integers, NaNs, infinities, or even negative numbers because I'm lazy, but extending it wouldn't be so hard.

(my $bin = sprintf "%b.%032b", int($num), 2**32 * ($num - int($num)))
    =~ s/\.?0+$//;

The 2**32 seems like an architecture-specific magic number but in fact it's basically just how many bits of precision you want after the dot. Too small and you get harmless truncation; too large and there's potential for overflow (since %b probably casts to UV sometime before doing its formatting).

1
  • you want %b.%032b there to preserve the leading zeros; without, .5, .25, .125 all become "0.1"
    – ysth
    Commented Oct 6, 2011 at 5:44
0
$TO_BIN = '-b';
$TO_DEC = '-d';
($op, $n ) = @ARGV;
die("USAGE: $0 -b <dec_to_convert> | -d <bin_to_convert>\n") unless ( $op =~ /^($TO_BIN|$TO_DEC)$/ && $n );

for (split(//,$n)) {
    if ($_ eq ".") {
        $f=".";
    } else {
        if (defined $f) { $f.=$_ } else { $i.=$_ }
    }   
} 
$ci = sprintf("%b", $i)          if $op eq $TO_BIN;
$ci = sprintf("%d", eval "0b$i") if $op eq $TO_DEC;

@f=split(//,$f) if $f;
if ($op eq $TO_BIN) {
    while( $f && length($cf) < 16 ) { 
        ($f *= 2) =~ s/(\d)(\.?.*)/$2/;
        $cf .= $1 ? '1' : '0';
    }
} else {
    for ($i=1;$i<@f;$i++) {
        $cf = ($cf + $f[@f-$i])/2;
    }
}
$cf=~s/^.*\.|^/./ if $cf;
print("$ci$cf\n");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.