On my prog class we had this problem:
user 1 have 1.10 height
user 2 have 1.40 height
user 1 have 0.30 grow rate per year
user 2 have 0.15 grow rate per year
How many years will take for user1 to grow taller than user2?
It's because floating point values are not very precise. They are more like approximations. Not all floating point values can be stored exactly and calculations will have rounding errors.
To calculate the number of years you don't need to use a loop. You could use simple algebra and that would give you an answer that is very close to the correct answer (2).
user1 = 1.10 + year * 0.30
user2 = 1.40 + year * 0.15
user1 = user2
1.10 + year * 0.30 = 1.40 + year * 0.15
year * (0.30 - 0.15) = 1.40 - 1.10
year = (1.40 - 1.10) / (0.30 - 0.15)
year = 0.30 / 0.15
year = 2
#include <iostream>
#include <fstream>
#include <iomanip>
usingnamespace std;
void f1();
void f2();
void f3();
int main()
{
cout << fixed << setprecision(7);
f1();
f2();
cout << fixed << setprecision(16);
f3();
return 0;
}
void f1()
{
cout << "\n---------------- f1 ---------\n";
int year = 0;
float user1 = 1.10;
float user2 = 1.40;
float i = 0.15;
float x = 0.30;
while (user1 <= user2)
{
user1 = user1 + x;
user2 = user2 + i;
year++;
cout << "User1 have " << user1 << "m " << endl;
cout << "User2 have " << user2 << "m" << endl;
}
cout << "\nUser1 took " << year << " to grow taller than user2." << endl << endl;
}
void f2()
{
cout << "\n---------------- f2 ---------\n";
int year = 0;
float user1 = 1.10;
float user2 = 1.40;
//float i = 0.15;
//float x = 0.30;
while (user1 <= user2)
{
user1 = user1 + 0.30;
user2 = user2 + 0.15;
year++;
cout << "User1 have " << user1 << "m " << endl;
cout << "User2 have " << user2 << "m" << endl;
}
cout << "\nUser1 took " << year << " to grow taller than user2." << endl << endl;
}
void f3()
{
cout << "\n---------------- f3 ---------\n";
int year = 0;
double user1 = 1.10;
double user2 = 1.40;
double i = 0.15;
double x = 0.30;
while (user1 <= user2)
{
user1 = user1 + x;
user2 = user2 + i;
year++;
cout << "User1 have " << user1 << "m " << endl;
cout << "User2 have " << user2 << "m" << endl;
}
cout << "\nUser1 took " << year << " to grow taller than user2." << endl << endl;
}
---------------- f1 ---------
User1 have 1.4000001m
User2 have 1.5500000m
User1 have 1.7000000m
User2 have 1.6999999m
User1 took 2 to grow taller than user2.
---------------- f2 ---------
User1 have 1.4000000m
User2 have 1.5500000m
User1 have 1.6999999m
User2 have 1.6999999m
User1 have 1.9999999m
User2 have 1.8499999m
User1 took 3 to grow taller than user2.
---------------- f3 ---------
User1 have 1.4000000000000001m
User2 have 1.5499999999999998m
User1 have 1.7000000000000002m
User2 have 1.6999999999999997m
User1 took 2 to grow taller than user2.
Not really.
that was an exercise and i just got troubled by the diff results on what i thought was almost the same code, 1 using float variable 1 using direct value on calculation. (btw double type worked).
Just asked if there are a "precise" way, just to learn :)
If in any point i need to do some decimal operation with variables.
what should i use? double? function?
However the difficulty comes in testing whether or not two floating point values are equal. In some situations you may subtract one from the other and check whether the absolute (no sign) difference is less than a certain amount.