浮点错误是确定性的吗? [英] Are floating point errors deterministic?

查看:78
本文介绍了浮点错误是确定性的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

浮点数最大的缺点之一是它们中的某些不能精确地用二进制表示.这会使它们难以使用.但是我很好奇的是浮点数中的细微或非细微错误是否是确定性的.有人可以预测它们吗?这是一个可以利用浮点错误的随机数生成器的示例:

One of the big got'chas of floating point numbers is that some of them cannot be exactly represented in binary. This can make them difficult to work with. However what I'm curious about is whether or not subtle or not-so-subtle errors in floating point are deterministic. Can somebody predict them for example? Here's one example of a random number generator that could take advantage of floating point errors:

#include <cmath>

float constant = M_PI;
float generate()
{
    static float state = 1;
    state = state * constant;
    return state;
}

必须知道实现,硬件,编译器设置等,这使得很难预测结果是什么.还是我的思维有缺陷?

One would have to know the implementation, the hardware, the compiler settings and so on, which makes it quite difficult to predict what the results would be. Or is my thinking flawed?

推荐答案

浮点错误"是确定性的.给定操作的输入值和输出值之间存在1:1的映射.您的示例每次都会产生相同的输出序列.

Floating point "errors" are deterministic. There is a 1:1 mapping between input and output values for a given operation. Your example will produce the same output sequence every time.

也就是说,可能会有一个浮点实现或十个实现会产生不同的序列,但这不是您可以考虑的随机"(即熵的来源).

That said, there could be a floating-point implementation or ten out there that will produce different sequences, but this is not something you can consider "random" (i.e. a source of entropy).

这篇关于浮点错误是确定性的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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