制作演员时发出警告 [英] warning while making cast

查看:87
本文介绍了制作演员时发出警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


给出一个简单的代码:


#include< math.h>

long hyp(长高,长基)

{

返回sqrt(高度*高度+基数*基数);

}


int main(无效)

{

长h = hyp(10,20);

返回0;

}


我在Debian 4.0上编译它(gcc(GCC)4.1.2 20061115(预发布)(Debian

4.1.1-21)):

#gcc -ansi -pedantic -W -Wall -lm hyp.c


据我所知,根据标准sqrt返回double,但我们将
结果分配给long,导致信息丢失。然而我没有得到

警告,为什么会这样?


希望这是严格的C相关问题。


最诚挚的问候,Roman Mashak。

Hello,

given a simple code:

#include <math.h>
long hyp(long height, long base)
{
return sqrt(height * height + base * base);
}

int main(void)
{
long h = hyp(10, 20);
return 0;
}

I compile this on Debian 4.0 (gcc (GCC) 4.1.2 20061115 (prerelease) (Debian
4.1.1-21)):
#gcc -ansi -pedantic -W -Wall -lm hyp.c

As I understand, according to standard sqrt returns double, but we assign
result to long, resulting in information loss. Nevertheless I get no
warning, why is it so?

Hope it''s strictly C related question.

With best regards, Roman Mashak.

推荐答案

6月6日上午6:04,Roman Mashak < m ... @ tusur.ruwrote:
On Jun 6, 6:04 am, "Roman Mashak" <m...@tusur.ruwrote:

你好,


给出一个简单的代码:


#include< math.h>

long hyp(长高,长基)

{

返回sqrt(高*高+基*底);


}


int main(无效)

{

长h = hyp(10,20);

返回0;


}

我在Debian 4.0上编译(gcc(GCC)4.1.2 20061115(预发布)(Debian

4.1.1-21)):

# gcc -ansi -pedantic -W -Wall -lm hyp.c


据我所知,按照标准sqrt返回double,但是我们分配了

结果给长,导致信息丢失。然而我没有得到

警告,为什么会这样?


希望它是严格的C相关问题。
Hello,

given a simple code:

#include <math.h>
long hyp(long height, long base)
{
return sqrt(height * height + base * base);

}

int main(void)
{
long h = hyp(10, 20);
return 0;

}

I compile this on Debian 4.0 (gcc (GCC) 4.1.2 20061115 (prerelease) (Debian
4.1.1-21)):
#gcc -ansi -pedantic -W -Wall -lm hyp.c

As I understand, according to standard sqrt returns double, but we assign
result to long, resulting in information loss. Nevertheless I get no
warning, why is it so?

Hope it''s strictly C related question.



sqrt的返回值是implicity转换为long。

6.3.1.4 -

当a ????????????????????????????????????????????????????????????????????????????????????????????????朝着

零)。如果

的值不能用整数类型表示,那么

的行为是未定的。

The return value of sqrt is implicity converted to long.
6.3.1.4 --
When a ???nite value of real ???oating type is converted to an integer
type other than _Bool,
the fractional part is discarded (i.e., the value is truncated toward
zero). If the value of
the integral part cannot be represented by the integer type, the
behavior is unde???ned.


在文章< g2 *********** @ relay.tomsk.ru> ;,,Roman Mashak< mr*@tusur.ruwrote:
In article <g2***********@relay.tomsk.ru>, Roman Mashak <mr*@tusur.ruwrote:

>据我了解,根据标准sqrt返回double,但我们将结果分配给long,导致信息丢失。不过我没有得到警告,为什么会这样?
>As I understand, according to standard sqrt returns double, but we assign
result to long, resulting in information loss. Nevertheless I get no
warning, why is it so?



编译器无需向您发出有关信息丢失的警告。如果编译器在默认情况下这样做,我肯定会发现它很烦人。


如果你指定-Wconversion,我会期望gcc警告它,

但似乎没有,所以你可以把它报告为一个bug。


- Richard


-

在紧接着数字9的两个字符的选择中,

应考虑将图形10和11更换为

促进采用纯正货币区域的代码。 (X3.4-1963)

Compilers aren''t required to warn you about information loss. I would
certainly find it annoying if a compiler did that by default.

I would have expected gcc to warn about it if you specified -Wconversion,
but it doesn''t seem to, so you could report that as a bug.

-- Richard

--
In the selection of the two characters immediately succeeding the numeral 9,
consideration shall be given to their replacement by the graphics 10 and 11 to
facilitate the adoption of the code in the sterling monetary area. (X3.4-1963)


您好, vi ***** *@gmail.com

您在2008年6月5日星期四04:27:18 -0700(PDT)上写道:


v sqrt的返回值是implicity转换为long。

在我的理解中,隐式转换是类型的推广,即

而不会丢失位。例如,浮动到双倍


v6.3.1.4 -

v当真实浮动类型的有限值转换为整数时/>
除了_Bool之外的其他类型,

v小数部分被丢弃(即,该值被截断为

vzero)。如果

的值不能用整数类型表示,那么

vbehavior是未定义的。

这是摘录自C99标准?我用-ansi编译了这个片段,其中
符合C90的要求。


最诚挚的问候,Roman Mashak。电子邮件: mr*@tusur.ru
Hello, vi******@gmail.com!
You wrote on Thu, 5 Jun 2008 04:27:18 -0700 (PDT):

vThe return value of sqrt is implicity converted to long.
In my understanding implicit conversion is a promotion of types, i.e.
without loss of bits. For instance, float to double

v6.3.1.4 --
vWhen a ?nite value of real ?oating type is converted to an integer
vtype other than _Bool,
vthe fractional part is discarded (i.e., the value is truncated toward
vzero). If the value of
vthe integral part cannot be represented by the integer type, the
vbehavior is unde?ned.
Is this excerpt from C99 standard? I compiled the snippet with -ansi, which
is C90 conformant.

With best regards, Roman Mashak. E-mail: mr*@tusur.ru


这篇关于制作演员时发出警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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