Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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, 7> // 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.
Expand Down
50 changes: 50 additions & 0 deletions src/Arithmetic/Number/Round.ts
Original file line number Diff line number Diff line change
@@ -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 number> = (
`${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<N extends number> = (
IsInt<N> extends 1 ? N :
IsNegative<N> extends 1
? Negate<Subtract<Gt<Multiply<Mod<Abs<N>, 1>, 10>, 5>, TakeIntegerComponent<N>>>
: Add<GtOrEq <Multiply<Mod< N , 1>, 10>, 5>, TakeIntegerComponent<N>>
)

/**
* 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<N extends number> = (
IsInt<N> extends 1 ? N : Negate<Subtract<IsNegative<N>, TakeIntegerComponent<N>>>
)

/**
* 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<N extends number> = (
IsInt<N> extends 1 ? N : Add<Floor<N>, 1>
)
3 changes: 2 additions & 1 deletion src/Arithmetic/Number/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
export type { Gt, Lt, Eq, GtOrEq, LtOrEq, Max, Min } from './Compare'
export type { Round, Floor, Ceil } from './Round'
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'