如何计算32位浮点epsilon?

时间:2016-05-25 15:16:52

标签: c++ floating-point game-engine epsilon

在书籍游戏引擎架构:" ...中,假设我们使用浮点变量来跟踪绝对游戏时间(以秒为单位)。在我们的时钟变量幅度变得如此之大以至于加上1/30秒不再改变其值之前,我们可以运行多长时间?答案大概是12.9天。" 为什么12.9天,如何计算呢?

3 个答案:

答案 0 :(得分:7)

当浮点计算的结果无法准确表示时,它会四舍五入到最接近的值。因此,您希望找到最小值 x ,使得 f = 1/30的增量小于之间 h 的一半 h x 和下一个最大的浮点数,这意味着 x + f 将回滚到 x

由于同一binade中所有元素的间隙相同,我们知道 x 必须是其binade中的最小元素,这是2的幂。

因此,如果 x = 2 k ,那么 h = 2 k-23 ,因为{{3有一个24位有效数字。所以我们需要找到最小的整数 k ,以便

2 k-23 / 2> 1/30

暗示 k > 19.09,因此 k = 20, x = 2 20 = 1048576(秒)。

请注意, x /(60×60×24)= 12.14(天),这比您的答案提议的要少一点,但凭经验检查:在朱莉娅

julia> x = 2f0^20
1.048576f6

julia> f = 1f0/30f0
0.033333335f0

julia> x+f == x
true

julia> p = prevfloat(x)
1.04857594f6

julia> p+f == p
false

更新:好的,那么12.9来自哪里? 12.14是游戏时间,而不是实际时间:由于浮点中涉及的舍入误差(特别是在结束时,当舍入误差实际上相对于 f 非常大时),这些将会发散。据我所知,没有办法直接计算,但实际上迭代32位浮点数相当快。

再次,朱莉娅​​:

julia> function timestuff(f)
           t = 0
           x = 0f0
           while true
               t += 1
               xp = x
               x += f
               if x == xp
                   return (t,x)
               end
           end
       end
timestuff (generic function with 1 method)

julia> t,x = timestuff(1f0/30f0)
(24986956,1.048576f6)

x与我们之前计算的结果相匹配,t是30秒内的时钟时间。转换为日期:

julia> t/(30*60*60*24)
9.640029320987654

甚至更远。所以我不知道12.9来自哪里......

更新2:我的猜测是12.9来自计算

y = 4× f /ε= 1118481.125(秒)

其中ε是标准float(1和下一个最大浮点数之间的差距)。将此缩放到天数为12.945。这提供了 x 的上限,但如上所述,它不是正确的答案。

答案 1 :(得分:1)

这是由于浮点表示中的可表达区域。 查看我的单身this lecture

随着指数变大,实际表示的值之间的真实数字线上的跳跃增加;当指数低时,表示密度高。举一个例子,用有限数量的位值对十进制数进行成像。给定1.0001e1和1.0002e1,两个值之间的差值为0.0001。但如果指数增加1.0001-10 1.0002-10,则两者之间的差值为0.000100135。显然,随着指数的增加,这会变大。在你谈到的情况下,跳跃可能变得如此之大,增加不会促使最低位的舍入增加

有趣的是,对于表示的极限,较大浮点类型的准确性更差!仅仅因为当指数有更多的比特时,尾数中位模式的增加在数字线上跳得更远;就像double,over float

的情况一样

答案 2 :(得分:1)

#include <iostream>
#include <iomanip>

/*
https://en.wikipedia.org/wiki/Machine_epsilon#How_to_determine_machine_epsilon
*/

typedef union
{
    long  i32;
    float f32;
} flt_32;

float float_epsilon(float nbr)
{
    flt_32 flt;
    flt.f32 = nbr;
    flt.i32++;
    return (flt.f32 - nbr);
}

int main()
{
    // How to calculate 32-bit floating-point epsilon?

    const float one{ 1 }, ten_mills{ 10e6 };
    std::cout << "epsilon for number " << one << " is:\n"
        << std::fixed << std::setprecision(25)
        << float_epsilon(one)
        << std::defaultfloat << "\n\n";

    std::cout << "epsilon for number " << ten_mills << " is:\n"
        << std::fixed << std::setprecision(25)
        << float_epsilon(ten_mills)
        << std::defaultfloat << "\n\n";


    // In book Game Engine Architecture : "..., let’s say we use a
    // floating-point variable to track absolute game time in seconds.
    // How long can we run our game before the magnitude of our clock
    // variable gets so large that adding 1/30th of a second to it no
    // longer changes its value? The answer is roughly 12.9 days."
    // Why 12.9 days, how to calculate it ?

    const float one_30th{ 1.f / 30 }, day_sec{ 60 * 60 * 24 };
    float time_sec{}, time_sec_old{};

    while ((time_sec += one_30th) > time_sec_old)
    {
        time_sec_old = time_sec;
    }

    std::cout << "We can run our game for "
        << std::fixed << std::setprecision(5)
        << (time_sec / day_sec)
        << std::defaultfloat << " days.\n";


    return EXIT_SUCCESS;
}

此输出

epsilon for number 1 is:
0.0000001192092895507812500

epsilon for number 10000000 is:
1.0000000000000000000000000

We can run our game for 12.13630 days.