1 |
On Wednesday 29 October 2008 00:55:42 Jorge Peixoto de Morais Neto wrote: |
2 |
> >> I mean to really know C, |
3 |
> >> that is, read a rigorous book such as "C: A Reference Manual" and be |
4 |
> >> able to write portable programs with well-defined behavior. Speaking |
5 |
> >> of well-defined behavior, do you know what happens when you cast a |
6 |
> >> float to an int, and the float is too big to fit into the int? |
7 |
> > |
8 |
> > Did oyu try it yourself and see? |
9 |
> |
10 |
> The point is that the behavior in this situation is "undefined". It |
11 |
> might do anything. Programming in C is different than programming in |
12 |
> Python. |
13 |
|
14 |
Most likely the compiler will try to treat the float as an int and use the |
15 |
first 4 bytes of the float, ignoring the rest. |
16 |
|
17 |
This is insane though. I cannot think of any reason why one would ever want to |
18 |
treat the first 32 bits of a float as an int. It's not like you are casting a |
19 |
long to an int which can make sense - just discard the high bits. |
20 |
|
21 |
I reckon the standard would say this is undefined. Most compiler would bomb |
22 |
out with a compile error but give you an obscure flag to proceed anyway. If |
23 |
you want to commit suicide, C is quite happy to pass you the pills as long s |
24 |
you ask nicely |
25 |
|
26 |
-- |
27 |
alan dot mckinnon at gmail dot com |