Warning in comparison of floats how to proceed

3

In an exercise I've made, you're asked to do a compound interest calculation with for for 5% , 6% , 7% , 8% , 9% , and 10%

As I could not use a counter check the counter being a type float , or even using switch , then I chose to do a conditional test where the user enters the value, but it generates the warning :

  

warning: comparing floating point with == or! = is unsafe [-Wfloat-equal]

I understand that the reason for the warning is for the comparison between floating points to be inaccurate, and for the%% flag to be active, but running the program does not generate any errors, if this way to perform the comparison generates a warning , what would be the correct way to compare two values of type -Wfloat-equal] .

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;
}
    
asked by anonymous 19.01.2016 / 00:57

1 answer

5

You really can not make a precise floating-point comparison. If you need this accuracy use another type. But if you want to minimize the problem, do a function that makes an approximation. Example:

bool isEqual(double x, double y) {
    return std::abs(x - y) <= 1e-5 * std::abs(x);
}

See running on ideone .

Font . Notice that the approximation is arbitrary.

    
19.01.2016 / 02:24