Tag: 数值稳定性

将float转换为UInt32 – 哪个表达式更精确

我有一个float x ,它应该在范围内,但它经过几次数值运算 – 结果可能略微超出。 我需要使用整个UInt32范围将此结果转换为uint y 。 当然,我需要在范围内钳制x并进行缩放。 但是哪种操作顺序更好? y = (uint)round(min(max(x, 0.0F), 1.0F) * UInt32.MaxValue) 要么 y = (uint)round(min(max(x * UInt32.MaxValue, 0.0F), UInt32.MaxValue) 换句话说,最好先缩放,然后钳制OR钳位然后缩放? 我在IEEE浮点表示中不是很深刻,但我相信上述表达式的计算顺序有所不同。

在javascript中测试“双重”平等

我已经将Clipper库的实验性C#“float”版本翻译成了 javascript。 在最新的沙盒版本中,有一个函数IsAlmostEqual似乎很难翻译。 由于数值稳定性问题,无法使用==运算符比较双重等式,因此需要此函数来处理这些问题。 -9223372036854775808 – aInt和-9223372036854775808 – bInt很容易用BigInteger库计算,但BitConverter.DoubleToInt64Bits更难。 知道如何将IsAlmostEqual函数转换为javascript吗? 或者具体如何实现BitConverter.DoubleToInt64Bits到javascript? private static bool IsAlmostEqual(double A, double B) { //http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm Int64 aInt = BitConverter.DoubleToInt64Bits(A); if (aInt < 0) aInt = unchecked(-9223372036854775808 – aInt); Int64 bInt = BitConverter.DoubleToInt64Bits(B); if (bInt < 0) bInt = unchecked(-9223372036854775808 – bInt); return (Math.Abs(aInt – bInt) <= 10000000000); } 数值稳定性和鲁棒性: http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm […]