在C否定浮点数在某些情况下失败 [英] negate floating number in c fails in some cases

查看:120
本文介绍了在C否定浮点数在某些情况下失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了适用于几百个案例,但未能在某些情况下的功能。

I wrote a function which works for hundreds of cases but fails in some cases.

下面是C函数:

unsigned negate_number(unsigned x) {
  int sign = (!(x & 0x80000000))<<31;
  int other = 0x7FFFFFFF & x;
  return (sign | other);
}

我只是掩盖迹象,反转并与蒙面指数和mantessa做一个OR(加盟)。所以这应该适用于所有情况。

I am just masking sign, inverting it and doing a OR (joining) with masked exponent and mantessa. So this should work in all cases.

但这里是它失败的案例:
X = 0x7fc00000(2143289344)

But here is a case where it fails: x = 0x7fc00000 (2143289344)

推荐答案

我问:

为什么你要问关于标题浮点数时,code正在与整数?你们是不是要打电话与浮动功能,把它当作一个位数组,多还是少?如果是这样,你在一个躲在什么!如果您调用函数的范围原型,C编译器会在浮动转换为 unsigned int类型。如果你不与范围原型调用它,C编译器将转换成浮动双击前调用该函数。

Why are you asking about 'floating number' in the title when the code is working with integers? Are you trying to call the function with a float and treat it as an array of bits, more or less? If so, you're on a hiding to nothing! If you call the function with a prototype in scope, the C compiler will convert the float to an unsigned int. If you don't call it with a prototype in scope, the C compiler will convert the float to a double before calling the function.

和反应是:

这是一个32位IEEE 754单precision号。所以我只是翻转最显著位(符号位)。

It is a 32 bit IEEE 754 single precision number. So I am just flipping the most significant bit (sign bit).

要翻转32位(无符号整数)量的最显著位,你可以简单地写:

To flip the most significant bit of a 32-bit (unsigned integer) quantity, you could simply write:

x ^= 0x80000000;

不过,正如我指出的,你根本就没有获得通过一个32位的浮动除非你是骗你的编译器。你可以得到它的工作(在某些机器上,在某些时候),如果您有:

However, as I indicated, you are simply not getting passed a 32-bit float unless you are lying to your compiler. You could 'get it to work' (on some machines, some of the time) if you had:

extern float negate_number(float x);

...
float f1 = 3.14159;
float f2 = negate_number(f1);
...

fileB.c

unsigned negate_number(unsigned x)
{
    return x ^ 0x80000000;
}

不过,你是在玩火和说谎你的编译器。编译器讨厌被欺骗,而且经常找到一种方式来获得自己回来。 不这样做!

要实现或多或少的影响你想用最少的问题(而不是'没有问题'),你可能需要:

To achieve more or less the effect you want with a minimum of issues (but not 'no issues'), you probably need:

float negate_number(float f)
{
    union { unsigned x; float y; } u;
    u.y = f;
    u.x ^= 0x80000000;
    return u.y;
}

严格,读,分配给乌伊后写 UX 是不确定的行为,但它通常会做什么你要;同样与返回的 u.y 操作后 u.x

Strictly, reading and writing to u.x after assigning to u.y is undefined behaviour, but it will normally do what you want; similarly with returning u.y after manipulating u.x.

所有这些假设的位布局浮动无符号是这样的符号位的浮动是最显著位无符号

All of this assumes that the bit layout of float and unsigned are such that the sign bit of the float is the most significant bit of the unsigned.

这篇关于在C否定浮点数在某些情况下失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆