Answer the question
In order to leave comments, you need to log in
Why does static_cast return wrong value?
Good day. I study C++ according to R. Laforet's book "OOP in C++".
There was a problem in the program
#include <iostream>
#include <clocale>
using namespace std;
int main()
{
setlocale(LC_CTYPE, "rus");
float a, c, k;
int b, z;
cout << "Введите количество фунтов: " << endl;
cin >> a;
b = static_cast<int>(a);
c = a - b;
c *= 20;
z = static_cast<int>(c);
k = c - z;
k *= 12;
k = static_cast<int>(k);
cout << "Эквивалентная сумма в старой форме записи: J" <<b<<"."<<z<<"."<<k<< endl;
return 0;
}
c *= 20;
z = static_cast<int>(c);
Answer the question
In order to leave comments, you need to log in
0.65 cannot be represented in memory as exactly 0.65
Use double instead of float
0.65 = 65 / 100 = 13 / (2 * 2 * 5). This fraction cannot be represented in the final form as a binary number, since its denominator contains numbers other than 2. Therefore, calculations with its participation are approximate.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question