473,406 Members | 2,956 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

assert failure

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?

Feb 14 '06 #1
11 2350
ke*********@gmail.com wrote:
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.

--
Scott McPhillips [VC++ MVP]

Feb 14 '06 #2
Thanks. Can you please tell me how should I compare float instead?

Feb 14 '06 #3
ke*********@gmail.com wrote:
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-lit...html#faq-29.17

Best

Kai-Uwe Bux
Feb 14 '06 #4
ke*********@gmail.com wrote:
Thanks. Can you please tell me how should I compare float instead?


<in Father Jacks voice>

feq!

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Feb 14 '06 #5

"Ben Pope" <be***************@gmail.com> wrote in message
news:43**********************@taz.nntpserver.com.. .
ke*********@gmail.com wrote:
Thanks. Can you please tell me how should I compare float instead?


<in Father Jacks voice>

feq!


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

-Howard

Feb 14 '06 #6
Howard wrote:
"Ben Pope" <be***************@gmail.com> wrote in message
news:43**********************@taz.nntpserver.com.. .
ke*********@gmail.com wrote:
Thanks. Can you please tell me how should I compare float instead?

<in Father Jacks voice>

feq!


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
--
I'm not just a number. To many, I'm known as a string...
Feb 14 '06 #7
ke*********@gmail.com wrote:
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?

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Feb 14 '06 #8
Is the STL equal_to<float> safe?
Will it have the same problem?

Thank you.

Kai-Uwe Bux wrote:
ke*********@gmail.com wrote:
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-lit...html#faq-29.17

Best

Kai-Uwe Bux


Feb 14 '06 #9
ke*********@gmail.com wrote:
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

Feb 14 '06 #10
In message <43**********************@taz.nntpserver.com>, Ben Pope
<be***************@gmail.com> writes
Howard wrote:
"Ben Pope" <be***************@gmail.com> wrote in message
news:43**********************@taz.nntpserver.com ...
ke*********@gmail.com wrote:
Thanks. Can you please tell me how should I compare float instead?
<in Father Jacks voice>

feq! 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,


Irish. Didn't you notice the difference?
he shouts "Feck", "Arse", "Drink" and "Girls", whilst drunk (which is
all the time).


--
Richard Herring
Feb 15 '06 #11
Richard Herring wrote:
In message <43**********************@taz.nntpserver.com>, Ben Pope
<be***************@gmail.com> writes

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


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
--
I'm not just a number. To many, I'm known as a string...
Feb 15 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
by: Fábio Mendes | last post by:
I'm sorry if it's an replicate. Either my e-mail program is messing with things or the python-list sent my msg to /dev/null. I couldn't find anything related in previous PEP's, so here it goes a...
12
by: Christian Christmann | last post by:
Hi, assert and error handling can be used for similar purposes. When should one use assert instead of try/catch and in which cases the error handling is preferable? I've read somewhere that...
11
by: David | last post by:
I am compiling C++ under both MacOsX and under Bloodshed Dev C++ on Windows XP. Because the console window disappears under Bloodshed when using the standard assert() via #include <cassert> I...
47
by: Rob Thorpe | last post by:
In general, is it considered bad practice to use asserts in production code? What about writing a macro that does the same as assert but continues to work regardless of the state of NDEBUG? I...
8
by: priyanka | last post by:
Hi there, Can anyone show me how the assert() function works ? I need to develop my own assert() function instead of using the one defined in the assert.h file. It would be great if anyone could...
29
by: mailforpr | last post by:
Sometimes, I can't think of any good reason why I should have the program's logic thrown an exception. Except for catching the exception and printing "Uh, oh" to the screen. I also think that in...
10
by: Paul Rubin | last post by:
So I have some assert statements in my code to verify the absence of some "impossible" conditions. They were useful in debugging and of course I left them in place for "real" runs of the program. ...
8
by: werasm | last post by:
Hi all, Care to share your thoughts on this, or point me to some thoughts already shared. My thoughts are like this (from a systems point of view): When I have logic errors outside of my...
1
by: murali026 | last post by:
Hi All, I have a code shown below. Could you please help me in assisting with the functionality of the assert. The code is very huge and here i am explaining here with a small portion of it....
32
by: bingfeng | last post by:
hello, please see following two code snatches: 1. int foo (const char* some) { assert(some); if (!some) { return -1; //-1 indicates error }
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.