#!/usr/bin/perl use strict; use warnings; my @array = (3.987654321,-3.987654321); foreach $_ (@array){ if ($_ < 0) { print "Value: $_ is negative\n"; my $negative_b = dec2bin($_,64,"F"); print "Negative binary: ".$negative_b."\n"; my $negative_d = bin2dec($negative_b,64,"F"); print "Negative_binary back to decimal: ".$negative_d."\n"; } else { print "Value: $_ is possitive\n"; my $possitive_b = dec2bin($_,64,"F"); print "Possitive binary: ".$possitive_b."\n"; my $possitive_d = bin2dec($possitive_b,64,"F"); print "Possitive_binary back to decimal: ".$possitive_d."\n"; } } sub dec2bin { my $bits = shift; my $size = shift; my $template = shift; my $str = unpack("B$size", pack($template, $bits)); print "Substring: ".$str."\n"; return $str; } sub bin2dec { my $bits = shift; my $size = shift; my $template = shift; return unpack($template, pack("B$size", substr("0" x $size . $bits , -$size))); } __END__ Value: 3.987654321 is possitive Substring: 1010111010010101000000110100111110110111111001100000111101000000 Possitive binary: 1010111010010101000000110100111110110111111001100000111101000000 Possitive_binary back to decimal: 3.987654321 Value: -3.987654321 is negative Substring: 1010111010010101000000110100111110110111111001100000111111000000 Negative binary: 1010111010010101000000110100111110110111111001100000111111000000 Negative_binary back to decimal: -3.987654321