库在.NET严格的浮点运算 [英] Library for strict floating point Math in .NET

查看:109
本文介绍了库在.NET严格的浮点运算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有算法/计算在Java和单元测试它。单元测试预期结果与一些precision /三角洲。现在我移植的算法中到.NET和想用相同的单元测试。我的工作与双数据类型。

I have algorithm/computation in Java and unit test for it. The unit test expects result with some precision/delta. Now I ported the algo into .NET and would like to use same unit test. I work with double data type.

的问题是,Java使用strictfp(64位),用于在数学类的一些操作。凡为.NET使用FPU / CPU总是(80位)。 .NET是更precise更快。 Java是更predictable。

The problem is that Java uses strictfp (64bits) for some operations in Math class. Where as .NET uses FPU/CPU always (80 bits). .NET is more precise and faster. Java is more predictable.

由于我的算法中是循环和重新使用previous轮,错误/差分/更多 - precision结果累积过大。我不靠速度(单元测试)。我很高兴使用.NET precision在生产中,但我想验证的实施。

Because my algo is cyclic and reuses the results from previous round, the error/difference/more-precision accumulates too big. I don't rely on speed (for unit test). And I'm happy to use .NET precision in production, but I would like to validate the implementation.

从JDK考虑这个


public final class Math {
    public static double atan2(double y, double x) {
    return StrictMath.atan2(y, x); // default impl. delegates to StrictMath
    }
}

我在找库或技术,使用严格的计划生育在.NET

preemptive评论:我明白IEEE 754格式的事实,浮点数字并不确切小数或分数。没有小数,不BigInt有或BigNumber。的请不要回答这种方式,谢谢。

Preemptive comment: I do understand IEEE 754 format and the fact that floating point number is not exact decimal number or fraction. No Decimal, no BigInt or BigNumber. Please don't answer this way, thanks.

推荐答案

不幸的是,没有办法执行FP严格在C#中,.NET CLR只是缺少做计算用更少的precision的能力,最大的是可以的。

Unfortunately there is no way to enforce FP strictness in C#, the .Net CLR just lacks the ability to do calculations with less precision that the maximum that is can.

我觉得有一个性能增益这一点 - 它不检查,你可能要少precision。但也没有必要 - .NET没有在虚拟机中运行,因此不会担心不同的浮点处理器

I think there's a performance gain for this - it doesn't check that you might want less precision. There's also no need - .Net doesn't run in a virtual machine and so doesn't worry about different floating point processors.

不过不是 strictfp 可选?你可以不带 strictfp 修改执行你的Java code?然后,它应该拿起相同的浮点机制,净

However isn't strictfp optional? Could you execute your Java code without the strictfp modifier? Then it should pick up the same floating point mechanism as .Net

您可以强制Java不使用 strictfp 并检查它再出来一样的.Net code。

So instead of forcing .Net to use strictfp and checking it comes out with the same values as your Java code you could force Java to not use strictfp and check that it then comes out the same as the .Net code.

这篇关于库在.NET严格的浮点运算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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