nakiya
nakiya

Reputation: 14403

Why does division(?) yield this number?

Rephrasing question :

The following code (Not C++ - written in an in-house scripting language)

if(A*B != 0.0)
{
   D = (C/(A*B))*100.0;
}
else
{
   D = 0.0;
}

yields a value of

90989373681853939930449659398190196007605312719045829137102976436641398782862768335320454041881784565022989668056715169480294533394160442876108458546952155914634268552157701346144299391656459840294022732906509880379702822420494744472135997630178480287638496793549447363202959411986592330337536848282003701760.000000

for D. We are 100% sure that A != 0.0. And we are almost 100% sure that B == 0.0. We never use such infinitesimally small values (close to 0.0 but not 0.0) such as the value of B that this value of C suggests. It is impossible that it acquired that value from our data. Can A*B yield anything that is not equal to 0.0 when B is 0?

Upvotes: 1

Views: 249

Answers (3)

old_timer
old_timer

Reputation: 71506

Assuming you are using IEEE floating point numbers it is not a good idea to use equal or not equal in this case with floating point numbers. Even if the same value like -0.0 and +0.0 they are not equal from a bitwise perspective which is what the equate does. Even if using other float formats, equal and not equal are discouraged.

Instead put some sort of range on it e=a*b; if ((e<0.0002)||(e>0.0002) then...

Upvotes: 1

Necrolis
Necrolis

Reputation: 26171

This looks like you are accruing error from previous calculations, so you divison is by a really small decimal, but not zero. You should add a margin of error if you want to catch something like this, psuedocode: if(num < margin_of_error) ret inf;, or use the epsilon method to be even safer

Upvotes: 0

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798436

The number you divided by was not in fact 0, just very, very close.

Upvotes: 2

Related Questions