简体   繁体   English

为什么说真假?

[英]Why true statement false?

I have a class , vector which inherited from a "raw vector" 我有一个类,从“原始向量”继承的向量

struct vector2raw {
    real_t x, y;
};
struct vector2 : public vector2raw {
    vector2() { null(); }
    vector2(real_t x, real_t y) { this->x = x; this->y = y; }
    vector2(const vector2 &v) { x = v.x; y = v.y; }
and so on

Now I want to compare two numbers, one vy=4 from v = (5.41, 4), another min.y = 4 from min=(4,4).This is only the strange case when I compare two equal numbers, other cases are executed correctly. 现在我想比较两个数字,一个是v =(5.41,4)的vy = 4,另一个是min =(4,4)的min.y = 4,这是我比较两个相等的数字时的奇怪情况。案件被正确执行。 I get always false on (4>=4) ( vy>=min.y) . 我在(4> = 4)(vy> = min.y)上总是错误的。 What can be the problem? 可能是什么问题?

real_t is defined to double UPD: this is written in C++ real_t被定义为UPD的两倍:这是用C ++编写的

Apparently (you're not giving a reproducible example) you're comparing floating point numbers with == . 显然(您没有给出可复制的示例),您正在将浮点数与==进行比较。

That's an ungood idea unless those numbers happen to be integral values, and for beginners it's an ungood idea in general. 除非这些数字恰好是整数值,否则这不是一个好主意,对于初学者来说,这通常不是一个好主意。

Two floating point values can appear to be equal, eg they give the same presentation when you don't request presentation of additional decimals, while in reality they differ in some otherwise very insignificant digit. 两个浮点值可能看起来是相等的,例如,当您不要求提供其他小数位时,它们会给出相同的表示形式,而实际上,它们的差值在一些其他方面却微不足道。

In the old days beginners who encountered this problem used to be referred to “What every scientist should know about floating point numbers” (or thereabouts, title from fallible memory). 在过去,遇到此问题的初学者曾经被称为“每个科学家都应该知道的有关浮点数的知识”(或大约有关易失性记忆的标题)。

In the last few years I have been criticized for giving that reference, because real technical stuff and so on is, allegedly, too hard for today's students. 在过去的几年中,我因提供该参考文献而受到批评,因为据称对于当今的学生来说,真正的技术知识等太难了。 And people have suggested more easy to digest alternatives, sort of like Wikipedia's simple edition. 人们提出了更容易理解的替代方案,类似于Wikipedia的简单版。 However, I can't remember any of them. 但是,我不记得其中任何一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM