0

I'm creating functional tests to my program, but for some reason when I wrote system1->getValue() - 36.6033 I got a crazy number like -6.58727e-005. I've done some debbuging and system1->getValue() is returning 36.6032, but the subtraction isn't resulting in 0.0001.

Here is the code:

void exponentialFunctionalTest(){
    Model* model = new Model("Exponential test");
    System* system1 = new System("System 1", 100);
    System* system2 = new System("System 2", 0);
    Exponential* exponential = new Exponential("Exponential", system1, system2);
        
    model->add(system1);
    model->add(system2);
    model->add(exponential);
    model->run(0, 100, 1);

    assert(abs(system1->getValue() - 36.6032) < 0.0001);
    assert(abs(system2->getValue() - 63.3968) < 0.0001);

    delete model;
}

In this program I have two systems, one has a variable value that starts with 100 and the other 0. Also, I have an exponential flow that will affect both system's values 100 times. The first system is returning 36.6032 at the end, as it should be, but when I try to use the assert() function like my teacher taught me, it isn't working, it keeps passing the test that is suppose to fail.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Paifo
  • 1
  • 2
    Floating point numbers aren't exact. They're just an approximation. If you rely on two floating point numbers being exactly the same, you're doing it wrong. – Jonathan S. May 08 '22 at 00:33
  • 2
    `-6.58727e-005` is `-0.0000658727` (shift the decimal point to the left 5 digits). This rounds to `-0.0001`, so it fits your data up to the precision with which you reported it. (A `double` will not store `36.6032` exactly.) So looks like a correct result to me. – JaMiT May 08 '22 at 00:37

0 Answers0