我需要一些关于比较不同维度的两个数组的值的帮助。
float gain[21][3]; // job_count is 21 and processor_count is 3
float numbers[MAX];
int arraysize = 21; // job_count = arraysize
// It a one dimensional sorted array
cout << "Sorted Numbers:" << endl;
for (int i = 0; i < arraysize; i++) {
cout << numbers[i] << endl;
}
// It is a 2 dim array
for (int i = 0; i < job_count; i++) {
// cout << "Job: " << i + 1 << endl;
for (int j = 0; j < processor_count; j++) {
if ((j + 1) == (jobs[i].processor_exec + 1)) {
cout << " " << endl;
} else {
gain[i][j] = {(jobs[i].criticality *
(1 - (1 - jobs[i].reliability) *
(1 - jobs[i].processors[j].new_reliability))) -
(jobs[i].criticality * jobs[i].reliability)};
if (numbers[i] == gain[i][j]) {
cout << numbers[i] << " is the Maximum gain is found for "
<< "Job" << i + 1 << "on Processor" << j + 1 << endl;
}
}
}
}
}
我需要将数字数组的每个值与增益数组的每个值进行比较,并返回匹配值的适当作业 ID 和处理器 ID。上面的代码没有进入if语句内部。即使我硬编码如下:- if(numbers[0] ==gain[1][2]) // 当我知道numbers[0]和gain[1][2]都有相同的值时。 还是行不通。 任何帮助表示赞赏。谢谢!
由于精度限制,直接比较浮点数可能会出现问题。
不要直接与
==
进行比较,而是检查绝对差异是否低于一个小阈值:
const float EPSILON = 1e-6; //
bool compFloat(float a, float b) {
return std::abs(a - b) < EPSILON;
}