简体   繁体   中英

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. I get always false on (4>=4) ( vy>=min.y) . What can be the problem?

real_t is defined to double UPD: this is written in 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. However, I can't remember any of them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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