std::midpoint(3)
β¨ std::midpoint(3) β¨
okay so this is a page from the C++ Standard Library manual (C++ Standard Libary, yes that's how the header spells it, we don't fix it, we love her anyway) and bestie, it's literally about the most wholesome little function ever: std::midpoint. it finds the middle. that's it. that's the whole vibe. π
π NAME π
std::midpoint β literally std::midpoint, she contains multitudes, the name is the name π«Ά
β¨ Synopsis β¨
First things first, she lives in this header:
#include <numeric>
And here's her whole gorgeous signature situation:
template< class T > (1) (since C++20)
constexpr T midpoint( T a, T b ) noexcept;
template< class T > (2) (since C++20)
constexpr T* midpoint( T* a, T* b );
So what does she do? She computes the midpoint of the integers, floating-points, or pointers a and b. that's the whole job. middle-finding icon behavior π
- This overload only shows up in overload resolution if
Tis an arithmetic type other thanbool. (sorrybool, not today π) - This overload only shows up in overload resolution if
Tis an object type. And heads up: using this overload is ill-formed ifTis an incomplete type. so don't do that. π
π¦ Parameters π¦
a,bβ integers, floating-points, or pointer values. the two besties who need a middle friend π
πΈ Return value πΈ
- Half the sum of
aandb. and the best part? No overflow occurs. none. zero. not a single one. π Ifaandbhave integer type and the sum is odd, the result is rounded towardsa. Ifaandbhave floating-point type, at most one inexact operation occurs. (at most one, she's precise like that β¨) - If
aandbpoint to, respectively,x[i]andx[j]of the same array objectx(for the purpose of pointer arithmetic), it returns a pointer tox[i + (j - i) / 2](or, equivalentlyx[std::midpoint(i, j)]), where the division rounds towards zero. BUT β and this is the important part bestie β ifaandbdo not point to elements of the same array object, the behavior is undefined. π« undefined behavior is not the vibe. don't.
π§ Exceptions π§
Throws no exceptions. literally none. she said "no drama, only midpoint." π
π Notes π
Overload (2) can be simply implemented as return a + (b - a) / 2; on common platforms. sounds easy right? but nope β such an implementation is not guaranteed to be portable, because there may be some platforms where creating an array with a number of elements greater than PTRDIFF_MAX is possible, and b - a may result in undefined behavior even if both b and a point to elements in the same array. so like, the shortcut is giving "works on my machine" energy and that's not it. π
Also there's a feature-test macro, and she's a whole table, iykyk:
__cpp_lib_interpolateβ value201902L, standard(C++20), feature:std::lerp,std::midpointπ
π₯ Example π₯
// Run this code
#include <cstdint>
#include <iostream>
#include <limits>
#include <numeric>
int main()
{
std::uint32_t a = std::numeric_limits<std::uint32_t>::max();
std::uint32_t b = std::numeric_limits<std::uint32_t>::max() - 2;
std::cout << "a: " << a << '\n'
<< "b: " << b << '\n'
<< "Incorrect (overflow and wrapping): " << (a + b) / 2 << '\n'
<< "Correct: " << std::midpoint(a, b) << "\n\n";
auto on_pointers = [](int i, int j)
{
char const* text = "0123456789";
char const* p = text + i;
char const* q = text + j;
std::cout << "std::midpoint('" << *p << "', '" << *q << "'): '"
<< *std::midpoint(p, q) << "'\n";
};
on_pointers(2, 4);
on_pointers(2, 5);
on_pointers(5, 2);
on_pointers(2, 6);
}
π Output π
a: 4294967295
b: 4294967293
Incorrect (overflow and wrapping): 2147483646
Correct: 4294967294
std::midpoint('2', '4'): '3'
std::midpoint('2', '5'): '3'
std::midpoint('5', '2'): '4'
std::midpoint('2', '6'): '4'
like, look at that. the "incorrect" one overflowed and wrapped and gave a whole wrong answer, and std::midpoint just β¨ delivered β¨ the correct one. and the pointer version rounds towards zero so ('5', '2') gives you '4' not '3' β she's got rules and she's sticking to them π
periodt.
π References π
- C++23 standard (ISO/IEC 14882:2023):
- 27.10.16 Midpoint [numeric.ops.midpoint]
- C++20 standard (ISO/IEC 14882:2020):
- 25.10.15 Midpoint [numeric.ops.midpoint]
(the receipts, in case anyone tries to come for her π)
π See also π
lerpβ linear interpolation function,(C++20)(function)β her cousin who interpolates. different job, same family. π«Ά
πΈ anyway bestie that's the whole page, no cap, every detail intact, we ate and left no crumbs π this masterpiece was brought to you by http://cppreference.com, dated 2024.06.10, and yeah the footer still says std::midpoint(3) because she's iconic and she knows it β
π¦ 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