计算最后一个单位(ULP)双打 [英] Calculate the unit in the last place (ULP) for doubles

查看:91
本文介绍了计算最后一个单位(ULP)双打的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.NET是否具有内置方法来计算给定双倍的 ULP 或float?

Does .NET have a built-in method to calculate the ULP of a given double or float?

如果没有,最有效的方法是什么?

If not, what is the most efficient way to do so?

推荐答案

似乎函数很简单;这是基于由vulkanino链接的问题的接受答案中的伪代码:

It seems the function is pretty trivial; this is based on the pseudocode in the accepted answer to the question linked by vulkanino:

double value = whatever;
long bits = BitConverter.DoubleToInt64Bits(value);
double nextValue = BitConverter.Int64BitsToDouble(bits + 1);
double result = nextValue - value;

对于浮点数,您需要提供自己的 SingleToInt32Bits Int32BitsToSingle ,因为BitConverter没有这些功能。

For floats, you'd need to provide your own implementation of SingleToInt32Bits and Int32BitsToSingle, since BitConverter doesn't have those functions.

此页面显示了特殊的案例在java实现的功能;处理这些应该也是相当微不足道的。

This page shows the special cases in the java implementation of the function; handling those should be fairly trivial, too.

这篇关于计算最后一个单位(ULP)双打的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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