c++ - 比较两个不同的双数时返回真

标签 c++

输出中有双数

344.6
563.4
455.6
10.7
10.6
1

最后两场演出<​​/p>

    [0] 10.000000000000000  double
    [1] 10.000000000000000  double

在 visual studio express 2015 的 locals 变量中,和

repRate[0]==repRate[1]
true

为什么?我尝试使用 (double) 来确定。输出给了我两个不同的数字,但 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 设置为两倍,因为这一行:

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

其中 0 是您想要的。

关于c++ - 比较两个不同的双数时返回真,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32471813/

相关文章:

C++ cout 字符串打印其内存值

c++ - 使用 Intel 的数学内核库进行特征分解的运行时问题

c++ - Visual C++ 没有运算符 ">>"与这些操作数匹配

c++ - g++ -lcurl 说找不到,但我可以在 ldconfig -p 中看到它

c++ - 为什么 const int 适合 char 大括号 init?

c++ - 如何在 C++ 中使用 XCode 4.2 设置 OpenGL 项目?

c++ - OpenCV 安装问题窗口

c++ - TensorFlow CPU 和 CUDA 代码共享

c++ - 重载不明确(int -> int64_t vs int -> double)

c++ - g++ 在 Windows 上不使用通配 rune 件名进行编译