这题没有这个要求`所以可以用 ? > : < 这种运算
以下代码用的是位级运算.因为我误解了题意 呜呜呜 想看用判断的代码请自行百度
((((ux<<9>>9)<<((ux<<1>>24)-127)) - ((uy<<9>>9)<<((uy<<1>>24)-127)))>>31);
原理是IEEE754,我解释一下这行代码:
(ux<<9>>9)是x的尾数Mx
((ux<<1>>24)-127))是x的e-Bias 也就是阶码Ex
Mx<<Ex就是M*
ux和uy这两个值的M* 相减 得到的差值再右移31位得到符号(得到result),也就是差值的正负
此时ux和uy如果是正数,就返回result,如果是负数,就返回!result
严格来说是符合题意的,因为result里面只用到了ux uy
#include <stdio.h>
int float_le(float x,float y);
unsigned f2u(float x) ;
int main()
{
printf("result = %d\n",float_le(1.1,2.2));
printf("result = %d\n",float_le(3.1,2.2));
printf("result = %d\n",float_le(-1.1,-1.2));
return 0;
}
int float_le(float x,float y)
{
unsigned ux = f2u(x) ;
unsigned uy = f2u(y) ;
unsigned sx = ux >> 31 ;
unsigned sy = uy >> 31 ;
printf("ux = %x\n",ux);
printf("expx = %x\n",(ux<<1>>24));
printf("expx-127 = %d\n",(ux<<1>>24)-127);
printf("Mx = %x\n",(ux<<9>>9));
printf("expx*mx = %x\n",((ux<<9>>9)<<((ux<<1>>24)-127)));
printf("uy = %x\n",uy);
printf("expx = %x\n",(uy<<1>>24));
printf("expy-127 = %d\n",(uy<<1>>24)-127);
printf("My = %x\n",(uy<<9>>9));
printf("expy*my = %x\n",((uy<<9>>9)<<((uy<<1>>24)-127)));
int result=((((ux<<9>>9)<<((ux<<1>>24)-127))-((uy<<9>>9)<<((uy<<1>>24)-127)))>>31);
return (ux==uy)||(sx==1&&sy==0)||(sx==0&&sy==0&&!result)||(sx==1&&sy==1&&!result);
}
unsigned f2u(float x)
{
return *(unsigned *)&x;
}