CnxRatio module
CnxRatio provides lossless methods for dealing with exact fractions
Example:
{./c} let time_seconds = 1000; let time_milliseconds_ratio = cnx_ratio_multiply(cnx_milli, time_seconds); // try to get the scalar value as an integer if it can be represented by one let maybe_exact_milliseconds = cnx_ratio_as_scalar_exact(time_milliseconds_ratio); let exact = cnx_option_unwrap(maybe_exact); // get the scalar value as an f64 let milliseconds_f64 = cnx_ratio_as_scalar(time_milliseconds);
Classes
Typedefs
Functions
- CnxRatio cnx_ratio_new(i64 num, i64 den)
- Constructs a
CnxRatio - CnxRatio cnx_ratio_add(CnxRatio lhs, CnxRatio rhs)
- Adds the given
CnxRatios. - CnxRatio cnx_ratio_subtract(CnxRatio lhs, CnxRatio rhs)
- Subtracts the given
CnxRatios. - CnxRatio cnx_ratio_multiply(CnxRatio lhs, CnxRatio rhs)
- Multiplies the given
CnxRatios. - CnxRatio cnx_ratio_divide(CnxRatio lhs, CnxRatio rhs)
- Divides the given
CnxRatios. - bool cnx_ratio_equal(CnxRatio lhs, CnxRatio rhs)
- Returns whether the two
CnxRatios are equal. - bool cnx_ratio_not_equal(CnxRatio lhs, CnxRatio rhs)
- Returns whether the two
CnxRatios are not equal. - bool cnx_ratio_less_than(CnxRatio lhs, CnxRatio rhs)
- Returns whether the left-hand
CnxRatiois less than the other. - bool cnx_ratio_less_than_or_equal(CnxRatio lhs, CnxRatio rhs)
- Returns whether the left-hand
CnxRatiois less than or equal to the other. - bool cnx_ratio_greater_than(CnxRatio lhs, CnxRatio rhs)
- Returns whether the left-hand
CnxRatiois greater than the other. - bool cnx_ratio_greater_than_or_equal(CnxRatio lhs, CnxRatio rhs)
- Returns whether the left-hand
CnxRatiois greater than or equal to the other. - CnxCompare cnx_ratio_compare(CnxRatio lhs, CnxRatio rhs)
- Returns the comparison of the left-hand
CnxRatioto the other. - CnxOption(i64)
- Returns the given
CnxRatioas a scalar value. - f64 cnx_ratio_as_scalar(CnxRatio ratio)
- Returns the given
CnxRatioas a scalar value. - CnxRatio cnx_ratio_add_scalar(CnxRatio ratio, i64 scalar)
- Adds the given ratio and scalar value.
- CnxRatio cnx_ratio_subtract_scalar(CnxRatio ratio, i64 scalar)
- Subtracts the given scalar value from the given ratio.
- CnxRatio cnx_ratio_multiply_scalar(CnxRatio ratio, i64 scalar)
- Multiplies the given ratio and scalar value.
- CnxRatio cnx_ratio_divide_scalar(CnxRatio ratio, i64 scalar)
- Divides the given ratio by the given scalar value.
-
static ImplTraitFor(CnxFormat,
CnxRatio,
cnx_
ratio_ is_ specifier_ valid, cnx_ ratio_ format, cnx_ ratio_ format_ with_ allocator) - Implement
CnxFormatforCnxRatio
Defines
- #define cnx_ratio(numerator, denominator)
- Constructs a
CnxRatioat compile time. - #define cnx_atto
- SI "atto" as a
CnxRatio - #define cnx_femto
- SI "femto" as a
CnxRatio - #define cnx_pico
- SI "pico" as a
CnxRatio - #define cnx_nano
- SI "nano" as a
CnxRatio - #define cnx_micro
- SI "micro" as a
CnxRatio - #define cnx_milli
- SI "milli" as a
CnxRatio - #define cnx_centi
- SI "centi" as a
CnxRatio - #define cnx_deci
- SI "deci" as a
CnxRatio - #define cnx_deca
- SI "deca" as a
CnxRatio - #define cnx_hecto
- SI "hecto" as a
CnxRatio - #define cnx_kilo
- SI "kilo" as a
CnxRatio - #define cnx_mega
- SI "mega" as a
CnxRatio - #define cnx_giga
- SI "giga" as a
CnxRatio - #define cnx_tera
- SI "tera" as a
CnxRatio - #define cnx_peta
- SI "peta" as a
CnxRatio - #define cnx_exa
- SI "exa" as a
CnxRatio
Typedef documentation
typedef struct CnxRatio CnxRatio
#include <include/Cnx/Ratio.h>
CnxRatio is a representation of an exact fraction
Function documentation
CnxRatio cnx_ratio_new(i64 num,
i64 den)
#include <include/Cnx/Ratio.h>
Constructs a CnxRatio
| Parameters | |
|---|---|
| num | - The numerator of the ratio |
| den | - The denominator of the ratio |
| Returns | A CnxRatio as the simplified fraction of numerator / denominator |
Constructs a CnxRatio from the given numerator and denominator, as a simplified fraction
CnxRatio cnx_ratio_add(CnxRatio lhs,
CnxRatio rhs)
#include <include/Cnx/Ratio.h>
Adds the given CnxRatios.
| Parameters | |
|---|---|
| lhs | - The left-hand ratio to add |
| rhs | - The right-hand ratio to add |
| Returns | The sum of lhs and rhs as a CnxRatio in simplest form |
Adds the given CnxRatios, returning the result as a new CnxRatio in simplest form
CnxRatio cnx_ratio_subtract(CnxRatio lhs,
CnxRatio rhs)
#include <include/Cnx/Ratio.h>
Subtracts the given CnxRatios.
| Parameters | |
|---|---|
| lhs | - The left-hand ratio to subtract from |
| rhs | - The right-hand ratio to subtract |
| Returns | The difference of lhs and rhs as a CnxRatio in simplest form |
Subtracts rhs from lhs, returning the result as a new CnxRatio in simplest form
CnxRatio cnx_ratio_multiply(CnxRatio lhs,
CnxRatio rhs)
#include <include/Cnx/Ratio.h>
Multiplies the given CnxRatios.
| Parameters | |
|---|---|
| lhs | - The left-hand ratio to multiply |
| rhs | - The right-hand ratio to multiply |
| Returns | The product of lhs and rhs as a CnxRatio in simplest form |
Multiplies the given CnxRatios, returning the result as a new CnxRatio in simplest form
CnxRatio cnx_ratio_divide(CnxRatio lhs,
CnxRatio rhs)
#include <include/Cnx/Ratio.h>
Divides the given CnxRatios.
| Parameters | |
|---|---|
| lhs | - The CnxRatio dividend |
| rhs | - The CnxRatio divisor |
| Returns | The quotient of lhs and rhs as a CnxRatio in simplest form |
Divides lhs by rhs, returning the result as a new CnxRatio in simplest form
bool cnx_ratio_equal(CnxRatio lhs,
CnxRatio rhs)
#include <include/Cnx/Ratio.h>
Returns whether the two CnxRatios are equal.
| Parameters | |
|---|---|
| lhs | - The left-hand ratio to check for equality |
| rhs | - The right-hand ratio to check for equality |
| Returns | Whether the given ratios are equal |
bool cnx_ratio_not_equal(CnxRatio lhs,
CnxRatio rhs)
#include <include/Cnx/Ratio.h>
Returns whether the two CnxRatios are not equal.
| Parameters | |
|---|---|
| lhs | - The left-hand ratio to check for inequality |
| rhs | - The right-hand ratio to check for inequality |
| Returns | Whether the given ratios are not equal |
bool cnx_ratio_less_than(CnxRatio lhs,
CnxRatio rhs)
#include <include/Cnx/Ratio.h>
Returns whether the left-hand CnxRatio is less than the other.
| Parameters | |
|---|---|
| lhs | - The ratio to compare |
| rhs | - The ratio to compare against |
| Returns | Whether lhs is less than rhs |
bool cnx_ratio_less_than_or_equal(CnxRatio lhs,
CnxRatio rhs)
#include <include/Cnx/Ratio.h>
Returns whether the left-hand CnxRatio is less than or equal to the other.
| Parameters | |
|---|---|
| lhs | - The ratio to compare |
| rhs | - The ratio to compare against |
| Returns | Whether lhs is less than or equal to rhs |
bool cnx_ratio_greater_than(CnxRatio lhs,
CnxRatio rhs)
#include <include/Cnx/Ratio.h>
Returns whether the left-hand CnxRatio is greater than the other.
| Parameters | |
|---|---|
| lhs | - The ratio to compare |
| rhs | - The ratio to compare against |
| Returns | Whether lhs is greater than rhs |
bool cnx_ratio_greater_than_or_equal(CnxRatio lhs,
CnxRatio rhs)
#include <include/Cnx/Ratio.h>
Returns whether the left-hand CnxRatio is greater than or equal to the other.
| Parameters | |
|---|---|
| lhs | - The ratio to compare |
| rhs | - The ratio to compare against |
| Returns | Whether lhs is greater than or equal to rhs |
CnxCompare cnx_ratio_compare(CnxRatio lhs,
CnxRatio rhs)
#include <include/Cnx/Ratio.h>
Returns the comparison of the left-hand CnxRatio to the other.
| Parameters | |
|---|---|
| lhs | - The ratio to compare |
| rhs | - The ratio to compare against |
| Returns | the comparison of lhs to rhs |
CnxOption(i64)
#include <include/Cnx/Ratio.h>
Returns the given CnxRatio as a scalar value.
| Returns | Some(i64, value) if ratio can be represented as a scalar, None(i64) otherwise |
|---|
Returns the given CnxRatio as a scalar value (Some(i64, value)), if it can be exactly represented as one, otherwise returns None(i64)
Returns the given CnxRatio as a scalar value.
Performs the checked division of left and right
Performs the checked multiplication of left and right
Performs the checked subtraction of right from left
Performs the checked addition of left and right, returning Some(i64, result) if overflow did not occur or None(i64) if overflow occurred
Performs the checked subtraction of right from left, returning Some(i64, result) if overflow did not occur or None(i64) if overflow occurred
Performs the checked multiplication of left and right, returning Some(i64, result) if overflow did not occur or None(i64) if overflow occurred
Performs the checked division of left and right, returning Some(i64, result) if overflow did not occur or None(i64) if overflow occurred
f64 cnx_ratio_as_scalar(CnxRatio ratio)
#include <include/Cnx/Ratio.h>
Returns the given CnxRatio as a scalar value.
| Parameters | |
|---|---|
| ratio | - The CnxRatio to convert to a scalar |
| Returns | ratio as a scalar value |
CnxRatio cnx_ratio_add_scalar(CnxRatio ratio,
i64 scalar)
#include <include/Cnx/Ratio.h>
Adds the given ratio and scalar value.
| Parameters | |
|---|---|
| ratio | - The CnxRatio to add to |
| scalar | - The scalar value to add |
| Returns | the CnxRatio resulting from the addition |
Adds the given CnxRatio and scalar value and returns the result as a CnxRatio
CnxRatio cnx_ratio_subtract_scalar(CnxRatio ratio,
i64 scalar)
#include <include/Cnx/Ratio.h>
Subtracts the given scalar value from the given ratio.
| Parameters | |
|---|---|
| ratio | - The CnxRatio to subtract from |
| scalar | - The scalar value to subtract |
| Returns | the CnxRatio resulting from the subtraction |
Subtracts the scalar value from the given CnxRatio and returns the result as a CnxRatio
CnxRatio cnx_ratio_multiply_scalar(CnxRatio ratio,
i64 scalar)
#include <include/Cnx/Ratio.h>
Multiplies the given ratio and scalar value.
| Parameters | |
|---|---|
| ratio | - The CnxRatio to multiply with |
| scalar | - The scalar value to multiply with |
| Returns | the CnxRatio resulting from the multiplication |
Multiplies the given CnxRatio and scalar value and returns the result as a CnxRatio
CnxRatio cnx_ratio_divide_scalar(CnxRatio ratio,
i64 scalar)
#include <include/Cnx/Ratio.h>
Divides the given ratio by the given scalar value.
| Parameters | |
|---|---|
| ratio | - The CnxRatio dividend |
| scalar | - The scalar value divisor |
| Returns | the CnxRatio resulting from the division |
Divides the given CnxRatio by the given scalar value and returns the result as a CnxRatio
Define documentation
#define cnx_ratio(numerator,
denominator)
#include <include/Cnx/Ratio.h>
Constructs a CnxRatio at compile time.
| Parameters | |
|---|---|
| numerator | - The numerator of the ratio |
| denominator | - The denominator of the ratio |
| Returns | A CnxRatio as the simplified fraction of numerator / denominator |
Constructs a CnxRatio from the given numerator and denominator, as a simplified fraction at compile time.
#define cnx_atto
#include <include/Cnx/Ratio.h>
SI "atto" as a CnxRatio
#define cnx_femto
#include <include/Cnx/Ratio.h>
SI "femto" as a CnxRatio
#define cnx_pico
#include <include/Cnx/Ratio.h>
SI "pico" as a CnxRatio
#define cnx_nano
#include <include/Cnx/Ratio.h>
SI "nano" as a CnxRatio
#define cnx_micro
#include <include/Cnx/Ratio.h>
SI "micro" as a CnxRatio
#define cnx_milli
#include <include/Cnx/Ratio.h>
SI "milli" as a CnxRatio
#define cnx_centi
#include <include/Cnx/Ratio.h>
SI "centi" as a CnxRatio
#define cnx_deci
#include <include/Cnx/Ratio.h>
SI "deci" as a CnxRatio
#define cnx_deca
#include <include/Cnx/Ratio.h>
SI "deca" as a CnxRatio
#define cnx_hecto
#include <include/Cnx/Ratio.h>
SI "hecto" as a CnxRatio
#define cnx_kilo
#include <include/Cnx/Ratio.h>
SI "kilo" as a CnxRatio
#define cnx_mega
#include <include/Cnx/Ratio.h>
SI "mega" as a CnxRatio
#define cnx_giga
#include <include/Cnx/Ratio.h>
SI "giga" as a CnxRatio
#define cnx_tera
#include <include/Cnx/Ratio.h>
SI "tera" as a CnxRatio
#define cnx_peta
#include <include/Cnx/Ratio.h>
SI "peta" as a CnxRatio
#define cnx_exa
#include <include/Cnx/Ratio.h>
SI "exa" as a CnxRatio