in reply to Re: Round down to '.5' or '.0'
in thread Round down to '.5' or '.0'
The best part is that *2 and /2 are lossless operations on floats.
>perl -le"$,=' '; $x=.1; print map /(.)(.{11})(.*)/, unpack 'B*', reve +rse pack 'd', $x/=2 for 1..5;" 0 01111111010 1001100110011001100110011001100110011001100110011010 0 01111111001 1001100110011001100110011001100110011001100110011010 0 01111111000 1001100110011001100110011001100110011001100110011010 0 01111110111 1001100110011001100110011001100110011001100110011010 0 01111110110 1001100110011001100110011001100110011001100110011010 - ----------- ---------------------------------------------------- S Exponent Mantissa
Precision already loss is not recuperated, of course.
my $n; $n += 0.1 for 1..10; # $n = 0.1 * 10 = 1 ...ish my $r = int($n*2)/2; print("round($n) = $r\n"); # round(1) = 0.5 print("cause it's really doing\n"); # cause it's really doing printf("round(%.16e)\n", $n); # round(9.9999999999999989e-001)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Round down to '.5' or '.0'
by gamache (Friar) on Oct 24, 2007 at 18:55 UTC | |
by ikegami (Patriarch) on Oct 24, 2007 at 19:10 UTC | |
| |
|
Re^3: Round down to '.5' or '.0'
by oha (Friar) on Oct 25, 2007 at 09:41 UTC |