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();
}
相关推荐
自信150413057591 分钟前
重生之从0开始学习c++之类与对象(中)
c++·学习
不爱吃炸鸡柳8 分钟前
5道经典贪心算法题详解:从入门到进阶
开发语言·数据结构·c++·算法·贪心算法
智者知已应修善业17 分钟前
【51单片机1,左边4个LED灯先闪烁2次后,右边4个LED灯再闪烁2次:2,接着所用灯一起闪烁3次,接着重复步骤1,如此循环。】2023-5-19
c++·经验分享·笔记·算法·51单片机
xiaoye-duck23 分钟前
《算法题讲解指南:优选算法-队列+宽搜》--70.N叉树的层序遍历,71.二叉树的锯齿形层序遍历,72.二叉树的最大宽度,73.在每个树行中找最大值
数据结构·c++·算法·队列
代码改善世界24 分钟前
【C++初阶】双向循环链表:List底层结构的完整实现剖析
c++·链表·list
REDcker27 分钟前
C++ 包管理工具概览
开发语言·c++
努力努力再努力wz31 分钟前
【C++高阶系列】告别内查找局限:基于磁盘 I/O 视角的 B 树深度剖析与 C++ 泛型实现!(附B树实现源码)
java·linux·开发语言·数据结构·c++·b树·算法
承渊政道32 分钟前
【优选算法】(实战攻坚BFS之FloodFill、最短路径问题、多源BFS以及解决拓扑排序)
数据结构·c++·笔记·学习·算法·leetcode·宽度优先
lcj25111 小时前
字符函数,字符串函数,内存函数
c语言·开发语言·c++·windows
吃着火锅x唱着歌1 小时前
深度探索C++对象模型 学习笔记 第三章 Data语意学(2)
c++·笔记·学习