std::ratio_add(3)
π std::ratio_add π
okay so this is the std::ratio_add(3) page, bestie, and it's from the C++ Standard Libary manual. iconic. It's literally the alias template for adding exact rational fractions, and yes, it ate. π
πΈ NAME πΈ
std::ratio_add β std::ratio_add. that's the whole name, no cap. β¨
β¨ Synopsis β¨
Defined in header <ratio> (mandatory vibe check):
Defined in header <ratio>
template< class R1, class R2 > (since C++11)
using ratio_add = /* see below */;
The alias template std::ratio_add denotes the result of adding two exact rational fractions represented by the std::ratio specializations R1 and R2.
The result is a std::ratio specialization std::ratio<U, V>, such that given Num == R1::num * R2::den + R2::num * R1::den and Denom == R1::den * R2::den (computed without arithmetic overflow), U is std::ratio<Num, Denom>::num and V is std::ratio<Num, Denom>::den.
π Notes π
π¨ warning, bestie: ifUorVis not representable instd::intmax_t, the program is ill-formed. IfNumorDenomis not representable instd::intmax_t, the program is ill-formed unless the implementation yields correct values forUandV.
The above definition requires that the result of std::ratio_add<R1, R2> be already reduced to lowest terms; for example, std::ratio_add<std::ratio<1, 3>, std::ratio<1, 6>> is the same type as std::ratio<1, 2>. lowkey important. π«Ά
π¦ Example π¦
The page says // Run this code, so go off, bestie:
#include <iostream>
#include <ratio>
int main()
{
using two_third = std::ratio<2, 3>;
using one_sixth = std::ratio<1, 6>;
using sum = std::ratio_add<two_third, one_sixth>;
std::cout << "2/3 + 1/6 = " << sum::num << '/' << sum::den << '\n';
}
Output π
2/3 + 1/6 = 5/6
π See also π
ratio_subtractβ subtracts two ratio objects at compile-time(C++11)(alias template). π
mwah π β source/version and date vibes: source/version http://cppreference.com, date 2024.06.10, page std::ratio_add(3). bye bestie, go add some fractions π«Άπ
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms