比较两个不同的双精度数时返回true
Return true while comparing two different double numbers
输出中有双位数
344.6
563.4
455.6
10.7
10.6
1
最后两个显示
[0] 10.000000000000000 double
[1] 10.000000000000000 double
visual studio express 2015中的locals变量和
repRate[0]==repRate[1]
true
为什么?我试着用(双份)来确认。输出给我两个不同的数字,但是repRate[0]==repRate[1]返回true。
this is the input.
5
1 10 3456
2 10 5644
3 10 4566
4 20 234
5 20 232
5表示您有5个项目,1为id代码,10为初始数字,3456为最终数字。复制率是(3456-10)/10,我需要使用复制率作为索引对数组进行排序。
#include <iostream>
using namespace std;
int main() {
int n;
int a[110][4];
double repRate[110];
//input
cin >> n;
for (int i = 0; i < n;i++) {
cin >> a[i][0] >> a[i][1] >> a[i][2];
}
//rep rate
for(int i = 0;i < n;i++){
repRate[i] = (double) ((double)a[i][2]- (double)a[i][1]) / (double)a[i][1];
cout << repRate[i] << endl;
}
//sort
for (int i = 0;i < n;i++) {
for (int j = 0;j < n - i-1;j++) {
int temp[5];
int rtemp;
if (repRate[j]>repRate[j+1]) {
rtemp = repRate[j+1];
repRate[j + 1] = repRate[j];
repRate[j] = rtemp;
for (int k = 0;k < 4;k++) {
temp[k] = a[j+1][k];
}
for (int k = 0;k < 4;k++) {
a[j+1][k] = a[j][k];
}
for (int k = 0;k < 4;k++) {
a[j][k] = temp[k];
}
}
}
}
int test;
test = repRate[0] == repRate[1];
cout << test;
return 0;
}
尝试将rtemp
设置为double,因为这一行:
rtemp = repRate[j+1];
会减少一些准确性。如果没有启用警告,这应该是对编译器的警告!
之后输出如下所示:
5
1 10 3456
2 10 5644
3 10 4566
4 20 234
5 20 232
344.6
563.4
455.6
10.7
10.6
0
相关文章:
- 如何防止 c++ 在从浮点型转换为双精度型(不适用于 IO)时添加额外的小数?
- 正在将csv文件读取为双精度矢量
- 我可以信任表示整数的浮点或双精度来保持精度吗
- 如何在C++中的同一函数中使用字符串和双精度
- 特征::矩阵<双精度,1,3> 结构类型函数中的返回类型函数
- 检查是否以特定精度给出双精度
- 转换函数,将 std::数组的双精度作为参数或双精度作为参数单独转换
- C 字符串返回字符串的整数/双精度/长整型值
- 为什么将双精度转换为 int 似乎在第 16 位数字之后将其四舍五入?
- 如何使双精度值的 C++ 和 C# 中的结果相同
- 使用浮点数和双精度数的非常小数字的数学
- 使用 Xcode 将双精度存储在数组C++中
- 在 C++ 中将双精度变量写入二进制文件
- 如何从字符串转换为双精度*
- 为什么我的数组双精度函数不起作用?
- 高精度双精度的 Sprintf 格式化问题
- C++ cout 将双精度对齐到精度 2 并正确对齐
- Fmod 函数清楚地输出一个预期的双精度值,但 if(fmod == 预期的双精度值)的计算结果不是 true
- 比较两个不同的双精度数时返回true
- 浮点-如果一个实数可以用双精度表示,我如何编写一个返回true的C++函数