https://github.com/hypercubed/real
https://github.com/hypercubed/real
Last synced: 5 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/hypercubed/real
- Owner: Hypercubed
- License: mit
- Created: 2019-07-23T07:06:47.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2023-01-04T05:39:12.000Z (over 3 years ago)
- Last Synced: 2025-04-06T22:43:04.245Z (about 1 year ago)
- Language: TypeScript
- Size: 874 KB
- Stars: 1
- Watchers: 1
- Forks: 1
- Open Issues: 10
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# Real
`Real` provides an object with properties and methods for mathematical constants and functions (similar to `Math`) that works with `Number` and `BigInt` as well as two additional classes (`Rational` and `Irrational`).
### Usage
```ts
import * as RealMath from '@hypercubed/real';
```
`RealMath` provides several math functions that work with `Number` and `BigInt`. These two types, however, are not sufficient in many cases. `@hypercubed/real` provides two additional classes that represent broader numeric types:
- `Rational` - any number that can be expressed as the quotient (or fraction) of two integers (BigInt) (`p/q`)
- `Irrational` - an approximation of a number expressed in scientific notation (`m x 10 ^ n`)
When using the `RealMath` functions, values are converted, as necessary, for the given function. Implict conversions are as follows:
- `BigInt` -> `Rational`
- `Number` -> `Irrational`
- `Rational` -> `Irrational`
For example, when using the `div` method, dividing two `BigInt` values would result in loss of precision... therefore, before the division operation `BigInt` values are converted to `Rational` values. Similarly, `Number` values are converted to `Irrational` values. If the division operation includes any `Irrational` values, `Rational` values are converted to `Irrational` values.
See this examples:
```ts
import * as RealMath from '@hypercubed/real';
RealMath.div(1n, 3n),toString(); // 1/3`
RealMath.div(1n, 3),toString(); // 3.3333333333333333333e-1
RealMath.div(1n, new Rational(3n)),toString(); // 1/3`
RealMath.div(1n, new Irrational(3n)),toString(); // 3.3333333333333333333e-1
```
### Methods
- `abs` - Returns the absolute value of a number.
- `trunc` - Returns the integer part of the number x, removing any fractional digits.
- `ceil` - Returns the smallest integer greater than or equal to a number.
- `floor` - Returns the largest integer less than or equal to a number.
- `add` - adds two values
- `sub` - subtracts two values
- `mul` - multiplies two values
- `div` - divies two values
- `inv` - inverts a value
- `ln` - Returns the natural logarithm (loge) of a number
## License
This project is licensed under the MIT License