c++ why does ((1 / 2) * 2) return 0 -
this question has answer here:
- why division result in 0 instead of decimal? 6 answers
- integer division 0 [duplicate] 1 answer
first time i'v posted here must know what's wrong simple peace of code:
#include <iostream> using namespace std; int main() { double test = (1 / 2) * 2; cout << test << endl; return 0; }
when ever run code displays 0, should casting something, happens regardless of compiler use , returns stranger results if '1' divided form of decimal.
because in integer maths 1 / 2 == 0
, 0 * 2 == 0
.
try 1.0
, 2.0
instead.
Comments
Post a Comment