float的存储空间 c语言,关于计算机中float近似存储的一个有关问题

关于计算机中float近似存储的一个问题?

#include "stdio.h"

int main(void)

{

float a;

float b;

a=0.565;

b=a*4;

printf("%f",b);

}

按理说0.565在计算机中应该是近似存储的调试也发现a是以0.564999998存储的,但是为什么本程序输出的时候结果却是2.26。不是应该是2.259999992吗

------解决方案--------------------

printf("%.20f",b);

------解决方案--------------------

引用:我的意思是中途的时候数据已经发生了变化,为什么到最后的时候数据还是正确的

google 浮点数寄存器

------解决方案--------------------

常量也有类型!

#include 

int main(void)

{

float a;

float b;

a=0.565f;

b=a*4.0f;

printf("%.6f",b);

return 0;

}

float.h

...

#define DBL_DIG         15                      /* # of decimal digits of precision */

#define DBL_EPSILON     2.2204460492503131e-016 /* smallest such that 1.0+DBL_EPSILON != 1.0 */

#define DBL_MANT_DIG    53                      /* # of bits in mantissa */

#define DBL_MAX         1.7976931348623158e+308 /* max value */

#define DBL_MAX_10_EXP  308                     /* max decimal exponent */

#define DBL_MAX_EXP     1024                    /* max binary exponent */

#define DBL_MIN         2.2250738585072014e-308 /* min positive value */

#define DBL_MIN_10_EXP  (-307)                  /* min decimal exponent */

#define DBL_MIN_EXP     (-1021)                 /* min binary exponent */

#define _DBL_RADIX      2                       /* exponent radix */

#define _DBL_ROUNDS     1                       /* addition rounding: near */

#define FLT_DIG         6                       /* # of decimal digits of precision */

#define FLT_EPSILON     1.192092896e-07F        /* smallest such that 1.0+FLT_EPSILON != 1.0 */

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值