Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::ratio

      From cppreference.com
      <cpp‎ |numeric‎ |ratio
       
       
      Metaprogramming library
      Type traits
      Type categories
      (C++11)
      (C++11)(DR*)
      (C++11)
      (C++11)
      (C++11)
      (C++11)
      (C++11)
      (C++11) 
      Type properties
      (C++11)
      (C++11)
      (C++14)
      (C++11)(deprecated in C++26)
      (C++11)(until C++20*)
      (C++11)(deprecated in C++20)
      (C++11)
      Type trait constants
      Metafunctions
      (C++17)
      Supported operations
      Relationships and property queries
      Type modifications
      Type transformations
      (C++11)(deprecated in C++23)
      (C++11)(deprecated in C++23)
      (C++11)
      (C++11)(until C++20*)(C++17)

      Compile-time rational arithmetic
      Compile-time integer sequences
       
       
      Defined in header<ratio>
      template<

         std::intmax_t Num,
         std::intmax_t Denom=1

      >class ratio;
      (since C++11)

      The class templatestd::ratio providescompile-time rational arithmetic support. Each instantiation of this template exactly represents any finite rational number as long as its numeratorNum and denominatorDenom are representable as compile-time constants of typestd::intmax_t. In addition,Denom may not be zero and bothNum andDenom may not be equal to the most negative value.

      The static data membersnum andden representing the numerator and denominator are calculated by dividingNum andDenom by their greatest common divisor. However, twostd::ratio with differentNum orDenom are distinct types even if they represent the same rational number (after reduction). Astd::ratio type can be reduced to the lowest terms via itstype member:std::ratio<3,6>::type isstd::ratio<1,2>.

      The following convenience typedefs that correspond to the SI ratios are provided by the standard library:

      Defined in header<ratio>
      Type Definition
      quecto(since C++26)std::ratio<1,1000000000000000000000000000000> (10-30)[1]
      ronto(since C++26)std::ratio<1,1000000000000000000000000000>    (10-27)[1]
      yocto(since C++11)std::ratio<1,1000000000000000000000000>       (10-24)[1]
      zepto(since C++11)std::ratio<1,1000000000000000000000>          (10-21)[1]
      atto(since C++11)std::ratio<1,1000000000000000000> (10-18)
      femto(since C++11)std::ratio<1,1000000000000000>    (10-15)
      pico(since C++11)std::ratio<1,1000000000000>       (10-12)
      nano(since C++11)std::ratio<1,1000000000>          (10-9)
      micro(since C++11)std::ratio<1,1000000> (10-6)
      milli(since C++11)std::ratio<1,1000>    (10-3)
      centi(since C++11)std::ratio<1,100>     (10-2)
      deci(since C++11)std::ratio<1,10>      (10-1)
      deca(since C++11)std::ratio<10,1>      (101)
      hecto(since C++11)std::ratio<100,1>     (102)
      kilo(since C++11)std::ratio<1000,1>    (103)
      mega(since C++11)std::ratio<1000000,1> (106)
      giga(since C++11)std::ratio<1000000000,1>          (109)
      tera(since C++11)std::ratio<1000000000000,1>       (1012)
      peta(since C++11)std::ratio<1000000000000000,1>    (1015)
      exa(since C++11)std::ratio<1000000000000000000,1> (1018)
      zetta(since C++11)std::ratio<1000000000000000000000,1>          (1021)[2]
      yotta(since C++11)std::ratio<1000000000000000000000000,1>       (1024)[2]
      ronna(since C++26)std::ratio<1000000000000000000000000000,1>    (1027)[2]
      quetta(since C++26)std::ratio<1000000000000000000000000000000,1> (1030)[2]
      1. 1.01.11.21.3These typedefs are only declared ifstd::intmax_t can represent the denominator.
      2. 2.02.12.22.3These typedefs are only declared ifstd::intmax_t can represent the numerator.

      Contents

      [edit]Nested types

      Type Definition
      typestd::ratio<num, den> (the rational type after reduction)

      [edit]Data members

      In the definitions given below,

      • sign(Denom) is-1 ifDenom is negative, or1 otherwise; and
      • gcd(Num, Denom) is the greatest common divisor ofstd::abs(Num) andstd::abs(Denom).
      Member Definition
      constexprstd::intmax_t num
      [static]
      sign(Denom)* Num/ gcd(Num, Denom)
      (public static member constant)
      constexprstd::intmax_t den
      [static]
      std::abs(Denom)/ gcd(Num, Denom)
      (public static member constant)

      [edit]Notes

      Feature-test macroValueStdFeature
      __cpp_lib_ratio202306L(C++26)Adding the new 2022 SI prefixes: quecto, quetta, ronto, ronna

      [edit]Example

      Run this code
      #include <ratio> static_assert(std::ratio_equal_v<std::ratio_multiply<std::femto, std::exa>, std::kilo>); int main(){}

      [edit]See also

      Mathematical constants(C++20) provides several mathematical constants, such asstd::numbers::e fore[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/numeric/ratio/ratio&oldid=159718"

      [8]ページ先頭

      ©2009-2025 Movatter.jp