我想创建一个循环链表,我需要在其中找到最大值和最小值,所以我需要一些检查点来停止,我想在第一个元素中使用-0(它只是一个检查点)不是别的什么)。 我不想使用任何其他值,因为用户可以将新数据输入到列表中,如果用户输入-0我将简单地将其替换为0,因为从数学方面来看它们之间没有区别(我不会永远使用 - 小数字:))。 我使用整数值。
现在问题不盈利。 我尝试了,结果是相同的:
#include <iostream>
using namespace std;
int main() {
if(0 != -0){
cout << "they are different!";
}else{
cout << "they are same";
}
return 0;
}
感谢所有人。
答案 0 :(得分:3)
与数学0
和-0
中的值相同。您无法以任何方式区分它们,因此您可以在描述时使用此条件。
答案 1 :(得分:2)
几乎通用的二进制补码系统中的整数值不能为负零 - 没有它们的表示。负零根本就不存在。
IEEE浮点数可能会出现负零,但不会经常发生。将负数乘以0.0即可。
我建议找另一个哨兵值。
答案 2 :(得分:1)
这需要你将整体存储为双打,记住。但你可以这样做。
signbit可能就是这个伎俩。如果没有,尝试采取倒数。 1 / + 0.0 = + inf,1 / -0.0 = -inf。作为最后的努力,做一些像:
double d=-0.0;
bool is_neg = (*(uint64_t*)&d)&0x8000000000000000;
答案 3 :(得分:0)
使用NaN作为检查点:
#include <iostream>
#include <limits>
struct cll {
double value;
cll * next;
};
int main() {
int N = 5;
double input[] = {1,2,3,4,5};
cll * first = new cll;
first->value = std::numeric_limits<double>::quiet_NaN();
cll * next = first;
for (int i = 0; i < N; ++i) {
cll * last = new cll;
next->next = last;
last->value = input[i];
last->next = first;
next = last;
}
next = next->next;
next = next->next;
while (next->value == next->value) {
next = next->next;
}
next = next->next;
while (next->value == next->value) {
std::cout << next->value << '\n';
next = next->next;
}
return 0;
}