C ++中是否有trunc函数?

时间:2011-07-15 15:25:05

标签: c++ math

我四处搜索,找不到C ++的trunc函数。我知道我可以这样做:

int main()
{
    double a = 12.566789;
    cout << setprecision(2) << fixed << (int)(a * 100) / 100.0 << endl;
    return 0;
}

但我不确定这是最好的方法。谢谢。

7 个答案:

答案 0 :(得分:19)

如果您的C库太旧而缺少trunc函数(在C99中指定),您可以根据floorceil(在C89中指定)轻松实现一个/ p>

double trunc(double d){ return (d>0) ? floor(d) : ceil(d) ; }

答案 1 :(得分:4)

trunc

中有

<cmath>

#include <iostream>
#include <cmath>

int main() {
        std::cout << trunc(3.141516) << std::endl; 
}

我想你正在寻找别的东西?

答案 2 :(得分:2)

C中有一个可以在C ++中使用的截断函数

trunc(a*100)/100

请记住,您仍然需要指定格式化请求,因为浮点数不能完全代表所有实数,如果您不告诉,您可以获得12.560000000112.55999999之类的输出输出代码是你想要的精度。

TL; DR

使用以下输出:

cout << setprecision(2) << fixed << a<< endl;

以下是在数学计算过程中需要截断的结果:

trunc(a*100)/100

(或者更好的是,使用定点数学。)

答案 3 :(得分:0)

不确定。使用math.h中的trunc()函数。它是一个C函数,但它在C ++中也可以像在C中一样。如果你想保留几个数字,你可以随时:

double a = 12.566789;
double b = trunc(a * 100) / 100.0;

答案 4 :(得分:0)

如果您使用的是未实现trunc的古老C或C ++库,请使用boost::math::trunc

答案 5 :(得分:0)

我开发了一种非常快速的截断功能:

double ftrunc( double d )
{
    static_assert(sizeof(double) == sizeof(uint64_t), "sizeof(double) not equal to sizeof(uint64_t)");
    static_assert(numeric_limits<double>::is_iec559,  "double must be IEEE-754");
    // assume size_t is our CPU's native register-width
    static_assert(sizeof(size_t) == sizeof(uint64_t) || sizeof(size_t) == sizeof(uint32_t), "register-width must be 32 or 64 bit");
    if constexpr( sizeof(size_t) == sizeof(uint64_t) )
    // we have 64 bit registers
    {
        unsigned const MANTISSA_BITS           = 52,
                       EXP_BIAS                = 0x3FF,
                       INF_NAN_BASE            = 0x7FF;
        uint64_t const EXP_MASK                = (uint64_t)0x7FF                      << MANTISSA_BITS,
                       SIGN_MASK               = (uint64_t)0x800                      << MANTISSA_BITS ,
                       MIN_INTEGRAL_DIGITS_EXP = (uint64_t) EXP_BIAS                  << MANTISSA_BITS,
                       MIN_INTEGRAL_ONLY_EXP   = (uint64_t)(EXP_BIAS + MANTISSA_BITS) << MANTISSA_BITS,
                       INF_NAN_EXP             = (uint64_t)INF_NAN_BASE               << MANTISSA_BITS,
                       NEG_MANTISSA_MASK       = 0x000FFFFFFFFFFFFFu;
        union
        {
            double   du;
            uint64_t dx;
        };
        du = d;
        uint64_t exp = dx & EXP_MASK;
        if( exp >= MIN_INTEGRAL_DIGITS_EXP )
            // value has integral digits
            if( exp < MIN_INTEGRAL_ONLY_EXP )
            {
                // there are fraction-digits to mask out, mask them
                unsigned shift  = (unsigned)(exp >> MANTISSA_BITS) - EXP_BIAS;
                dx             &= ~(NEG_MANTISSA_MASK >> shift);
                return du;
            }
            else
                if( exp < INF_NAN_EXP )
                    // value is integral
                    return du;
                else
                    // infinite, NaN, SNaN
                    // raise exception on SNaN if necessary
                    return du + du;
        else
        {
            // below  +/-1.0
            // return +/-0.0
            dx &= SIGN_MASK;
            return du;
        }
    }
    else if constexpr( sizeof(size_t) == sizeof(uint32_t) )
    // we have 32 bit registers
    {
        unsigned const MANTISSA_BITS           = 52,
                       HI_MANTISSA_BITS        = 20,
                       EXP_BIAS                = 0x3FF,
                       INF_NAN_BASE            = 0x7FF;
        uint32_t const EXP_MASK                = (uint32_t)0x7FFu                        << HI_MANTISSA_BITS,
                       SIGN_MASK               = (uint32_t)0x800u                        << HI_MANTISSA_BITS,
                       MIN_INTEGRAL_DIGITS_EXP = (uint32_t) EXP_BIAS                     << HI_MANTISSA_BITS,
                       MAX_INTEGRAL32_EXP      = (uint32_t)(EXP_BIAS + HI_MANTISSA_BITS) << HI_MANTISSA_BITS,
                       MIN_INTEGRAL_ONLY_EXP   = (uint32_t)(EXP_BIAS + MANTISSA_BITS)    << HI_MANTISSA_BITS,
                       INF_NAN_EXP             = (uint32_t)INF_NAN_BASE                  << HI_MANTISSA_BITS,
                       NEG_HI_MANTISSA_MASK    = 0x000FFFFFu,
                       NEG_LO_MANTISSA_MASK    = 0xFFFFFFFFu;
        union
        {
            double du;
            struct
            {
                uint32_t dxLo;
                uint32_t dxHi;
            };
        };
        du = d;
        uint32_t exp = dxHi & EXP_MASK;
        if( exp >= MIN_INTEGRAL_DIGITS_EXP )
            // value has integral digits
            if( exp < MIN_INTEGRAL_ONLY_EXP )
                // there are fraction-digits to mask out
                if( exp <= MAX_INTEGRAL32_EXP )
                {
                    // the fraction digits are in the upper dword, mask them and zero the lower dword
                    unsigned shift  = (unsigned)(exp >> HI_MANTISSA_BITS) - EXP_BIAS;
                    dxHi           &= ~(NEG_HI_MANTISSA_MASK >> shift);
                    dxLo            = 0;
                    return du;
                }
                else
                {
                    // the fraction digits are in the lower dword, mask them
                    unsigned shift  = (unsigned)(exp >> HI_MANTISSA_BITS) - EXP_BIAS - HI_MANTISSA_BITS;
                    dxLo           &= ~(NEG_LO_MANTISSA_MASK >> shift);
                    return du;
                }
            else
                if( exp < INF_NAN_EXP )
                    // value is integral
                    return du;
                else
                    // infinite, NaN, SNaN
                    // raise exception on SNaN if necessary
                    return du + du;
        else
        {
            // below  +/-1.0
            // return +/-0.0
            dxHi &= SIGN_MASK;
            dxLo  = 0;
            return du;
        }
    }
}

它比大多数实现要快。在我的Ryzen 7 1800X上,值> = 2 ^ 0和<= 2 ^ 54的平均执行时间为12个时钟周期。

答案 6 :(得分:-2)

使用来自cmath的ceilfloor