std::erf,st...f,std::erfl(3)
๐ 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:
- โ 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ยฑ0back. ๐ซง - if the argument is
ยฑโ, you getยฑ1back. ๐ฅ - if the argument is
NaN, you getNaNback. ๐ (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 ๐
๐ EXTERNAL LINKS ๐
- 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
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 ยท MIT
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms