Why do I get unexecpted computation results using std::pow?

  Kiến thức lập trình

I am running the following code to compute -1.9 * 10.

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <math.h>

using namespace std;

int main() {
    float x = -1.9;
    int value = x * (int) std::pow(10, 1);
    cout << value << endl;
    return 0;
}

The expected result should be -19, but I get -18 instead.
What is the reason and how can I fix it forever?

I have tried to convert float into int and also replace std::pow with other methods.
Both solve the problem, and why?

2

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT