Anfänger: Warum 1.0 statt 1 beim dividieren?
-
#include <iostream> #include <math.h> using namespace std; int main() { double f; f = 1.0/500000000; cout << f << endl; return 0; }
warum 1.0 statt 1 ?
LG
-
Weil:
1.0 ist double
1.0f ist float
1 ist int.1/5 (int/int) -> int
1.0/5 (double/int) -> doubleProbiers mal aus.
-
Wichtig für dich: das Ergebnis der Division hängt NICHT davon ab, was für ein Typ links vom =-Zeichen steht. Gegebenenfalls wird NACH der erfolgten Berechnung der rechten Seite das Ergebnis in den Typ links vom =-Zeichen konvertiert.
Wenn du also eine double-Variable d und zwei int-Variablen i1 und i2 hast, wird bei
d = i1 / i2
erst i1/i2 (int/int=int) berechnet und das Ergebnis erst danach in double konvertiert.