1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[C++] 浮動小数点数をゼロからイチの間に正規化するコード

Last updated at Posted at 2025-05-06
inline constexpr auto zero_to_one = []<std::floating_point T>(const T x) noexcept {
  static_assert(sizeof(T) <= 8);
  using U = std::conditional_t<sizeof(T) == 4, int, long long>;
  constexpr auto one = std::bit_cast<U>(T(1));
  return std::bit_cast<T>(-U(T(0) < x) & (one ^ ((std::bit_cast<U>(x) ^ one) & -U(x < T(1)))));
};

C++だけで書くならこれが最速ではないでしょうか。
別の案があればコメントで教えてください。

1
2
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?