Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::numeric_limits<T>::epsilon

      From cppreference.com
      <cpp‎ |types‎ |numeric limits
       
       
      Utilities library
       
       
      std::numeric_limits
      Static constants
      Static member functions
      numeric_limits::epsilon
      Helper types
       
      static T epsilon()throw();
      (until C++11)
      staticconstexpr T epsilon()noexcept;
      (since C++11)

      Returns the machine epsilon, that is, the difference between1.0 and the next value representable by the floating-point typeT. It is only meaningful ifstd::numeric_limits<T>::is_integer==false.

      [edit]Return value

      Tstd::numeric_limits<T>::epsilon()
      /* non-specialized */T()
      boolfalse
      char0
      signedchar0
      unsignedchar0
      wchar_t0
      char8_t(since C++20)0
      char16_t(since C++11)0
      char32_t(since C++11)0
      short0
      unsignedshort0
      int0
      unsignedint0
      long0
      unsignedlong0
      longlong(since C++11)0
      unsignedlonglong(since C++11)0
      floatFLT_EPSILON
      doubleDBL_EPSILON
      longdoubleLDBL_EPSILON

      [edit]Example

      Demonstrates the use of machine epsilon to compare floating-point values for equality:

      Run this code
      #include <algorithm>#include <cmath>#include <cstddef>#include <iomanip>#include <iostream>#include <limits>#include <type_traits> template<class T>std::enable_if_t<notstd::numeric_limits<T>::is_integer,bool>equal_within_ulps(T x, T y,std::size_t n){// Since `epsilon()` is the gap size (ULP, unit in the last place)// of floating-point numbers in interval [1, 2), we can scale it to// the gap size in interval [2^e, 2^{e+1}), where `e` is the exponent// of `x` and `y`. // If `x` and `y` have different gap sizes (which means they have// different exponents), we take the smaller one. Taking the bigger// one is also reasonable, I guess.const T m=std::min(std::fabs(x),std::fabs(y)); // Subnormal numbers have fixed exponent, which is `min_exponent - 1`.constint exp= m<std::numeric_limits<T>::min()?std::numeric_limits<T>::min_exponent-1:std::ilogb(m); // We consider `x` and `y` equal if the difference between them is// within `n` ULPs.returnstd::fabs(x- y)<= n*std::ldexp(std::numeric_limits<T>::epsilon(), exp);} int main(){double x=0.3;double y=0.1+0.2;std::cout<<std::hexfloat;std::cout<<"x = "<< x<<'\n';std::cout<<"y = "<< y<<'\n';std::cout<<(x== y?"x == y":"x != y")<<'\n';for(std::size_t n=0; n<=10;++n)if(equal_within_ulps(x, y, n)){std::cout<<"x equals y within "<< n<<" ulps"<<'\n';break;}}

      Output:

      x = 0x1.3333333333333p-2y = 0x1.3333333333334p-2x != yx equals y within 1 ulps

      [edit]See also

      (C++11)(C++11)(C++11)(C++11)(C++11)(C++11)
      next representable floating-point value towards the given value
      (function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/types/numeric_limits/epsilon&oldid=161249"

      [8]ページ先頭

      ©2009-2025 Movatter.jp