[Numpy-discussion] Re: numpy, overflow, inf, ieee, and rich , comparison

Alex Martelli aleaxit at yahoo.com
Wed Oct 25 04:32:52 EDT 2000


"Paul Prescod" <paulp at ActiveState.com> wrote in message
news:mailman.972450070.10196.python-list at python.org...
    [snip]
> case of floats, as you are taught to in high school. To a student, the
> output of this program is somewhat confusing:
>
> #include <stdio.h>
>
> void main(void){
>     printf("%f\n",5/2);
>     printf("%f\n",5.0/2);
>     printf("%f\n",5/2.0);
> }
>
> 0.000000
> 2.500000
> 2.500000

Pretty lucky to get 0.00000 here... the type mismatch between
the format-string to printf and the rest of the arguments leads
to undefined-behavior by C rules!-)  It's _probably_ not going
to bite, right here, but try a small variation...:

void main(void){
    printf("%d %f %d\n",1,5/2,7);
    printf("%d %f %d\n",2,5.0/2,8);
    printf("%d %f %d\n",3,5/2.0,9);
}

this happens to output

1 0.000000 4198773
2 2.500000 8
3 2.500000 9

on a given compiler/platform on which I tried it.  Now try
explaining *THAT* to a typical beginner...!-)


Alex






More information about the Python-list mailing list