Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::midpoint

      From cppreference.com
      <cpp‎ |numeric
       
       
       
      Defined in header<numeric>
      template<class T>
      constexpr T midpoint( T a, T b)noexcept;
      (1)(since C++20)
      template<class T>
      constexpr T* midpoint( T* a, T* b);
      (2)(since C++20)

      Computes the midpoint of the integers, floating-points, or pointersa andb.

      1) This overload participates in overload resolution only ifT is an arithmetic type other thanbool.
      2) This overload participates in overload resolution only ifT is an object type. Use of this overload is ill-formed ifT is anincomplete type.

      Contents

      [edit] Parameters

      a, b - integers, floating-points, or pointer values

      [edit]Return value

      1) Half the sum ofa andb. No overflow occurs. Ifa andb have integer type and the sum is odd, the result is rounded towardsa. Ifa andb have floating-point type, at most one inexact operation occurs.
      2) Ifa andb point to, respectively,x[i] andx[j] of the same array objectx (for the purpose ofpointer arithmetic), returns a pointer tox[i+(j- i)/2] (or, equivalentlyx[std::midpoint(i, j)]) where the division rounds towards zero. Ifa andb do not point to elements of the same array object, the behavior is undefined.

      [edit] Exceptions

      Throws no exceptions.

      [edit]Notes

      Overload(2) can be simply implemented asreturn a+(b- a)/2; on common platforms. However, such implementation is not guaranteed to be portable, because there may be some platforms where creating an array with number of elements greater thanPTRDIFF_MAX is possible, andb- a may result in undefined behavior even if bothb anda point to elements in the same array.

      Feature-test macroValueStdFeature
      __cpp_lib_interpolate201902L(C++20)std::lerp,std::midpoint

      [edit]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){charconst* text="0123456789";charconst* p= text+ i;charconst* 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: 4294967295b: 4294967293Incorrect (overflow and wrapping): 2147483646Correct: 4294967294 std::midpoint('2', '4'): '3'std::midpoint('2', '5'): '3'std::midpoint('5', '2'): '4'std::midpoint('2', '6'): '4'

      [edit]References

      • C++23 standard (ISO/IEC 14882:2024):
      • 27.10.16 Midpoint [numeric.ops.midpoint]
      • C++20 standard (ISO/IEC 14882:2020):
      • 25.10.15 Midpoint [numeric.ops.midpoint]

      [edit]See also

      (C++20)
      linear interpolation function
      (function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/numeric/midpoint&oldid=151507"

      [8]ページ先頭

      ©2009-2025 Movatter.jp