c++ why does ((1 / 2) * 2) return 0 -


this question has answer here:

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

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -