In C++, why does dividing 2 floating point numbers via 2 different mechanisms (inline and via a function call) output different results as per below?
After the following computations where 5 is divided into 4, f1 = 0 and f2 = 0.8.
#include <iostream>
using namespace std;
int main() {
float f1 = 4 / 5;
float f2 = FloatDivision(4, 5);
cout << "f1 equals " << f1 << endl;
cout << "f2 equals " << f2 << endl;
cin.ignore();
}
float FloatDivision(float f1, float f2) {
if (f2 != 0) {
return f1 / f2;
} else {
cout << "Error: The denominator cannot be zero" << endl;
return 0;
}
}