断言失败 [英] assert failure

查看:175
本文介绍了断言失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我有以下代码,但我不明白为什么断言失败?

我打印出我的价值我检查,它有正确的价值,但

断言仍然失败。


这是代码:

// bd.areaPerCent的类型为''浮动''

cout<< a.areaPerCent<< endl;

断言(a.areaPerCent == 0.00137389);


这是节目的输出:

0.00137389

snapshot:../ TestBlockData.cpp:31:void TestBlockData :: test1():

断言`a.areaPerCent == 0.00137389''失败。


为什么会失败?

解决方案

ke ********* @ gmail.com 写道:



我有以下代码,但我不明白为什么断言失败?
我打印出我检查的值,它有正确的值,但
断言仍然失败。

以下是代码:
// bd.areaPerCent类型为''float''

cout<< a.areaPerCent<<断言;
断言(a.areaPerCent == 0.00137389);

以下是该程序的输出:
0.00137389
snapshot:../ TestBlockData.cpp: 31:void TestBlockData :: test1():
断言`a.areaPerCent == 0.00137389''失败。

为什么失败?




==比较是以二进制浮点执行的,而不是您输出的ASCII

表示。 ASCII和二进制之间的翻译

float通常是不准确的。这是一个基本的编程错误使用==到

比较花车。


-

Scott McPhillips [VC ++ MVP]


谢谢。你能否告诉我如何比较浮动?


ke ********* @ gmail.com 写道:



我有下列内容代码,但我不明白为什么断言失败?
我打印出我检查的值,它有正确的值,但
断言仍然失败。
// bd.areaPerCent类型''浮动''

cout<< a.areaPerCent<<断言;
断言(a.areaPerCent == 0.00137389);

以下是该程序的输出:
0.00137389
snapshot:../ TestBlockData.cpp: 31:void TestBlockData :: test1():
断言`a.areaPerCent == 0.00137389''失败。

为什么失败?



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


Best


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?

解决方案

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]


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


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


这篇关于断言失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆