diff --git a/README.md b/README.md index a1bf5d3..66f0733 100644 --- a/README.md +++ b/README.md @@ -171,6 +171,11 @@ type ModExmaple = Mod<87, 7> * [`Max`](#max) * [`Min`](#min) +## Rounding Operations +- [`Round`](#round) +- [`Floor`](#floor) +- [`Ceil`](#ceil) + ## Other Checks ### Sign * [`IsPositive`](#ispositive) @@ -514,7 +519,45 @@ type NormalExample3 = Min<10.1234, -5.111> // -5.111 type UnionExample = Min<5|8|10, 7> // 5 | 7 type NumberExample = Min // number +``` +### `Round` +Round the number to the nearest integer, rounding up if it equidistant. +```ts +type PositiveExample1 = Round<13> // 13 +type PositiveExample2 = Round<13.47> // 13 +type PositiveExample3 = Round<13.50> // 14 +type PositiveExample4 = Round<13.85> // 14 + +type NegativeExample1 = Round<-8.21> // -8 +type NegativeExample2 = Round<-8.50> // -8 +type NegativeExample3 = Round<-8.94> // -9 +``` +### `Floor` +Round the number to the closest integer less than or equal to the given number. +```ts +type PositiveExample1 = Floor<3> // 3 +type PositiveExample2 = Floor<3.31> // 3 +type PositiveExample3 = Floor<3.50> // 3 +type PositiveExample4 = Floor<3.99> // 3 + +type NegativeExample1 = Floor<-6> // -6 +type NegativeExample2 = Floor<-6.50> // -7 +type NegativeExample3 = Floor<-6.92> // -7 +type NegativeExample4 = Floor<-7.11> // -8 + +``` +### `Ceil` +Round the number to the closest integer greater than or equal to the given number. +```ts +type PositiveExample1 = Ceil<70> // 70 +type PositiveExample2 = Ceil<70.12> // 71 +type PositiveExample3 = Ceil<70.50> // 71 +type PositiveExample4 = Ceil<70.88> // 71 +type NegativeExample1 = Ceil<-8> // -8 +type NegativeExample2 = Ceil<-8.21> // -8 +type NegativeExample3 = Ceil<-8.50> // -8 +type NegativeExample4 = Ceil<-8.94> // -8 ``` ### `IsPositive` Check if a numeric literal is positive. diff --git a/src/Arithmetic/Number/Round.ts b/src/Arithmetic/Number/Round.ts new file mode 100644 index 0000000..532eecb --- /dev/null +++ b/src/Arithmetic/Number/Round.ts @@ -0,0 +1,50 @@ +import type { Abs, IsNegative, Negate } from './Sign' +import type { Gt, GtOrEq } from './Compare' +import type { Multiply } from '../Multiplication/Multiply' +import type { Subtract } from '../Subtraction/Subtract' +import type { IsInt } from './IsInt' +import type { Mod } from '../../Arithmetic/Division' +import type { Add } from '../../Arithmetic/Addition' + +type TakeIntegerComponent = ( + `${N}` extends `${infer I extends number}.${string}` ? I : N +) + +/** + * Round the number to the nearest integer, rounding up if it equidistant. + * + * @param N - The number to round. + * @returns round(N) - The closest integer to the given number, rounding up if it is equidistant. + * + * @public +*/ +export type Round = ( + IsInt extends 1 ? N : + IsNegative extends 1 + ? Negate, 1>, 10>, 5>, TakeIntegerComponent>> + : Add, 10>, 5>, TakeIntegerComponent> +) + +/** + * Round the number to the closest integer less than or equal to the given number. + * + * @param N - The number to round. + * @returns floor(N) - The closest integer less than or equal to the given number. + * + * @public +*/ +export type Floor = ( + IsInt extends 1 ? N : Negate, TakeIntegerComponent>> +) + +/** + * Round the number to the closest integer greater than or equal to the given number. + * + * @param N - The number to round. + * @returns ceil(N) - The closest integer greater than or equal to the given number. + * + * @public +*/ +export type Ceil = ( + IsInt extends 1 ? N : Add, 1> +) diff --git a/src/Arithmetic/Number/index.ts b/src/Arithmetic/Number/index.ts index 7d6388c..64d3690 100644 --- a/src/Arithmetic/Number/index.ts +++ b/src/Arithmetic/Number/index.ts @@ -6,4 +6,5 @@ export type { IsOdd, IsEven } from './Parity' export type { ToUnsignedFloat, ToSignedFloat } from './ToFloat' export type { ToSignedInt } from './ToInt' export type { IsInt, IsNotInt } from './IsInt' -export type { Gt, Lt, Eq, GtOrEq, LtOrEq, Max, Min } from './Compare' \ No newline at end of file +export type { Gt, Lt, Eq, GtOrEq, LtOrEq, Max, Min } from './Compare' +export type { Round, Floor, Ceil } from './Round' \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 9abd420..166b10a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,5 +4,5 @@ export type { Multiply } from './Arithmetic/Multiplication' export type { Divide, Mod } from './Arithmetic/Division' export type { Pow } from './Arithmetic/Exponentiation' export type { Negate, Abs, IsPositive, IsNegative, Compare, IsOdd, IsEven, IsInt, IsNotInt } from './Arithmetic/Number' -export type { Gt, Lt, Eq, GtOrEq, LtOrEq, Max, Min } from './Arithmetic/Number' +export type { Gt, Lt, Eq, GtOrEq, LtOrEq, Max, Min, Round, Floor, Ceil } from './Arithmetic/Number' export type { Bit, And, Or, Xor, Not } from './Arithmetic/Bit'