Very strange. I note that the problem goes away if you replace 1.5
negative integer:
$ perl -e ' my @a=(-9.0, -3.0, -2.0, -1.5, -1.2, -1.0, -0.8, -0.5); for my $a (@a) { $bin = join("", map {sprintf("%02x", ord($_))} split(//, pack("d>", $a*0))); printf("%4.1f * 0 = %4.1f %s\n", $a, $a*0, $bin); }' -9.0 * 0 = 0.0 0000000000000000 -3.0 * 0 = 0.0 0000000000000000 -2.0 * 0 = 0.0 0000000000000000 -1.5 * 0 = -0.0 8000000000000000 -1.2 * 0 = -0.0 8000000000000000 -1.0 * 0 = 0.0 0000000000000000 -0.8 * 0 = -0.0 8000000000000000 -0.5 * 0 = -0.0 8000000000000000
All I can think of is to consider -0.0 as a special case:
my $ans = (0.6*$m+ 0.7 * $e-1.5)*$g; my $x= sprintf("%0.1f", $ans == -0.0 ? 0.0 : $ans)
( EDIT: That was a dumb suggestion, since -0.0 == 0.0
.)
I also tested Python's behavior, which preserves a character in sequence, which assumes that a negative sign is not really an error in Perl, is just a bit strange (although I would say that reversing integers and non-integers in different ways is an error):
$ python -c ' for a in [-9.0, -3.0, -2.0, -1.5, -1.2, -1.0, -0.8, -0.5]: print "%0.1f" % (a*0,) ' -0.0 -0.0 -0.0 -0.0 -0.0 -0.0 -0.0 -0.0