最終更新日時:
が更新

履歴 編集

function template
<simd>

std::simd::hypot(C++26)

namespace std::simd {
  template<math-floating-point V>
  constexpr deduced-vec-t<V> hypot(const V& x, const V& y);                          // (1) C++26
  template<math-floating-point V>
  constexpr deduced-vec-t<V> hypot(const deduced-vec-t<V>& x, const V& y);       // (2) C++26
  template<math-floating-point V>
  constexpr deduced-vec-t<V> hypot(const V& x, const deduced-vec-t<V>& y);       // (3) C++26

  template<math-floating-point V>
  constexpr deduced-vec-t<V> hypot(const V& x, const V& y, const V& z);              // (4) C++26
  template<math-floating-point V>
  constexpr deduced-vec-t<V> hypot(const deduced-vec-t<V>& x,
                                       const V& y, const V& z);                          // (5) C++26
  template<math-floating-point V>
  constexpr deduced-vec-t<V> hypot(const V& x,
                                       const deduced-vec-t<V>& y, const V& z);       // (6) C++26
  template<math-floating-point V>
  constexpr deduced-vec-t<V> hypot(const V& x, const V& y,
                                       const deduced-vec-t<V>& z);                   // (7) C++26
  template<math-floating-point V>
  constexpr deduced-vec-t<V> hypot(const deduced-vec-t<V>& x,
                                       const deduced-vec-t<V>& y, const V& z);       // (8) C++26
  template<math-floating-point V>
  constexpr deduced-vec-t<V> hypot(const deduced-vec-t<V>& x, const V& y,
                                       const deduced-vec-t<V>& z);                   // (9) C++26
  template<math-floating-point V>
  constexpr deduced-vec-t<V> hypot(const V& x, const deduced-vec-t<V>& y,
                                       const deduced-vec-t<V>& z);                   // (10) C++26
}

概要

浮動小数点数型を要素とするbasic_vecについて、各要素の直角三角形の斜辺の長さ(引数の平方和の平方根)を求める。各要素に<cmath>hypotを要素ごとに適用したものである。

  • (1) : 2引数版。両引数をVとして受け取り、各要素について x2 + y2 を求める。
  • (2), (3) : 2引数版で、一方の引数をスカラー値(または対応するbasic_vec)で受け取れるようにするオーバーロードである。
  • (4) : 3引数版。3引数をVとして受け取り、各要素について x2 + y2 + z2 を求める。
  • (5)〜(10) : 3引数版で、一部の引数をスカラー値(または対応するbasic_vec)で受け取れるようにするオーバーロードである。

制約math-floating-pointは、Vが浮動小数点数型を要素とするbasic_vecであることを表す説明専用のコンセプトである。deduced-vec-t<V>Vから導出されるbasic_vec型(通常はV自身)を表す。一部の引数をdeduced-vec-t<V>とするオーバーロードにより、スカラー値を渡して全要素にブロードキャストできる。

戻り値

各要素i0以上V::size()未満)について、各引数の第i要素に<cmath>hypotを適用した結果に要素ごとに等しいbasic_vecオブジェクトを返す。

ある要素iについて定義域エラー・極エラー・値域エラーが発生する場合、その要素の値は未規定である。

備考

errnoにアクセスするか否かは未規定である。

#include <simd>
#include <print>

namespace simd = std::simd;

int main()
{
  simd::vec<float, 4> x([](int i) { return float(i + 1); }); // {1, 2, 3, 4}
  simd::vec<float, 4> y{[](int i) {
    float table[] = {0.0f, 0.0f, 4.0f, 3.0f};
    return table[i];
  }};

  // 各要素について sqrt(x^2 + y^2) を求める
  simd::vec<float, 4> r = simd::hypot(x, y);

  for (std::size_t i = 0; i < r.size(); ++i)
    std::print("{} ", r[i]);
  std::println("");
}

出力例

1 2 5 5 

バージョン

言語

  • C++26

処理系

関連項目

参照