电脑爱好者,提供IT资讯信息及各类编程知识文章介绍,欢迎大家来本站学习电脑知识。 最近更新 | 联系我们 RSS订阅本站最新文章
电脑爱好者
站内搜索: 
当前位置:首页>> C++/VC>>Dev-C++下关于float/double类型的实验:

Dev-C++下关于float/double类型的实验

来源:www.cncfan.com | 2006-4-28 | (有7339人读过)

kingwei 2005.3.11

实验环境: Dev-C++ 4.9.6.0 (gcc/mingw32), 使用-Wall编译选项


#include <stdio.h>

int main()
{
float v_float;
double v_double;
long double v_long_double;

printf("sizeof(float) = %u\n", sizeof(float));
printf("sizeof(double) = %u\n", sizeof(double));
printf("sizeof(long double) = %u\n", sizeof(long double));

/* -3.40282e+038 ~ +3.40282e+038 */
scanf("%f", &v_float);
printf("%f\n", v_float);
printf("%e\n", v_float);

/* -1.79769e+308 ~ +1.79769e+308 */
scanf("%lf", &v_double);
printf("%f\n", v_double);
printf("%e\n", v_double);

/* -1.79769e+308 ~ +1.79769e+308 */
scanf("%Lf", &v_long_double);
printf("%Lf\n", v_long_double);
printf("%Le\n", v_long_double);

return 0;
}


1. float,double,long double长度分别为:

sizeof(float) = 4
sizeof(double) = 8
sizeof(long double) = 12


2. 数值范围测试

float +/- 3.40282e+038
double +/- 1.79769e+308
long double +/- 1.79769e+308


----- Test case #1 正向极值-----

3.40282e+038
1.79769e+308
1.79769e+308

output:

340282001837565600000000000000000000000.000000
3.402820e+038
179769000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000
1.797690e+308
179769000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000
1.797690e+308

----- Test case #2 负向极值-----

-3.40282e+038
-1.79769e+308
-1.79769e+308

output:

-340282001837565600000000000000000000000.000000
-3.402820e+038
-179769000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000
-1.797690e+308
-179769000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000
-1.797690e+308

----- Test case #3 正向溢出-----

3.40283e+038
1.79770e+308
1.79770e+308

output:

1.#INF00
1.#INF00e+000
1.#INF00
1.#INF00e+000
1.#INF00
1.#INF00e+000


----- Test case #4 负向溢出-----

-3.40283e+038
-1.79770e+308
-1.79770e+308

output:

-1.#INF00
-1.#INF00e+000
-1.#INF00
-1.#INF00e+000
-1.#INF00
-1.#INF00e+000

可见,虽然long double比double长4个字节,但是表示的数值范围却是一样的.
long double类型的长度,精度及表示范围与所使用的编译器,操作系统等有关.

VC++6.0下使用IEEE标准浮点数,long double为80位长度, 范围约为 +/- 1.2e+4932,
printf格式符为: %lf, %le, %lg. (未验证)


3. 浮点参数压栈的规则: float(4 字节)类型扩展成double(8 字节)入栈.

所以在输入时,需要区分float(%f)与double(%lf),而在输出时,用%f即可,
printf函数将按照double型的规则对压入堆栈的float(已扩展成double)和double型数据进行输出.
如果在输出时指定%lf格式符,gcc编译器将给出一个警告.


4. gcc编译器可以选择float的长度,是否与double一致.

C++/VC热门文章排行
网站赞助商
购买此位置

 

关于我们 | 网站地图 | 文档一览 | 友情链接| 联系我们

Copyright © 2003-2021 电脑爱好者 版权所有 备案号:鲁ICP备09059398号