检查精度到小数点后第n位
Check precision to nth decimal place?
我必须计算热板的值,并使其精确到小数点后第一位。我被困在试图找出如何检查所有的数组值,如果他们改变。我发现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
这里有这么多问题。
-
您正在就地更新
hot_plate
数组。所以你在"上一代"中使用的一些值已经在当前一代中更新了!你必须在一个单独的数组中计算每一代,然后将其复制回'主'hot_plate
数组。 -
如果您希望最终结果精确到小数点后第一位,那么直到值的变化不超过0.1才继续是不够的。例如,有些值在十代之后可能会改变0.05以上,这相当于改变了0.5以上。事实上,这是一个非常棘手的问题:它需要对初始条件如何随时间演变进行全局分析。
-
你确定你的
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)
然后,您可以根据所需的精度进行直接比较。相关文章:
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 如何在C++中只打印小数点而不设置限制,但动态设置精度
- 双精度 10 小数点精度
- 如何在 C/C++ 中有效地计算双倍到两个小数点精度
- 使用设置精度在C++中舍入小数点时出现问题
- 如何使双精度始终具有三个小数点?C++
- 在小数点之前设置数字的精度
- 小数点后的数字,用于双精度变量/C++计算
- 双精度和小数点
- 变体浮点数到双精度值转换,四舍五入到小数点后 1 位
- 将双精度值的格式设置为小数点后 1 位
- C++将双精度中的每个小数点制作成数组元素
- 如何将双精度四舍五入到小数点后n位c++
- 如何在小数点后保留0,将字符串转换为双精度
- 如何在c++中设置输出精度为小数点后2位
- 即使使用长双精度(c++),结果也只有小数点后5位
- 检查精度到小数点后第n位
- 我在试着编写一种疾病在不同年龄段的发病率,我需要把它写成一个百分数它的精度是小数点后两位
- 将双精度值四舍五入到小数点后3位
- 如何将双精度转换为小数点后两位