Re: C: Dividieren
David Haller <[email protected]>
| Newsgroups | gmane.linux.suse.programming |
|---|---|
| Message-ID | <[email protected]> |
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
--
Um die Liste abzubestellen, schicken Sie eine Mail an:
[email protected]
Um eine Liste aller verfÃŒgbaren Kommandos zu bekommen, schicken
Sie eine Mail an: [email protected]