Initial commit
This commit is contained in:
commit
00b87715fa
10 changed files with 336 additions and 0 deletions
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
*.beam
|
||||||
|
*.ez
|
||||||
|
/build
|
||||||
|
erl_crash.dump
|
2
.tool-versions
Normal file
2
.tool-versions
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
gleam 0.34.1
|
||||||
|
erlang 26.1.2
|
7
LICENSE
Normal file
7
LICENSE
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
Copyright (c) Mikko Ahlroth, 2024.
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
25
README.md
Normal file
25
README.md
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
# bigi
|
||||||
|
|
||||||
|
[![Package Version](https://img.shields.io/hexpm/v/bigi)](https://hex.pm/packages/bigi)
|
||||||
|
[![Hex Docs](https://img.shields.io/badge/hex-docs-ffaff3)](https://hexdocs.pm/bigi/)
|
||||||
|
|
||||||
|
```sh
|
||||||
|
gleam add bigi
|
||||||
|
```
|
||||||
|
```gleam
|
||||||
|
import bigi
|
||||||
|
|
||||||
|
pub fn main() {
|
||||||
|
// TODO: An example of the project in use
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Further documentation can be found at <https://hexdocs.pm/bigi>.
|
||||||
|
|
||||||
|
## Development
|
||||||
|
|
||||||
|
```sh
|
||||||
|
gleam run # Run the project
|
||||||
|
gleam test # Run the tests
|
||||||
|
gleam shell # Run an Erlang shell
|
||||||
|
```
|
17
gleam.toml
Normal file
17
gleam.toml
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
name = "bigi"
|
||||||
|
version = "1.0.0"
|
||||||
|
|
||||||
|
# Fill out these fields if you intend to generate HTML documentation or publish
|
||||||
|
# your project to the Hex package manager.
|
||||||
|
#
|
||||||
|
# description = ""
|
||||||
|
# licences = ["Apache-2.0"]
|
||||||
|
# repository = { type = "github", user = "username", repo = "project" }
|
||||||
|
# links = [{ title = "Website", href = "https://gleam.run" }]
|
||||||
|
#
|
||||||
|
# For a full reference of all the available options, you can have a look at
|
||||||
|
# https://gleam.run/writing-gleam/gleam-toml/.
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
gleam_stdlib = "~> 0.34 or ~> 1.0"
|
||||||
|
gleeunit = "~> 1.0"
|
11
manifest.toml
Normal file
11
manifest.toml
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
# This file was generated by Gleam
|
||||||
|
# You typically do not need to edit this file
|
||||||
|
|
||||||
|
packages = [
|
||||||
|
{ name = "gleam_stdlib", version = "0.34.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "1FB8454D2991E9B4C0C804544D8A9AD0F6184725E20D63C3155F0AEB4230B016" },
|
||||||
|
{ name = "gleeunit", version = "1.0.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "D364C87AFEB26BDB4FB8A5ABDE67D635DC9FA52D6AB68416044C35B096C6882D" },
|
||||||
|
]
|
||||||
|
|
||||||
|
[requirements]
|
||||||
|
gleam_stdlib = { version = "~> 0.34 or ~> 1.0" }
|
||||||
|
gleeunit = { version = "~> 1.0" }
|
93
src/bigi.gleam
Normal file
93
src/bigi.gleam
Normal file
|
@ -0,0 +1,93 @@
|
||||||
|
pub opaque type Bigi {
|
||||||
|
Bigi(data: BigInt)
|
||||||
|
}
|
||||||
|
|
||||||
|
type BigInt
|
||||||
|
|
||||||
|
pub fn from_int(int: Int) {
|
||||||
|
Bigi(bigint_from(int))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn to_int(bigi: Bigi) {
|
||||||
|
bigint_to(bigi.data)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn to_string(bigi: Bigi) {
|
||||||
|
bigint_to_string(bigi.data)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn zero() {
|
||||||
|
Bigi(bigint_zero())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn add(a: Bigi, b: Bigi) {
|
||||||
|
Bigi(bigint_add(a.data, b.data))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn subtract(a: Bigi, b: Bigi) {
|
||||||
|
Bigi(bigint_subtract(a.data, b.data))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn multiply(a: Bigi, b: Bigi) {
|
||||||
|
Bigi(bigint_multiply(a.data, b.data))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn divide(a: Bigi, b: Bigi) {
|
||||||
|
Bigi(bigint_divide(a.data, b.data))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn remainder(a: Bigi, b: Bigi) {
|
||||||
|
Bigi(bigint_remainder(a.data, b.data))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn modulo(a: Bigi, b: Bigi) {
|
||||||
|
Bigi(bigint_modulo(a.data, b.data))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn power(a: Bigi, b: Bigi) {
|
||||||
|
Bigi(bigint_power(a.data, b.data))
|
||||||
|
}
|
||||||
|
|
||||||
|
@external(erlang, "bigi_ffi", "from")
|
||||||
|
@external(javascript, "./bigi_ffi.mjs", "from")
|
||||||
|
fn bigint_from(int: Int) -> BigInt
|
||||||
|
|
||||||
|
@external(erlang, "bigi_ffi", "to")
|
||||||
|
@external(javascript, "./bigi_ffi.mjs", "to")
|
||||||
|
fn bigint_to(bigint: BigInt) -> Result(Int, Nil)
|
||||||
|
|
||||||
|
@external(erlang, "bigi_ffi", "to_string")
|
||||||
|
@external(javascript, "./bigi_ffi.mjs", "to_string")
|
||||||
|
fn bigint_to_string(bigint: BigInt) -> String
|
||||||
|
|
||||||
|
@external(erlang, "bigi_ffi", "zero")
|
||||||
|
@external(javascript, "./bigi_ffi.mjs", "zero")
|
||||||
|
fn bigint_zero() -> BigInt
|
||||||
|
|
||||||
|
@external(erlang, "bigi_ffi", "add")
|
||||||
|
@external(javascript, "./bigi_ffi.mjs", "add")
|
||||||
|
fn bigint_add(a: BigInt, b: BigInt) -> BigInt
|
||||||
|
|
||||||
|
@external(erlang, "bigi_ffi", "subtract")
|
||||||
|
@external(javascript, "./bigi_ffi.mjs", "subtract")
|
||||||
|
fn bigint_subtract(a: BigInt, b: BigInt) -> BigInt
|
||||||
|
|
||||||
|
@external(erlang, "bigi_ffi", "multiply")
|
||||||
|
@external(javascript, "./bigi_ffi.mjs", "multiply")
|
||||||
|
fn bigint_multiply(a: BigInt, b: BigInt) -> BigInt
|
||||||
|
|
||||||
|
@external(erlang, "bigi_ffi", "divide")
|
||||||
|
@external(javascript, "./bigi_ffi.mjs", "divide")
|
||||||
|
fn bigint_divide(a: BigInt, b: BigInt) -> BigInt
|
||||||
|
|
||||||
|
@external(erlang, "bigi_ffi", "remainder")
|
||||||
|
@external(javascript, "./bigi_ffi.mjs", "remainder")
|
||||||
|
fn bigint_remainder(a: BigInt, b: BigInt) -> BigInt
|
||||||
|
|
||||||
|
@external(erlang, "bigi_ffi", "modulo")
|
||||||
|
@external(javascript, "./bigi_ffi.mjs", "modulo")
|
||||||
|
fn bigint_modulo(a: BigInt, b: BigInt) -> BigInt
|
||||||
|
|
||||||
|
@external(erlang, "bigi_ffi", "power")
|
||||||
|
@external(javascript, "./bigi_ffi.mjs", "power")
|
||||||
|
fn bigint_power(a: BigInt, b: BigInt) -> BigInt
|
47
src/bigi_ffi.erl
Normal file
47
src/bigi_ffi.erl
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
-module(bigi_ffi).
|
||||||
|
|
||||||
|
-export([
|
||||||
|
from/1,
|
||||||
|
to/1,
|
||||||
|
to_string/1,
|
||||||
|
zero/0,
|
||||||
|
add/2,
|
||||||
|
subtract/2,
|
||||||
|
multiply/2,
|
||||||
|
divide/2,
|
||||||
|
remainder/2,
|
||||||
|
modulo/2,
|
||||||
|
power/2
|
||||||
|
]).
|
||||||
|
|
||||||
|
from(Int) -> Int.
|
||||||
|
|
||||||
|
to(BigInt) -> {ok, BigInt}.
|
||||||
|
|
||||||
|
to_string(BigInt) -> erlang:integer_to_binary(BigInt).
|
||||||
|
|
||||||
|
zero() -> 0.
|
||||||
|
|
||||||
|
add(A, B) -> A + B.
|
||||||
|
|
||||||
|
subtract(A, B) -> A - B.
|
||||||
|
|
||||||
|
multiply(A, B) -> A * B.
|
||||||
|
|
||||||
|
divide(A, B) -> A div B.
|
||||||
|
|
||||||
|
remainder(A, B) -> A rem B.
|
||||||
|
|
||||||
|
modulo(A, B) -> ((A rem B) + B) rem B.
|
||||||
|
|
||||||
|
power(_, 0) ->
|
||||||
|
1;
|
||||||
|
power(A, 1) ->
|
||||||
|
A;
|
||||||
|
power(A, N) ->
|
||||||
|
B = power(A, N div 2),
|
||||||
|
B * B *
|
||||||
|
(case N rem 2 of
|
||||||
|
0 -> 1;
|
||||||
|
1 -> A
|
||||||
|
end).
|
49
src/bigi_ffi.mjs
Normal file
49
src/bigi_ffi.mjs
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
import { Ok, Error } from "./gleam.mjs";
|
||||||
|
|
||||||
|
export function from(int) {
|
||||||
|
return BigInt(int);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function to(bigint) {
|
||||||
|
if (bigint > Number.MAX_SAFE_INTEGER || bigint < Number.MIN_SAFE_INTEGER) {
|
||||||
|
return new Error(undefined);
|
||||||
|
} else {
|
||||||
|
return new Ok(Number(bigint));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function to_string(bigint) {
|
||||||
|
return bigint.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
export function zero() {
|
||||||
|
return 0n;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function add(a, b) {
|
||||||
|
return a + b;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function subtract(a, b) {
|
||||||
|
return a - b;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function multiply(a, b) {
|
||||||
|
return a * b;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function divide(a, b) {
|
||||||
|
return a / b;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function remainder(a, b) {
|
||||||
|
return a % b;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function modulo(a, b) {
|
||||||
|
return ((a % b) + b) % b;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function power(a, b) {
|
||||||
|
return a ** b;
|
||||||
|
}
|
81
test/bigi_test.gleam
Normal file
81
test/bigi_test.gleam
Normal file
File diff suppressed because one or more lines are too long
Loading…
Reference in a new issue