检查精度到小数点后第n位?

编程入门 行业动态 更新时间:2024-10-20 20:42:45
本文介绍了检查精度到小数点后第n位?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我必须计算热板的值,并且仅将其精确到小数点后一位。我很想尝试找出所有数组值更改后如何检查它们。我发现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位?

本文发布于:2023-07-06 08:01:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1047625.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:小数点   精度

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!