Faced such a problem that VS in C ++ rounds the numbers that it gets after all the calculations. I don't need this. How to make it give accuracy up to 10 ^ -14? Because the errors are about there. I used float for all numbers. Then I tried double. there was no difference.

#include "stdafx.h" #include <iostream> #include <fstream> using namespace std; int main() { ifstream fmatrix("C:\\Users\\Pavel\\Desktop\\cpp1\\matrix.txt"); ifstream ffree("C:\\Users\\Pavel\\Desktop\\cpp1\\free.txt"); double matrix[30][30]; double matrixCopy[30][30]; double free[30]; double x[30]; double freeNew[30]; for (int i = 0; i < 30; i++) for (int j = 0; j < 30; j++) fmatrix >> matrix[i][j]; for (int i = 0; i < 30; i++) for (int j = 0; j < 30; j++) matrixCopy[i][j]=matrix[i][j]; for (int i = 0; i < 30; i++) ffree >> free[i]; for (int k = 0; k < 29; k++) //Прямой ход Гаусса { for (int j = k + 1; j < 30; j++) { double r = matrix[j][k] / matrix[k][k]; for (int i = k; i < 30; i++) matrix[j][i] -= matrix[k][i] * r; free[j] -= free[k] * r; } } for (int i = 0; i < 30; i++) x[i] = 0; for (int k = 29; k >= 0; k--)//Обратный ход Гаусса { double r = 0; for (int j = k + 1; j < 30; j++) { double g = matrix[k][j] * x[j]; r += g; } x[k] = (free[k]-r) / matrix[k][k];; } for (int i = 0; i < 30; i++) freeNew[i] = 0; for (int i = 0; i < 30; i++) for (int j = 0; j < 30; j++) freeNew[i] += matrixCopy[i][j] * x[j]; for (int i = 0; i < 30; i++) cout << freeNew[i] << endl; system("pause"); return 0; } 
  • How do you determine rounding occurs? - tutankhamun
  • @tutankhamun We were told on the pair that we had already checked all the 30x30 matrices - the answers should be exactly with minimal error. Our goal is to pull out this error. - PavelKas
  • one
    There is a float, there is a double, and there are special methods for working with large digits. There are complex numbers, there is a work with the numbers represented in the array, "manual" multiplication / division. Decide on the task, you can even specify the formula that you consider. And preferably with a technique. If you study working with numbers with this method, then you need this method. Write a piece of code where you think the result. - nick_n_a
  • 6
    I have a bad suspicion that you mean the output of values ​​on a screen with 6 characters, and call it rounding ... If you output through cout<< , then look towards setprecision ... - Harry
  • one
    Show that code. - isnullxbh

1 answer 1

it is possible that in a binary representation your number is an infinite periodic fraction. And with the final length of the mantissa can not be represented accurately. try other numbers or all the same long double, which has 20 decimal places

 cout<< setprecision(14) << freeNew[i] << endl; // 14, потому что вы 14 знаков хотите после запятой