std::tan,st...f,std::tanl(3)

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

🌸 std::tan, std::tanf, std::tanl πŸ’…

okay bestie, so this one lives in the C++ Standard Library and it's the trig girlie you already know β€” she takes an angle and gives you back the tangent. Let's get into it ✨


✨ NAME ✨

std::tan, std::tanf, std::tanl β€” literally just std::tan, std::tanf, and std::tanl. that's the whole name, no notes πŸŽ€


πŸ¦‹ Synopsis πŸ¦‹

okay so the way this is laid out β€” everything's Defined in header <cmath>, and there are a few flavours of her. deep breath, we're getting through all of them πŸ’–

Defined in header <cmath>

float       tan ( float num );

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

long double tan ( long double num );
/* floating-point-type */                                  (since C++23)
                tan ( /* floating-point-type */ num );     (constexpr since C++26)
float       tanf( float num );                     (1) (2) (since C++11)
                                                                   (constexpr since C++26)
long double tanl( long double num );                   (3) (since C++11)
                                                                   (constexpr since C++26)

Additional overloads (since C++11)
Defined in header <cmath>

template< class Integer >                              (A) (constexpr since C++26)
double      tan ( Integer num );
  • 1-3) Computes the tangent of num (measured in radians, not degrees, we've been over this 😭).
  • The library provides overloads of std::tan 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 representing the angle in radians πŸŒ€

πŸ’– Return value πŸ’–

so if nothing goes wrong, you get the tangent of num, i.e. tan(num). iconic, no drama. ✨

(until C++11) ngl the result may have little or no significance if the magnitude of num is large. she's doing her best but the math is not mathing at that scale πŸ™ˆ
  • If a domain error occurs, an implementation-defined value is returned (NaN where supported).
  • If a range error occurs due to underflow, the correct result (after rounding) is returned.

πŸ”₯ Error handling πŸ”₯

Errors are reported as specified in math_errhandling. periodt.

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

  • if the argument is Β±0, it is returned unmodified πŸ’…
  • if the argument is ±∞, NaN is returned and FE_INVALID is raised πŸ’€
  • if the argument is NaN, NaN is returned 🫢

🧚 Notes 🧚

okay the lore here is genuinely wild, sit down πŸ“

The case where the argument is infinite is not specified to be a domain error in C (to which C++ defers), but it is defined as a domain error in POSIX. so like, depends who you ask, iykyk.

The function has mathematical poles at Ο€(1/2 + n) β€” but no common floating-point representation is able to represent Ο€/2 exactly, so there is no value of the argument for which a pole error occurs. like she simply cannot be caught slipping πŸ˜­πŸ‘‘

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::tan(num) has the same effect as std::tan(static_cast<double>(num)). just vibe-equivalent, that's the requirement πŸ’œ


🌈 Example 🌈

// Run this code

#include <cerrno>
#include <cfenv>
#include <cmath>
#include <iostream>

// #pragma STDC FENV_ACCESS ON
const double pi = std::acos(-1); // or C++20's std::numbers::pi

int main()
{
    // typical usage
    std::cout << "tan(1*pi/4) = " << std::tan(1*pi/4) << '\n' // 45Β°
              << "tan(3*pi/4) = " << std::tan(3*pi/4) << '\n' // 135Β°
              << "tan(5*pi/4) = " << std::tan(5*pi/4) << '\n' // -135Β°
              << "tan(7*pi/4) = " << std::tan(7*pi/4) << '\n'; // -45Β°

    // special values
    std::cout << "tan(+0) = " << std::tan(0.0) << '\n'
              << "tan(-0) = " << std::tan(-0.0) << '\n';

    // error handling
    std::feclearexcept(FE_ALL_EXCEPT);

    std::cout << "tan(INFINITY) = " << std::tan(INFINITY) << '\n';
    if (std::fetestexcept(FE_INVALID))
        std::cout << "    FE_INVALID raised\n";
}

And the output she gives you:

tan(1*pi/4) = 1
tan(3*pi/4) = -1
tan(5*pi/4) = 1
tan(7*pi/4) = -1
tan(+0) = 0
tan(-0) = -0
tan(INFINITY) = -nan
    FE_INVALID raised

like look at -0 and -nan β€” she's serving detail πŸ’€βœ¨


πŸ‘‘ See also πŸ‘‘

the whole friend group, bestie, we're not leaving anyone out πŸ’–

  • sin, sinf, sinl β€” computes sine (sin(x)) (function) (C++11) (C++11)
  • cos, cosf, cosl β€” computes cosine (cos(x)) (function) (C++11) (C++11)
  • atan, atanf, atanl β€” computes arc tangent (arctan(x)) (function) (C++11) (C++11)
  • tan(std::complex) β€” computes tangent of a complex number (tan(z)) (function template)
  • tan(std::valarray) β€” applies the function std::tan to each element of valarray (function template)
  • C documentation for tan β€” the og, the blueprint 🫑

and that's the whole page, bestie β€” no cap, every flag and footnote accounted for πŸ’…

🌸 source/version + date, respectfully: http://cppreference.com, 2024.06.10, and this whole thing is filed under std::tan,st...f,std::tanl(3). mwah 🫢✨

πŸ“¦ where this page comes from