Name: Anonymous 2007-05-17 11:03 ID:ewt37wAw
Prelude> round 0.5
0
Prelude> round 1.5
2
Prelude> round 2.5
2
Prelude> round 3.5
4WHAT THE FUCK
Prelude> round 0.5
0
Prelude> round 1.5
2
Prelude> round 2.5
2
Prelude> round 3.5
4Prelude> map properFraction [0.5..9.5]
[(0,0.5),(1,0.5),(2,0.5),(3,0.5),(4,0.5),(5,0.5),(6,0.5),(7,0.5),(8,0.5),(9,0.5)]round x = if (snd $ properFraction x) >= 0.5 then ceiling x else floor xPrelude> map round [0.5..9.5]
[1,2,3,4,5,6,7,8,9,10]use Quantum::Superpositions;
sub round($){
my($n)=@_;
return int($n)+($n-int$n>.5)-($n-int$n<-.5)+any(0,(any(-1,1)==2*($n-int$n)));
}
$ cat > fptest.c
#include <stdio.h>
void print_bytes_of_double(double d) {
int i;
printf("%f is", d);
for(i=0;i<sizeof(d);i++)
{
printf(" %02x", ((unsigned char*)&d)[i]);
}
printf("\n");
}
int main() {
int n;
for(n=1;n<20;n++)
{
print_bytes_of_double(1.0/n);
}
}
$ cc fptest.c -o fptest
$ ./fptest
1.000000 is 00 00 00 00 00 00 f0 3f
0.500000 is 00 00 00 00 00 00 e0 3f
0.333333 is 55 55 55 55 55 55 d5 3f
0.250000 is 00 00 00 00 00 00 d0 3f
0.200000 is 9a 99 99 99 99 99 c9 3f
0.166667 is 55 55 55 55 55 55 c5 3f
0.142857 is 92 24 49 92 24 49 c2 3f
0.125000 is 00 00 00 00 00 00 c0 3f
0.111111 is 1c c7 71 1c c7 71 bc 3f
0.100000 is 9a 99 99 99 99 99 b9 3f
0.090909 is 46 17 5d 74 d1 45 b7 3f
0.083333 is 55 55 55 55 55 55 b5 3f
0.076923 is 14 3b b1 13 3b b1 b3 3f
0.071429 is 92 24 49 92 24 49 b2 3f
0.066667 is 11 11 11 11 11 11 b1 3f
0.062500 is 00 00 00 00 00 00 b0 3f
0.058824 is 1e 1e 1e 1e 1e 1e ae 3f
0.055556 is 1c c7 71 1c c7 71 ac 3f
0.052632 is 28 af a1 bc 86 f2 aa 3f