首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

奇怪的5 关于常量的数据类型的有关问题

2012-07-08 
奇怪的5关于常量的数据类型的问题int main(){float aa1.1}当我编译这样一段程序,VC会提示:warning C430

奇怪的5 关于常量的数据类型的问题
int main()
{
float a;
a=1.1;
}

当我编译这样一段程序,VC会提示:warning C4305: '=' : truncation from 'const double ' to 'float '。

经过百度,原来“在C语言中,如果不指定数据类型,那么小数常量会被认为是 double 类型”。

a是float,而1.1是double。 double 不能隐式转换为 float ,所以才会产生这个warning。



可是!!当我将1.1改为1.5,再次编译,编译器却没有提示warning!(我又再次把1.5改为10.5,编译后仍然没提示warning)

我发现:凡是类似于X.5这种形式的常量都不会提示warning。


------------------------------
-----于是我产生疑惑:难道以5结尾的常量不是当做double类型吗?------
------------------------------

纠结了很久,希望知道的人能告诉下,谢谢!!

虽然这个warning并不影响程序,但是我想知道这个warning是怎么产生的。

[解决办法]
C Floating-Point Constants
A “floating-point constant” is a decimal number that represents a signed real number. The representation of a signed real number includes an integer portion, a fractional portion, and an exponent. Use floating-point constants to represent floating-point values that cannot be changed. 

Syntax

floating-point-constant :

fractional-constant exponent-part opt floating-suffix opt
digit-sequence exponent-part floating-suffix opt

fractional-constant :

digit-sequence opt . digit-sequence
digit-sequence .

exponent-part :

e sign opt digit-sequence
E sign opt digit-sequence

sign : one of

+ –

digit-sequence :

digit
digit-sequence digit

floating-suffix : one of

f l F L

You can omit either the digits before the decimal point (the integer portion of the value) or the digits after the decimal point (the fractional portion), but not both. You can leave out the decimal point only if you include an exponent. No white-space characters can separate the digits or characters of the constant. 

The following examples illustrate some forms of floating-point constants and expressions:

15.75
1.575E1 /* = 15.75 */
1575e-2 /* = 15.75 */
-2.5e-3 /* = -0.0025 */
25E-4 /* = 0.0025 */

Floating-point constants are positive unless they are preceded by a minus sign (–). In this case, the minus sign is treated as a unary arithmetic negation operator. Floating-point constants have type float, double, long, or long double.

A floating-point constant without an f, F, l, or L suffix has type double. If the letter f or F is the suffix, the constant has type float. If suffixed by the letter l or L, it has type long double. For example:

100L /* Has type long double */
100F /* Has type float */
100D /* Has type double */

Note that the Microsoft C compiler maps long double to type double. See Storage of Basic Types in Chapter 3 for information about type double, float, and long.

You can omit the integer portion of the floating-point constant, as shown in the following examples. The number .75 can be expressed in many ways, including the following:

.0075e2
0.075e1
.075e1
75e-2

不要迷信书、考题、老师、回帖;
要迷信CPU、编译器、调试器、运行结果。
并请结合“盲人摸太阳”和“驾船出海时一定只带一个指南针。”加以理解。
任何理论、权威、传说、真理、标准、解释、想象、知识……都比不上摆在眼前的事实!

[解决办法]
因为有误差的,double a 比 float a 误差要小一点,1.1化成二进制是无限循环小数。
[解决办法]
说明编译器还是很智能的,因为.5的可以直接由double转换到float不损失精度,所以不不报警了

热点排行