https://github.com/gleam-community/maths
A basic mathematics library containing fundamental functions and utilities.
https://github.com/gleam-community/maths
gleam gleam-community gleam-lang mathematics maths
Last synced: about 2 months ago
JSON representation
A basic mathematics library containing fundamental functions and utilities.
- Host: GitHub
- URL: https://github.com/gleam-community/maths
- Owner: gleam-community
- License: apache-2.0
- Created: 2022-12-21T18:27:56.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2025-04-03T22:47:37.000Z (3 months ago)
- Last Synced: 2025-05-02T23:33:56.662Z (about 2 months ago)
- Topics: gleam, gleam-community, gleam-lang, mathematics, maths
- Language: Gleam
- Homepage:
- Size: 312 KB
- Stars: 37
- Watchers: 3
- Forks: 10
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Code of conduct: CODE_OF_CONDUCT.md
- Codeowners: CODEOWNERS
Awesome Lists containing this project
- awesome-gleam - gleam_community_maths - [📚](https://hexdocs.pm/gleam_community_maths/) - A basic maths library (Packages / Numbers)
README
# gleam-community/maths
[](https://hex.pm/packages/gleam_community_maths)
[](https://hexdocs.pm/gleam_community_maths/)A basic mathematics library that contains some of the most fundamental mathematics functions and utilities.
The library supports both targets: Erlang and JavaScript.
## Quickstart
```gleam
import gleam/float
import gleam/list
import gleam/yielder
import gleam_community/maths
import gleeunit/shouldpub fn example() {
// Evaluate the sine function
let result = maths.sin(maths.pi())
// Set the relative and absolute tolerance
let assert Ok(absolute_tol) = float.power(10.0, -6.0)
let relative_tol = 0.0
// Check that the value is very close to 0.0
// That is, if 'result' is within +/- 10^(-6)
maths.is_close(result, 0.0, relative_tol, absolute_tol)
|> should.be_true()// Find the greatest common divisor
maths.gcd(54, 24)
|> should.equal(6)// Determine if 999983 is a prime number
maths.is_prime(999_983)
|> should.equal(True)// Find the minimum and maximum of a list
maths.extrema([10.0, 3.0, 50.0, 20.0, 3.0], float.compare)
|> should.equal(Ok(#(3.0, 50.0)))// Determine if a number is fractional
maths.is_fractional(0.3333)
|> should.equal(True)// Generate all k = 2 combinations of [1, 2, 3]
let assert Ok(combinations) = maths.list_combination([1, 2, 3], 2)
combinations
|> yielder.to_list()
|> should.equal([[1, 2], [1, 3], [2, 3]])// Compute the Cosine Similarity between two (orthogonal) "vectors"
maths.cosine_similarity([#(-1.0, 1.0), #(1.0, 1.0), #(0.0, -1.0)])
|> should.equal(Ok(0.0))// Generate a list of 3 logarithmically-spaced points over a specified
// interval, i.e., [10^1, 10^3]
let assert Ok(logspace) = maths.logarithmic_space(1.0, 3.0, 3, True, 10.0)
let pairs = logspace |> list.zip([10.0, 100.0, 1000.0])
pairs
|> list.all(fn(x) { x.0 == x.1 })
|> should.be_true()
}```
## Installation
`gleam_community` packages are published to [hex.pm](https://hex.pm/packages/gleam_community_maths)
with the prefix `gleam_community_`. You can add them to your Gleam projects directly:```sh
gleam add gleam_community_maths
```