not_at_all 发表于 2012-5-21 21:00:16

浮点数怎样实现逻辑运算

如题,浮点转字符串,为了避免浮点乘除运算,想自己通过移位等实现浮点转字符串,但是发现浮点不能实现逻辑运算,否则编译报错

3050311118 发表于 2012-5-21 21:07:36

楼主再讲具体点嘛

NJ8888 发表于 2012-5-21 21:44:51

你是定点单片机,开始时就应当用定点数,不知道你是哪种控制器

lswood 发表于 2012-5-21 22:18:19

楼主要是想避免浮点乘法和除法,就可以先去了解下IEEE规定的浮点存储规则,然后去实现ftoa()函数。

not_at_all 发表于 2012-5-21 23:35:52

lswood 发表于 2012-5-21 22:18 static/image/common/back.gif
楼主要是想避免浮点乘法和除法,就可以先去了解下IEEE规定的浮点存储规则,然后去实现ftoa()函数。 ...

浮点存储规则我已经知道, 但是现在问题关键是我想取某几bit,想用位与或运算,编译器报错,说一定要整型的数。现在只能用指针算出绝对地址提取内存数值,人为把它变成整型数了。

aureole 发表于 2012-5-22 00:12:18

union 也不行吗?

iamseer 发表于 2012-5-22 00:15:38

摘自Quake III
float Q_rsqrt( float number )
{
      long i;
      float x2, y;
      const float threehalfs = 1.5F;

      x2 = number * 0.5F;
      y= number;
      i= * ( long * ) &y;                     // evil floating point bit level hacking
      i= 0x5f3759df - ( i >> 1 );               // what the fuck?
      y= * ( float * ) &i;
      y= y * ( threehalfs - ( x2 * y * y ) );   // 1st iteration
//      y= y * ( threehalfs - ( x2 * y * y ) );   // 2nd iteration, this can be removed

      return y;
}
i 就是被转换的变量

iamseer 发表于 2012-5-22 00:20:38

i= * ( long * ) &y;    这句是不需要时间的,编译器知道你什么意思.

not_at_all 发表于 2012-5-22 18:26:11

iamseer 发表于 2012-5-22 00:15 static/image/common/back.gif
摘自Quake III
float Q_rsqrt( float number )
{


谢谢!这是我需要的答案!

not_at_all 发表于 2012-5-22 18:30:47

i= * ( long * ) &y;    这句是不需要时间的,编译器知道你什么意思.

用指针转换是最有效的方法。不像普通强制转换那样截断数据。
页: [1]
查看完整版本: 浮点数怎样实现逻辑运算