5
In an exercise I did, you are asked to make a compound interest calculation with for for the values of 5%, 6%, 7%, 8%, 9%, and 10%.
As I could not use a check per counter being the counter a type float
, nor use switch
, then I chose to make a conditional test where the user inserts the value, but generates me the Warning:
Warning:Comparing floating point with == or != is unsafe [-Wfloat-Equal]
I understand the reason for Warning is by comparison between floating points be inaccurate, and by the flag -Wfloat-equal]
be active, but when "running" the program is not generated me any error, if this way of performing the comparison generates a Warning, what would be the correct way to compare two values of the type float
.
Follow the code for evaluation:
#include <iostream>
#include <iomanip>
#include <cmath>
using std::cout;
using std::cin;
using std::endl;
using std::fixed;
using std::setw;
using std::setprecision;
using std::pow;
int main()
{
//quantia em depósito no fim de cada ano.
double deposito;
//quantia inicial antes dos juros.
double principal = 1000.0;
//taxa de juros.
double taxa;
cout<<"Insira a taxa de juros entre 0.05 á 0.10: ";
cin>>taxa;
//exibe cabeçalhos.
cout << " Ano(s) " << setw(21)<<"Quantidade depositada"<<endl;
//configura o formato de saída do valo de ponto flutuante.
cout << fixed << setprecision(2);
//calcula a quantia de depósito para cada um dos dez anos.
for( int ano = 1; ano <= 10; ano++ )
{
if(taxa == .05)
{
//calcula a nova quantia durante o ano especificado
deposito = principal * pow(1.0 + taxa, ano);
//exibe o ano e a quantia
cout << setw(4) << ano << setw(21) << deposito << endl;
}
else if(taxa == .06)
{
deposito = principal * pow(1.0 + taxa, ano);
cout << setw(4) << ano << setw(21) << deposito << endl;
}
else if(taxa == .07)
{
deposito = principal * pow(1.0 + taxa, ano);
cout << setw(4) << ano << setw(21) << deposito << endl;
}
else if(taxa == .08)
{
deposito = principal * pow(1.0 + taxa, ano);
cout << setw(4) << ano << setw(21) << deposito << endl;
}
else if(taxa == .09)
{
deposito = principal * pow(1.0 + deposito, ano);
cout << setw(4) << ano << setw(21) << deposito << endl;
}
else if(taxa == .10)
{
deposito = principal * pow(1.0 + taxa, ano);
cout << setw(4) << ano << setw(21) << deposito << endl;
}
else
{
//para que não seja recebido um valor além do especificado.
cout<<"O valor de taxa inserido é inválido. ";
return 0;
}
}
return 0;
}
Now I just figured out what it is 1e-5 on the page you sent me, refers to machine epsilon, used specifically for this, simple and complete reply thank you.
– Diego
Just to clarify a little: 1e-5 is nothing more than 10 -5 or 0.00001. It is a value that, in this context, is being used to compare the absolute difference between x and y. Epsilon is a Greek letter that usually has this meaning. Therefore, it is common to call variables that keep this Epsilon value. (http://math.stackexchange.com/a/176784)
– cantoni