C ++ double operator +

Possible duplicates:
Incorrect floating point math?
Failure to schedule compilation time?

Strange things that happen today, I'm going to lose it ...

#include <iomanip> #include <iostream> using namespace std; int main() { cout << setprecision(14); cout << (1/9+1/9+4/9) << endl; } 

This code outputs 0 on MSVC 9.0 x64 and x86 and on GCC 4.4 x64 and x86 (default parameters and strict math ...). And, as far as I remember, 1/9+1/9+4/9 = 6/9 = 2/3 != 0

+4
source share
8 answers

1/9 is zero because 1 and 9 are integers and are divisible by integer divisions. The same goes for 4/9 .

If you want to express floating point division through arithmetic literals, you need to either use floating point literals 1.0/9 + 1.0/9 + 4.0/9 (or 1/9. + 1/9. + 4/9. Or 1.f/9 + 1.f/9 + 4.f/9 ), or explicitly use one operand for the desired floating point type (double) 1/9 + (double) 1/9 + (double) 4/9 .

PS Finally, my chance to answer this question :)

+31
source

Use the decimal point in your calculations to force the use of floating point math, as well as one of these suffixes: fl FL on your numbers. One number without a decimal point and without one of these suffixes is not considered a floating point literal.

C ++ 03 2.13.3-1 on floating literals:

A floating literal consists of an integer part, a decimal point, a fraction part, e or E, optionally with an integer sign, and an additional type suffix. integer and fractional parts consist of a sequence of decimal (base ten) digits. Either the whole or part of the fraction (not both) may be omitted; either the decimal point, or the letter e (or E) and the exponent (not both) can be omitted. The integer part, the optional decimal point, and the optional fraction part form a significant part of the floating literal. The indicator, if present, indicates a power of 10 at which a significant portion should be scaled. If the scaled value is in the range of representable values ​​for its type, the result is a scaled value, if imaginable, otherwise the greater or lesser representable value of the nearest scaled value selected in a certain way. double literal type unless suffix explicitly specified. The suffixes f and F indicate float, the suffixes l and L indicate a long double. If the scaled value is not in the range of representable values ​​for its type, the program is poorly formed. 18

+6
source

They are all whole. So 1/9 is 0. 4/9 is also 0. And 0 + 0 + 0 = 0. Thus, the result is 0. If you want fractions, throw your fractions on floating ones.

+5
source
 1/9(=0)+1/9(=0)+4/9(=0) = 0 
+4
source

well, in C ++ (and many other languages) 1/9+1/9+4/9 is zero because it is integer arithmetic.

You might want to write 1/9.0+1/9.0+4/9.0

+3
source

Unless you specifically specify a decimal number, C ++ numbers use integers, so 1/9 = 4/9 = 0 and 0 + 0 + 0 = 0 .

You just need to add the decimal character 1.0 , etc.

+2
source

According to type C rules, you do all the math. 1/9 and 4/9 both truncate to 0 (as integers). If you wrote 1.0 / 9.0 etc., It will use double precision math and do what you want.

+2
source

You may have the habit of using more parentheses. They cost a little time, clearly state what you intend, and guarantee that you get what you want. Well, basically ...;)

-2
source

Source: https://habr.com/ru/post/1315201/


All Articles