我必须计算热板的值,并且仅将其精确到小数点后一位。我很想尝试找出所有数组值更改后如何检查它们。我发现724次运行之后,到第4个小数点(打印了多少个)没有任何改变。
I have to calculate the values of a hot plate and have it accurate only to the first decimal place. I am stumped on trying to figure out how to check all the array values if they changed. I found out that 724 runs made no change after that to the 4th decimal (how many were being printed).
有没有一种方法可以只比较double变量直到
Is there a way to compare doubles variables only up to the n-th decimal place?
#include <iostream> #include <string> #include <fstream> using namespace std; const int ARRAY_SIZE = 20; const int NEIGHBORS = 4; void initialize(double hot_plate[][ARRAY_SIZE]); bool writeFile(const double HOT_PLATE[][ARRAY_SIZE], const string FILE_NAME); double sum_cell(const double HOT_PLATE[][ARRAY_SIZE], const int CELL_X, const int CELL_Y); int main() { double hot_plate[ARRAY_SIZE][ARRAY_SIZE]; initialize(hot_plate); string file_name = "hot_plate.csv"; //accuracy up to 4 decmials int runs = 724; while ( runs > 0) { for (int i = 0; i < ARRAY_SIZE; i++) { for (int j = 0; j < ARRAY_SIZE; j++) { if (i > 0 && i < ARRAY_SIZE - 1 && j > 0 && j < ARRAY_SIZE - 1) { hot_plate[i][j] = sum_cell(hot_plate, j, i); } } } runs--; } if (writeFile(hot_plate, file_name)) { cout << "File wrote correctly\n"; } else { cout << "The file did not write!\n"; } //system("pause"); return 0; } //////////////////////////////////////////////////////////////////////////////// //////////////////////////// Completed Code //////////////////////////////////// //////////////////////////////////////////////////////////////////////////////// double sum_cell(const double HOT_PLATE[][ARRAY_SIZE], const int CELL_X, const int CELL_Y) { /* This code should never go out of bounds as it's in an if statement if (i > 0 && i < ARRAY_SIZE - 1 && j > 0 && j < ARRAY_SIZE - 1) */ double cell_num = HOT_PLATE[CELL_X - 1][CELL_Y]; // Top cell_num += HOT_PLATE[CELL_X][CELL_Y - 1]; // Left cell_num += HOT_PLATE[CELL_X][CELL_Y + 1]; // Right cell_num += HOT_PLATE[CELL_X + 1][CELL_Y]; // Bottom cell_num /= NEIGHBORS; return cell_num; } // setup the Array so all values are defined when starting void initialize(double hot_plate[][ARRAY_SIZE]) { for (int i = 0; i < ARRAY_SIZE; i++) { for (int j = 0; j < ARRAY_SIZE; j++) { if (i == 0 || i == ARRAY_SIZE - 1) { if (j == 0 || j == ARRAY_SIZE - 1) { hot_plate[i][j] = 0.0; } else { hot_plate[i][j] = 100.0; } } else { hot_plate[i][j] = 0.0; } } } } // Write the data to the CSV file bool writeFile(const double HOT_PLATE[][ARRAY_SIZE], const string FILE_NAME) { // open the file ofstream fout(FILE_NAME); if (fout.fail()) return false; for (int i = 0; i < ARRAY_SIZE; i++) { for (int j = 0; j < ARRAY_SIZE; j++) { fout << HOT_PLATE[i][j]; if ( j < ARRAY_SIZE - 1) { fout << ", "; } else if (i != ARRAY_SIZE - 1) { fout << endl; } } } // close the input stream from the file. fout.close(); return true; }推荐答案
是否有办法只将双精度变量比较到小数点后第n位?
Is there a way to compare doubles variables only up to the n-th decimal place?
是,请检查绝对值它们之间的差值小于10 ^ -n。
Yes there is, check whether the absolute value of the difference between them is less than 10^-n.
更多推荐
检查精度到小数点后第n位?
发布评论