std::ratio

The class template std::ratio provides compile-time rational arithmetic support. Each instantiation of this template exactly represents any finite rational number as long as its numerator Num and denominator Denom are representable as compile-time constants of type std::intmax_t. In addition, Denom may not be zero and both Num and Denom may not be equal to the most negative value.

有理数基本用法示例:

cpp 复制代码
#include <iostream>
#include <ratio>
#include <format>
#include <typeinfo>

void test_01(){
    using r1 = std::ratio<1,60>;  // 与std::ratio<2,120>是不同的类型
    std::cout << typeid(r1).name() << std::endl;  // St5ratioILl1ELl60EE

    std::intmax_t num{r1::num};
    std::intmax_t den{r1::den};
    std::cout << std::format("num={}, den={}\n", num, den); // num=1, den=60

    std::intmax_t n{1};
    std::intmax_t d{60};
    // 要求Num和Denom均是编译期常量
    //using r2 = std::ratio<n,d>; // the value of 'n' is not usable in a constant expression
                                  // the value of 'n' is not usable in a constant expression
}

int main() {
    test_01();
}

有理数加法运算示例:

cpp 复制代码
#include <iostream>
#include <ratio>
#include <format>

void test_02(){
    using r1 = std::ratio<1,6>;
    using r2 = std::ratio<1,3>;

    using radd = std::ratio_add<r1, r2>;
    std::cout << std::format("{0}/{1}+{2}/{3}={4}/{5}\n",
        r1::num, r1::den, r2::num, r2::den, radd::num, radd::den);   // 1/6+1/3=1/2

    using rsub = std::ratio_subtract<r1, r2>;
    std::cout << std::format("{0}/{1}-{2}/{3}={4}/{5}\n",
        r1::num, r1::den, r2::num, r2::den, rsub::num, rsub::den);   // 1/6-1/3=-1/6

    using rmultiply = std::ratio_multiply<r1, r2>;
    std::cout << std::format("{0}/{1}*{2}/{3}={4}/{5}\n",
        r1::num, r1::den, r2::num, r2::den, rmultiply::num, rmultiply::den);   // 1/6*1/3=1/18

    using rdivide = std::ratio_divide<r1, r2>;
    std::cout << std::format("{0}/{1}➗{2}/{3}={4}/{5}\n",
        r1::num, r1::den, r2::num, r2::den, rdivide::num, rdivide::den);  // 1/6➗1/3=1/2
}

int main() {
    test_02();
}

有理数比较运算示例:

cpp 复制代码
#include <iostream>
#include <ratio>
#include <format>

void test_03(){
    constexpr bool equ = std::ratio_equal_v<std::ratio<2,3>, std::ratio<4,6>>;
    static_assert(equ == true);
    std::cout << "2/3 " << (equ ? "==" : "!=") << " 4/6\n";  // 2/3 == 4/6

    using r1 = std::ratio<1,6>;
    using r2 = std::ratio<2,12>;
    std::cout << std::format("{}/{}\n", r1::num, r1::den);  // 1/6
    std::cout << std::format("{}/{}\n", r2::num, r2::den);  // 1/6
    constexpr bool r3 = std::ratio_equal<r1, r2>::value;
    std::cout << std::format("{}/{}{}{}/{}\n",
        r1::num, r1::den, r3?"==":"!=",r2::num, r2::den);  // 1/6==1/6

    using r4 = std::ratio_less<r1,r2>;
    std::cout << r4::value << std::endl;  // 0

    using r5 = std::milli;
    std::cout << std::format("{}/{}\n", r5::num, r5::den);  // 1/1000

    using r6 = std::mega;
    std::cout << std::format("{}/{}\n", r6::num, r6::den);  // 1000000/1
}

int main() {
    test_03();
}
相关推荐
程序员龙一1 天前
C++之static_cast关键字
开发语言·c++·static_cast
奶茶树1 天前
【C++/STL】map和multimap的使用
开发语言·c++·stl
云知谷1 天前
【C/C++基本功】C/C++江湖风云录:void* 的江湖传说
c语言·开发语言·c++·软件工程·团队开发
ShineWinsu1 天前
对于数据结构:堆的超详细保姆级解析—上
数据结构·c++·算法·计算机·二叉树·顺序表·
im_AMBER1 天前
Leetcode 46
c语言·c++·笔记·学习·算法·leetcode
QX_hao1 天前
【Go】--文件和目录的操作
开发语言·c++·golang
卡提西亚1 天前
C++笔记-20-对象特性
开发语言·c++·笔记
三掌柜6661 天前
C++ 零基础入门与冒泡排序深度实现
java·开发语言·c++
沐怡旸1 天前
【穿越Effective C++】条款15:在资源管理类中提供对原始资源的访问——封装与兼容性的平衡艺术
c++·面试
利刃大大1 天前
【高并发服务器:HTTP应用】十五、HttpRequest请求模块 && HttpResponse响应模块设计
服务器·c++·http·项目