Created
September 19, 2013 23:06
-
-
Save natemcmaster/6631090 to your computer and use it in GitHub Desktop.
Demo of integer math vs. double math
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
int main(){ | |
int half = 3/2; | |
cout << half << endl; //Prints 1 because int's throw away the decimal | |
double intMath= 3 / 2; | |
cout<<intMath<<endl; //Prints 1, not what we expected | |
double doubleMath = 3 / 2.0; //Notice that we wrote 2.0, not 2 | |
cout <<doubleMath <<endl; // Prints 1.5 | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment