30 Sep
2004
30 Sep
'04
22:52
Hallo, Am Fri, 01 Oct 2004, volker wirth schrieb:
Hallo, das hört sich vermutlich etwas seltsam an, aber: ich benutze Dev-C++4 mit cygnus; Code:
float z = (1 / 2); printf("%f", z);
und er sagt mir: 0.000000
Bei anderen Zahlen ist es genauso. Das hat mich doch _etwas_ überrascht.
Das liegt daran, dass du zwei ints dividierst und erst _dann_ das Ergebnis (und das ist 0) in einen float gewandelt wird. Explizit: float z = (float)( (int) 1 / (int) 2 ); Du willst (explizit): float z = ( (float) 1 / (float) 2 ); oder: float z = ( 1.0f / 2.0f ); oder: float z = ( 1.0 / 2.0 ); HTH, -dnh -- "What I saw in the Xerox PARC technology was the caveman interface, you point and you grunt." -- Eben Moglen