I have the following code:
#include <iostream>
#include <string>
#include <math.h>
using namespace std;
double sysmoroundtoabnt(double value, int digits)
{
double potencia, valorElevado, auxiliar;
int parteInteira, parteFracionada;
potencia = pow(10, abs(digits));
valorElevado = value * (potencia);
parteInteira = trunc(valorElevado);
parteFracionada = trunc(modf(valorElevado, &auxiliar) * 100);
return parteFracionada;
}
int main()
{
cout << sysmoroundtoabnt(1.015, 2);
}
I set "1.015" to first param's value. When I'm debbugin, that value was change to "1.0149999999999999".
At this code, I set the integer part of value
to parteInteira
and the decimal part to parteFracionada
. Both as int.
So when I return parteFracionada
the result is 49, but it must be 50.
Why it happens and how can I solve it ?