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

Check precision to nth decimal place?

本文关键字:小数点 精度 检查      更新时间:2023-10-16

我必须计算热板的值,并使其精确到小数点后第一位。我被困在试图找出如何检查所有的数组值,如果他们改变。我发现724次运行之后,小数点后4位没有变化(打印了多少)。

是否有一种方法可以只比较到小数点后n位的双精度变量?

#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 correctlyn";
    }
    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位的双精度变量?

有,检查两者差的绝对值是否小于10^-n

检查浮点数与deniweb上的这篇文章的比较

使用这个函数

double getUpToDecPlace (double value, int decPlace)
{
    int dec = 1;
    for (int i = 0; i < decPlace; i++)
    {
        dec *= 10;
    }
    return floor(value*dec + 0.5)/dec;
}

对于getUpToDecPlace(12.345678, 2)将返回12.35,您可以比较double s到任意小数点:

double var1 = 12.345678;
double var2 = 12.351234;
bool comp1 = (getUpToDecPlace(var1, 2) == getUpToDecPlace(var2, 2)); // true
bool comp2 = (getUpToDecPlace(var1, 3) == getUpToDecPlace(var2, 3)); // false

这里有这么多问题。

  1. 您正在就地更新hot_plate数组。所以你在"上一代"中使用的一些值已经在当前一代中更新了!你必须在一个单独的数组中计算每一代,然后将其复制回'主' hot_plate数组。

  2. 如果您希望最终结果精确到小数点后第一位,那么直到值的变化不超过0.1才继续是不够的。例如,有些值在十代之后可能会改变0.05以上,这相当于改变了0.5以上。事实上,这是一个非常棘手的问题:它需要对初始条件如何随时间演变进行全局分析。

  3. 你确定你的sum_cell正确吗?下一代hot_plate[i][j]的温度肯定取决于hot_plate[i][j]的当前值,而不仅仅取决于它的邻居。

还有,这看起来有点傻:

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)

我建议等价的表述:

for (int i = 1; i < ARRAY_SIZE - 1; i++)
{
    for (int j = 1; j < ARRAY_SIZE - 1; j++)

至于检验是否等于小数点后第n位,其他海报已经涵盖了。

将测量的FP值存储为缩放后的整数:

ix = int(fp * 10000)

然后,您可以根据所需的精度进行直接比较。