从符号长长转换为浮动圆形至最近的偶数 [英] Converting from unsigned long long to float with round to nearest even

查看:209
本文介绍了从符号长长转换为浮动圆形至最近的偶数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要写轮从符号长长飘起了一个功能,四舍五入应该是朝最近的偶数。
我不能只是做一个C ++的类型转换,因为据我所知的标准没有规定的四舍五入。
我想使用boost ::数字,但阅读文档后,我找不到任何有用的领先优势。可以这样使用该库做什么?
当然,如果有选择,我会很乐意使用它。

I need to write a function that rounds from unsigned long long to float, and the rounding should be toward nearest even. I cannot just do a C++ type-cast, since AFAIK the standard does not specify the rounding. I was thinking of using boost::numeric, but i could not find any useful lead after reading the documentation. Can this be done using that library? Of course, if there is an alternative, i would be glad to use it.

任何帮助将是非常美联社preciated。

Any help would be much appreciated.

编辑:添加一个例子来使事情更清楚一点。
假设我想0xffffff7fffffffff转换为它的浮点再presentation。 C ++标准允许两种之一:

Adding an example to make things a bit clearer. Suppose i want to convert 0xffffff7fffffffff to its floating point representation. The C++ standard permits either one of:


  1. 0x5f7fffff〜1.9999999 * 2 ^ 63

  2. 0x5f800000 = 2 ^ 64

现在如果你添加一轮的限制最近偶数,只有第一个结果是可以接受的。

Now if you add the restriction of round to nearest even, only the first result is acceptable.

推荐答案

既然你已经在源有太多的比特不能重新在浮动psented $ P $ ,你不能(显然)依赖于语言的转换,你必须自己做。

Since you have so many bits in the source that can't be represented in the float and you can't (apparently) rely on the language's conversion, you'll have to do it yourself.

我设计了一个方案,可能会或可能不会帮助你。基本上,有31位在浮动重新present正数所以我拿起源数31最显著位。然后,我保存了和掩盖了所有的低位。然后根据下位的值我圆了新的LSB向上或向下,最后用的static_cast 来创建一个浮动

I devised a scheme that may or may not help you. Basically, there are 31 bits to represent positive numbers in a float so I pick up the 31 most significant bits in the source number. Then I save off and mask away all the lower bits. Then based on the value of the lower bits I round the "new" LSB up or down and finally use static_cast to create a float.

我留在一些COUTS根据需要,你可以删除。

I left in some couts that you can remove as desired.

const unsigned long long mask_bit_count = 31;

float ull_to_float2(unsigned long long val)
{
    // How many bits are needed?
    int b = sizeof(unsigned long long) * CHAR_BIT - 1;
    for(; b >= 0; --b)
    {
        if(val & (1ull << b))
        {
            break;
        }
    }

    std::cout << "Need " << (b + 1) << " bits." << std::endl;

    // If there are few enough significant bits, use normal cast and done.
    if(b < mask_bit_count)
    {
        return static_cast<float>(val & ~1ull);
    }

    // Save off the low-order useless bits:
    unsigned long long low_bits = val & ((1ull << (b - mask_bit_count)) - 1);
    std::cout << "Saved low bits=" << low_bits << std::endl;

    std::cout << val << "->mask->";
    // Now mask away those useless low bits:
    val &= ~((1ull << (b - mask_bit_count)) - 1);
    std::cout << val << std::endl;

    // Finally, decide how to round the new LSB:
    if(low_bits > ((1ull << (b - mask_bit_count)) / 2ull))
    {
        std::cout << "Rounding up " << val;
        // Round up.
        val |= (1ull << (b - mask_bit_count));
        std::cout << " to " << val << std::endl;
    }
    else
    {
        // Round down.
        val &= ~(1ull << (b - mask_bit_count));
    }

    return static_cast<float>(val);
}

这篇关于从符号长长转换为浮动圆形至最近的偶数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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