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

std::trunc

Defined in header <cmath>

?

?

float trunc( float arg );

(1)

(since C++11)

double trunc( double arg );

(2)

(since C++11)

long double trunc( long double arg );

(3)

(since C++11)

double trunc( Integral arg );

(4)

(since C++11)

1-3%29计算最接近的整数,其大小不大于arg...

4%29一组过载或接受任意参数的函数模板积分型等于2%29%28double29%。

参数

arg

-

floating point value

返回值

如果没有发生错误,则最近的整数值在大小上不大于arg%28,换言之,arg四舍五入至零%29,返回。

返回值

二次

二次

论辩

错误处理

错误按数学[医]错误处理...

如果实现支持ieee浮点算法%28IEC 60559%29,

  • 电流舍入方式没有效果。
  • 如果arg是±∞,它被返回,未经修改
  • 如果arg是±0,它被返回,未经修改
  • 如果阿格是南,南就会被归还。

注记

FE_INEXACT可能为%28,但在截断非整数有限值时,%27T是否需要提高%29。

最大的可表示浮点值是所有标准浮点格式的精确整数,因此此函数本身不会溢出,但是结果可能会溢出任何整数类型%28,包括std::intmax_t%29,存储在整数变量中。

大隐式转换从浮点类型到整型,也是圆圈到零,但仅限于目标类型可以表示的值。

二次

代码语言:javascript
复制
#include <cmath>
#include <iostream>
int main()
{
    std::cout << std::fixed
              << "trunc(+2.7) = " << std::trunc(+2.7) << '\n'
              << "trunc(-2.9) = " << std::trunc(-2.9) << '\n'
              << "trunc(-0.0) = " << std::trunc(-0.0) << '\n'
              << "trunc(-Inf) = " << std::trunc(-INFINITY) << '\n';
}

二次

可能的产出:

二次

代码语言:javascript
复制
trunc(+2.7) = 2.000000
trunc(-2.9) = -2.000000
trunc(-0.0) = -0.000000
trunc(-Inf) = -inf

二次

另见

floor

nearest integer not greater than the given value (function)

ceil

nearest integer not less than the given value (function)

roundlroundllround (C++11)(C++11)(C++11)

nearest integer, rounding away from zero in halfway cases (function)

C.trunc的文档

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

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

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com