std::erf,st...f,std::erfl(3)

๐Ÿ“ฆ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

๐Ÿ’– NAME ๐Ÿ’–

okay so bestie, this is the std::erf, std::erff, std::erfl page from the C++ Standard Library manual (std::erf,st...f,std::erfl(3) โ€” yes the title got a lil truncated, we love that for us). These three are the error function crew. that's the whole vibe. iykyk.


โœจ SYNOPSIS โœจ

so first off, everything lives in this header:

#include <cmath>

and here's the whole cast of characters ๐Ÿ’…

float       erf ( float num );

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

long double erf ( long double num );

/* floating-point-type */                                  (since C++23)
            erf ( /* floating-point-type */ num );         (constexpr since C++26)

then the f/l girlies:

float       erff( float num );                     (1) (2) (since C++11)
                                                                     (constexpr since C++26)

long double erfl( long double num );                   (3) (since C++11)
                                                                     (constexpr since C++26)

and the bonus integer overloads โ€” "Additional overloads (since C++11)" โ€” also from <cmath>:

template< class Integer >                              (A) (constexpr since C++26)
double      erf ( Integer num );

okay so what do they do:

  1. โ€“ 3. they compute the error function of num ๐Ÿงฎ

and since C++23 the library provides overloads of std::erf for all cv-unqualified floating-point types as the type of the parameter. slay, no more casting.

A) for the integer girlies: additional overloads are provided for all integer types (since C++11), and they're treated as double. periodt.


๐ŸŽ€ PARAMETERS ๐ŸŽ€

  • num โ€” a floating-point or integer value ๐Ÿ“

๐Ÿ’… RETURN VALUE ๐Ÿ’…

if nothing goes wrong, you get the value of the error function of num, which is literally this:

  2      num
------ โˆซ  e^(-tยฒ) dt
 โˆšฯ€     0

that's 2/(โˆšฯ€) ยท โˆซโ‚€^num e^(-tยฒ) dt if you're typing it in a group chat ๐Ÿ’–

but if a range error happens due to underflow, then the correct result (after rounding) is returned instead, and that one is:

2*num
------
  โˆšฯ€

so like 2*num/โˆšฯ€. no cap.


๐Ÿซถ ERROR HANDLING ๐Ÿซถ

errors get reported exactly as specified in math_errhandling. ๐Ÿ“‹

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

  • if the argument is ยฑ0, you get ยฑ0 back. ๐Ÿซง
  • if the argument is ยฑโˆž, you get ยฑ1 back. ๐Ÿ”ฅ
  • if the argument is NaN, you get NaN back. ๐Ÿ’€ (as it should be)

๐Ÿฆ‹ NOTES ๐Ÿฆ‹

underflow is guaranteed if |num| < DBL_MIN * (std::sqrt(ฯ€) / 2). that's a promise, not a maybe. ๐Ÿ˜ญ

also this little beauty:

erf( x / (ฯƒโˆš2) )

is the probability that a measurement whose errors are subject to a normal distribution with standard deviation ฯƒ is less than x away from the mean value. stats girlies rise ๐Ÿ™ˆโœจ

and one technical thing that matters: 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::erf(num) has the same effect as std::erf(static_cast<double>(num)). that's the actual requirement, everything else is vibes. ๐Ÿ’…


๐ŸŒธ EXAMPLE ๐ŸŒธ

this example calculates the probability that a normal variate is on the interval (x1, x2). iconic behaviour honestly ๐Ÿ‘‘

// Run this code

#include <cmath>
#include <iomanip>
#include <iostream>

double phi(double x1, double x2)
{
    return (std::erf(x2 / std::sqrt(2)) - std::erf(x1 / std::sqrt(2))) / 2;
}

int main()
{
    std::cout << "Normal variate probabilities:\n"
              << std::fixed << std::setprecision(2);
    for (int n = -4; n < 4; ++n)
        std::cout << '[' << std::setw(2) << n
                  << ':' << std::setw(2) << n + 1 << "]: "
                  << std::setw(5) << 100 * phi(n, n + 1) << "%\n";

    std::cout << "Special values:\n"
              << "erf(-0) = " << std::erf(-0.0) << '\n'
              << "erf(Inf) = " << std::erf(INFINITY) << '\n';
}

Output

Normal variate probabilities:
[-4:-3]:  0.13%
[-3:-2]:  2.14%
[-2:-1]: 13.59%
[-1: 0]: 34.13%
[ 0: 1]: 34.13%
[ 1: 2]: 13.59%
[ 2: 3]:  2.14%
[ 3: 4]:  0.13%
Special values:
erf(-0) = -0.00
erf(Inf) = 1.00

look at that bell curve distribution, she ate and left no crumbs ๐Ÿฆ‹๐Ÿ“ˆ


โญ SEE ALSO โญ

  • erfc, erfcf, erfcl โ€” complementary error function (C++11) (function) (C++11) (C++11) ๐Ÿ’œ
  • C documentation for erf โ€” go check the C side bestie, it's the same energy ๐ŸŒˆ

  • Weisstein, Eric W. "Erf." From MathWorld โ€” A Wolfram Web Resource. ๐Ÿงš math lore fr fr

so yeah that's the whole page โœจ that's it, that's the error function, go compute some probabilities bestie ๐Ÿ’–

this cutie came from http://cppreference.com, dated 2024.06.10 โ€” shoutout to the whole cppreference gang for keeping us fed, we love you, mwah ๐Ÿซถ๐ŸŽ€
๐Ÿ“ฆ where this page comes from