assert failure

K

ken.carlino

Hi,

I have the follwing code, but I can't understand why the assert fail?
I print out the value that I am check, it has the right value, but the
assert still fail.

Here is the code:
// bd.areaPerCent is of type 'float'

cout << a.areaPerCent << endl;
assert (a.areaPerCent == 0.00137389);

Here is the output of the program:
0.00137389
snapshot: ../TestBlockData.cpp:31: void TestBlockData::test1():
Assertion `a.areaPerCent == 0.00137389' failed.

why it fails?
 
S

Scott McPhillips [MVP]

Hi,

I have the follwing code, but I can't understand why the assert fail?
I print out the value that I am check, it has the right value, but the
assert still fail.

Here is the code:
// bd.areaPerCent is of type 'float'

cout << a.areaPerCent << endl;
assert (a.areaPerCent == 0.00137389);

Here is the output of the program:
0.00137389
snapshot: ../TestBlockData.cpp:31: void TestBlockData::test1():
Assertion `a.areaPerCent == 0.00137389' failed.

why it fails?

The == comparison is performed in binary floating point, not the ASCII
representation that you output. Translations between ASCII and binary
float are often inexact. It is a basic programming error to use == to
compare floats.
 
K

Kai-Uwe Bux

Hi,

I have the follwing code, but I can't understand why the assert fail?
I print out the value that I am check, it has the right value, but the
assert still fail.

Here is the code:
// bd.areaPerCent is of type 'float'

cout << a.areaPerCent << endl;
assert (a.areaPerCent == 0.00137389);

Here is the output of the program:
0.00137389
snapshot: ../TestBlockData.cpp:31: void TestBlockData::test1():
Assertion `a.areaPerCent == 0.00137389' failed.

why it fails?

http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.17

Best

Kai-Uwe Bux
 
B

Ben Pope

Howard said:
What's "feq" ??? (And who's Father Jack?)

Hmm, I thought feq was for floating point equals. It's not, my
subconscious made that up.

Father Jack is one of the characters from Father Ted, a UK Sitcom, he
shouts "Feck", "Arse", "Drink" and "Girls", whilst drunk (which is all
the time).

Anyway, apologies for the tangent, it's all based on a fictitious
function called feq.

Apologies, I'll create one to redeem myself!

#include <iostream>
#include <cmath>
#include <limits>

template<class T>
bool feq(T val1, T val2) {
return (fabs(val1 - val2) <= std::numeric_limits<T>::epsilon());
}

int main() {
float float1 = 1.2;
float float2 = 1.200000001;

double double1 = 1.2;
double double2 = 1.200000001;

std::cout << (feq(float1, float2) ? "equal\n" : "not equal\n");
std::cout << (feq(double1, double2) ? "equal\n" : "not equal\n");

return 0;
}

/me runs for cover

Ben Pope
 
P

Pete Becker

Thanks. Can you please tell me how should I compare float instead?

It depends on what you want to do. In some circumstances, == is just the
right thing. In others it's not. What are you trying to do?
 
T

Thomas Tutone

Is the STL equal_to<float> safe?
Yes.

Will it have the same problem?

Yes. In fact, it is defined in terms of ==.

Did you look at the FAQ that Kai-Uwe Bux referred you to?

Best regards,

Tom
 
R

Richard Herring

Ben Pope said:
Hmm, I thought feq was for floating point equals. It's not, my
subconscious made that up.

Father Jack is one of the characters from Father Ted, a UK Sitcom,

Irish. Didn't you notice the difference?
 
B

Ben Pope

Richard said:
Irish. Didn't you notice the difference?

OK, it was filmed in County Clare, for Channel 4, a UK TV company.

Apologies to anybody from Southern Ireland (or anywhere else) offended
by my calling Father Ted a "UK sitcom".

Ben Pope
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,135
Latest member
VeronaShap
Top