std::trunc,...std::truncl(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… std::trunc, std::truncf, std::truncl

okay so bestie, welcome to the <cmath> corner of the C++ Standard Library β€” this is std::trunc, std::truncf, std::truncl(3) and honestly? she's doing one job and doing it flawlessly. ✨

Think "chop off everything after the decimal point, no matter what side of zero you're on." No cap. Let's get into it. 🫢


🌸 SYNOPSIS 🌸

Defined in header <cmath>:

float       trunc ( float num );

double      trunc ( double num );                            (until C++23)

long double trunc ( long double num );
constexpr /* floating-point-type */                          (since C++23)
                trunc ( /* floating-point-type */ num );
float       truncf( float num );                     (1) (2) (since C++11)
                                                                     (constexpr since C++23)
long double truncl( long double num );                   (3) (since C++11)
                                                                     (constexpr since C++23)

Additional overloads (since C++11)

Defined in header <cmath>:

template< class Integer >                                (A) (constexpr since C++23)
double      trunc ( Integer num );
  • 1-3) Computes the nearest integer not greater in magnitude than num. 🎯
  • The library provides overloads of std::trunc for all cv-unqualified floating-point types as the type of the parameter. (since C++23)
  • A) Additional overloads are provided for all integer types, which are treated as double. (since C++11)

πŸŽ€ PARAMETERS πŸŽ€

  • num β€” floating-point or integer value. that's it. that's the whole vibe check. πŸ’–

⭐ RETURN VALUE ⭐

If no errors occur, the nearest integer value not greater in magnitude than num is returned β€” in other words, num rounded towards zero. 🧚✨

Return value

math-trunc.svg

num

(yes bestie, the docs literally drop a little svg diagram and then just... say num again. iconic behavior honestly. πŸ‘‘)


πŸ’€ ERROR HANDLING πŸ’€

Errors are reported as specified in math_errhandling.

If the implementation supports IEEE floating-point arithmetic (IEC 60559):

  • The current rounding mode has no effect. (she does not care about your rounding mode, periodt.)
  • If num is ±∞, it is returned, unmodified.
  • If num is Β±0, it is returned, unmodified.
  • If num is NaN, NaN is returned.

πŸ¦‹ NOTES πŸ¦‹

FE_INEXACT may be (but isn't required to be) raised when truncating a non-integer finite value. lowkey a maybe, highkey not a promise. πŸ™ˆ

The largest representable floating-point values are exact integers in all standard floating-point formats, so this function never overflows on its own β€” however the result may overflow any integer type (including std::intmax_t) when stored in an integer variable. so like, the function is fine, your variable might not be. 😭

The implicit conversion from floating-point to integral types also rounds towards zero, but is limited to the values that can be represented by the target type.

The additional overloads are not required to be provided exactly as (A). They only need to be sufficient to ensure that for their argument num of integer type, std::trunc(num) has the same effect as std::trunc(static_cast<double>(num)). ✨


🌈 EXAMPLE 🌈

// Run this code

#include <cmath>
#include <initializer_list>
#include <iostream>

int main()
{
    const auto data = std::initializer_list<double>
    {
        +2.7, -2.9, +0.7, -0.9, +0.0, 0.0, -INFINITY, +INFINITY, -NAN, +NAN
    };

    std::cout << std::showpos;
    for (double const x : data)
        std::cout << "trunc(" << x << ") == " << std::trunc(x) << '\n';
}

Possible output

trunc(+2.7) == +2
trunc(-2.9) == -2
trunc(+0.7) == +0
trunc(-0.9) == -0
trunc(+0) == +0
trunc(+0) == +0
trunc(-inf) == -inf
trunc(+inf) == +inf
trunc(-nan) == -nan
trunc(+nan) == +nan

πŸ‘‘ SEE ALSO πŸ‘‘

  • floor, floorf, floorl β€” nearest integer not greater than the given value (function) (C++11)
  • ceil, ceilf, ceill β€” nearest integer not less than the given value (function) (C++11)
  • round, roundf, roundl, lround, lroundf, lroundl, llround, llroundf, llroundl β€” nearest integer, rounding away from zero in halfway cases (function) (C++11)
  • C documentation for trunc

πŸ’– bestie reminder: trunc just throws away the fractional part β€” she does not round to nearest, she does not care about halfway cases, she simply says "nope" to your decimals and moves on. queen behavior. πŸ’…

✨ that's the whole page! brought to you by http://cppreference.com β€” version dated 2024.06.10, manual std::trunc,...std::truncl(3). go forth and truncate, bestie πŸŽ€πŸŒΈπŸ’œ

πŸ“¦ where this page comes from