首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

FE_UPWARD

Defined in header <cfenv>

?

?

#define FE_DOWNWARD /*implementation defined*/

?

(since C++11)

#define FE_TONEAREST /*implementation defined*/

?

(since C++11)

#define FE_TOWARDZERO /*implementation defined*/

?

(since C++11)

#define FE_UPWARD /*implementation defined*/

?

(since C++11)

这些宏常量中的每一个扩展为一个非负整数常量表达式,该表达式可与std::fesetroundstd::fegetround若要指示支持的浮点舍入模式之一,请执行以下操作。实现可以在<cfenv>,这一切都应该从FE_后面至少有一个大写字母。每个宏只有在支持时才定义。

在大多数实现中,这些宏常量展开为等于FLT_ROUNDSstd::float_round_style...

Constant

Explanation

FE_DOWNWARD

rounding towards negative infinity

FE_TONEAREST

rounding towards nearest representable value

FE_TOWARDZERO

rounding towards zero

FE_UPWARD

rounding towards positive infinity

实现可以支持其他舍入模式。

当前的舍入模式会影响以下内容:

  • 浮点结果算术算子常量表达式外二次double x = 1; x/10; // 0.09999999999999999167332731531132594682276248931884765625 // or 0.1000000000000000055511151231257827021181583404541015625二次
  • 标准库结果数学函数

二次

代码语言:javascript
复制
std::sqrt(2); // 1.41421356237309492343001693370752036571502685546875
           // or 1.4142135623730951454746218587388284504413604736328125

二次

  • 浮点到浮点的隐式转换和转换二次double d = 1 + std::numeric_limits<double>::epsilon(); float f = d; // 1.00000000000000000000000 // or 1.00000011920928955078125二次
  • 字符串转换,例如std::strtodstd::printf

二次

代码语言:javascript
复制
std::stof("0.1"); // 0.0999999940395355224609375
               // or 0.100000001490116119384765625

二次

  • 图书馆舍入功能std::nearbyint,,,std::rint,,,std::lrint二次std::lrint(2.1); // 2 or 3二次当前舍入模式不影响以下内容:
  • 浮点到整数隐式转换,并将%28始终转换为零%29。
  • 常数表达式中浮点算术运算符%28总是最接近%29的结果
  • 图书馆功能std::round,,,std::lround,,,std::ceil,,,std::floor,,,std::trunc

就像任何浮点环境功能,只有在以下情况下才能保证四舍五入#pragma STDC FENV_ACCESS ON已经设定好了。

二次

代码语言:javascript
复制
#include <iostream>
#include <iomanip>
#include <string>
#include <cfenv>
#include <cmath>
int main()
{
#pragma STDC FENV_ACCESS ON
    std::fesetround(FE_DOWNWARD);
    std::cout << "rounding down: \n" << std::setprecision(50)
              << "         pi = " << std::acos(-1.f) << '\n'
              << "stof(\"1.1\") = " << std::stof("1.1") << '\n'
              << "  rint(2.1) = " << std::rint(2.3) << "\n\n";
    std::fesetround(FE_UPWARD);
    std::cout << "rounding up: \n"
              << "         pi = " << std::acos(-1.f) << '\n'
              << "stof(\"1.1\") = " << std::stof("1.1") << '\n'
              << "  rint(2.1) = " << std::rint(2.3) << '\n';
}

二次

产出:

二次

代码语言:javascript
复制
rounding down: 
         pi = 3.141592502593994140625
stof("1.1") = 1.099999904632568359375
  rint(2.1) = 2
 
rounding up: 
         pi = 3.1415927410125732421875
stof("1.1") = 1.10000002384185791015625
  rint(2.1) = 3

二次

另见

float_round_style

indicates floating-point rounding modes (enum)

fegetroundfesetround (C++11)(C++11)

gets or sets rounding direction (function)

c浮点四舍五入宏的文档

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com