简体   繁体   English

在浮点环境中更改舍入模式的实际用途是什么?

[英]What is the practical use of changing the rounding mode in floating point evnironment?

I'm trying to find a good use for changing rounding mode in IEEE-754 floating-point environment.我试图找到在 IEEE-754 浮点环境中更改舍入模式的好方法。

I'm looking mostly from perspective of C and C++ code.我主要从 C 和 C++ 代码的角度来看。 There, it can be done using在那里,可以使用

int fesetround(int round)

function from "fenv.x" (or <cfenv> in C++). function 来自"fenv.x" (或 C++ 中的<cfenv> )。

The best use I could find is setting the environment to FE_TONEAREST , if for any godforsaken reason it wasn't the default.我能找到的最佳用途是将环境设置为FE_TONEAREST ,如果出于任何不可原谅的原因,它不是默认设置。

In this question someone suggested using it to get some implementation-dependent behavior of string formatting. 在这个问题中,有人建议使用它来获得一些与实现相关的字符串格式化行为。 I personally find this answer completely wrong, and I believe changing rounding modes around formatting functions can only result in unexpected, or simply wrong behavior.我个人认为这个答案是完全错误的,而且我相信围绕格式化函数改变舍入模式只会导致意外或错误的行为。

Another thing that is changed by rounding mode, are the functions: nearbyint and rint .舍入模式改变的另一件事是函数: nearbyintrint But why would you ever want to use them, instead of dispatching to floor , ceil and trunc functions?但是你为什么要使用它们,而不是调度到floorceiltrunc函数呢?

That leaves us with the only useful part being local difference in floating point arithmetic.这给我们留下了唯一有用的部分是浮点运算的局部差异。 Where could that be useful?这在哪里有用? I was trying to find some niche use for that, but so far I couldn't find any.我试图为此找到一些利基用途,但到目前为止我找不到任何用途。

The rouding modes have little to do with the floor and ceiling (the latter round to integers ).路由模式与地板和天花板关系不大(后者舍入为整数)。 I don't know about a trunk function.我不知道trunk function。

Changing the rounding rule might be useful in some sophisticated cases where theoretical study shows that a particular rule has advantages over other or gives some desirable guarantee.在理论研究表明特定规则比其他规则具有优势或提供某些理想保证的某些复杂情况下,更改舍入规则可能很有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM