Skip to content

Commit

Permalink
Initial IEEEFloat implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
richard-viney committed Jun 13, 2024
0 parents commit 7f333f1
Show file tree
Hide file tree
Showing 10 changed files with 1,658 additions and 0 deletions.
38 changes: 38 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Test

on:
push:
branches:
- main
pull_request:

jobs:
test:
runs-on: ubuntu-24.04
steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Setup BEAM
uses: erlef/setup-beam@v1
with:
otp-version: "27.0.0"
gleam-version: "1.2.1"
rebar3-version: "3"

- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 22

- name: Install dependencies
run: gleam deps download

- name: Run tests on Erlang target
run: gleam test

- name: Run tests on JavaScript target
run: gleam test --target javascript

- name: Check code formatting
run: gleam format --check src test
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
*.beam
*.ez
/build
erl_crash.dump
.DS_Store
7 changes: 7 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"editor.rulers": [80],

"files.exclude": {
"**/build": true,
}
}
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Richard Viney

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.
86 changes: 86 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# Gleam IEEE Float

This Gleam library provides an `IEEEFloat` type that is compliant with the IEEE
754 standard for floating point arithmetic.

[![Package Version](https://img.shields.io/hexpm/v/ieee_float)](https://hex.pm/packages/ieee_float)
[![Hex Docs](https://img.shields.io/badge/hex-docs-ffaff3)](https://hexdocs.pm/ieee_float/)
![Erlang Compatible](https://img.shields.io/badge/target-erlang-a90432)
![JavaScript Compatible](https://img.shields.io/badge/target-javascript-f3e155)

## Background

Erlang's native float data type does not support infinity and NaN values, and
this library fills that gap when these values need to be able to be represented
and worked with.

On the JavaScript target, an `IEEEFloat` is simply a `number` because JavaScript
natively implements the IEEE 754 standard.

## Usage

Add this library to your project:

```sh
gleam add ieee_float
```

API documentation can be found at <https://hexdocs.pm/ieee_float/>.

The following code demonstrates commonly used functionality of this library.

```gleam
import ieee_float.{finite}
pub fn main() {
// Create finite values
let zero = finite(0.0)
let one = finite(1.0)
let two = finite(2.0)
let three = finite(3.0)
// Create infinity and NaN values
let positive_inf = ieee_float.positive_infinity()
let negative_inf = ieee_float.negative_infinity()
let nan = ieee_float.nan()
// Check whether a value is finite or NaN
let assert False = ieee_float.is_finite(positive_inf)
let assert True = ieee_float.is_nan(nan)
// Convert to a finite value of type `Float`. If the IEEE float is not finite
// then the fallback value is returned.
let assert 1.0 = ieee_float.to_finite(one, 5.0)
let assert 5.0 = ieee_float.to_finite(positive_inf, 5.0)
// Convert a value to raw bytes
let assert <<0x3F, 0x80, 0x00, 0x00>> = ieee_float.to_bytes_32_be(one)
// Create a value from raw bytes
let assert True =
ieee_float.from_bytes_32_be(<<0x3F, 0x80, 0x00, 0x00>>) == one
// Perform math operations
let assert True = ieee_float.add(two, three) == finite(5.0)
let assert True = ieee_float.divide(one, two) == finite(0.5)
let assert True = ieee_float.multiply(two, three) == finite(6.0)
let assert True = ieee_float.subtract(three, one) == finite(2.0)
// Perform math operations not supported by Erlang floats
let assert True = ieee_float.add(one, positive_inf) == positive_inf
let assert True =
ieee_float.add(positive_inf, negative_inf) |> ieee_float.is_nan
let assert True = ieee_float.multiply(positive_inf, two) == positive_inf
let assert True =
ieee_float.multiply(negative_inf, positive_inf) == negative_inf
let assert True = ieee_float.divide(two, zero) == positive_inf
let assert True =
ieee_float.divide(positive_inf, positive_inf) |> ieee_float.is_nan
}
```

## License

This library is published under the MIT license, a copy of which is included.
15 changes: 15 additions & 0 deletions gleam.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name = "ieee_float"
version = "0.1.0"
description = "Gleam library that provides an IEEE 754 compliant float type."
licences = ["MIT"]
repository = { type = "github", user = "richard-viney", repo = "ieee_float" }
links = [
{ title = "Website", href = "https://github.com/richard-viney/ieee_float" },
]

[dependencies]
gleam_stdlib = "~> 0.34 or ~> 1.0"
gleam_erlang = ">= 0.25.0 and < 1.0.0"

[dev-dependencies]
startest = ">= 0.2.4 and < 1.0.0"
29 changes: 29 additions & 0 deletions manifest.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# This file was generated by Gleam
# You typically do not need to edit this file

packages = [
{ name = "argv", version = "1.0.2", build_tools = ["gleam"], requirements = [], otp_app = "argv", source = "hex", outer_checksum = "BA1FF0929525DEBA1CE67256E5ADF77A7CDDFE729E3E3F57A5BDCAA031DED09D" },
{ name = "bigben", version = "1.0.0", build_tools = ["gleam"], requirements = ["birl", "gleam_erlang", "gleam_otp", "gleam_stdlib"], otp_app = "bigben", source = "hex", outer_checksum = "8E5A98FA6E981EEEF016C40F1CDFADA095927CAF6CAAA0C7E295EED02FC95947" },
{ name = "birl", version = "1.7.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "ranger"], otp_app = "birl", source = "hex", outer_checksum = "B1FA529E7BE3FF12CADF32814AB8EC7294E74CEDEE8CC734505707B929A98985" },
{ name = "exception", version = "2.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "exception", source = "hex", outer_checksum = "F5580D584F16A20B7FCDCABF9E9BE9A2C1F6AC4F9176FA6DD0B63E3B20D450AA" },
{ name = "filepath", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "EFB6FF65C98B2A16378ABC3EE2B14124168C0CE5201553DE652E2644DCFDB594" },
{ name = "gleam_community_ansi", version = "1.4.0", build_tools = ["gleam"], requirements = ["gleam_community_colour", "gleam_stdlib"], otp_app = "gleam_community_ansi", source = "hex", outer_checksum = "FE79E08BF97009729259B6357EC058315B6FBB916FAD1C2FF9355115FEB0D3A4" },
{ name = "gleam_community_colour", version = "1.4.0", build_tools = ["gleam"], requirements = ["gleam_json", "gleam_stdlib"], otp_app = "gleam_community_colour", source = "hex", outer_checksum = "795964217EBEDB3DA656F5EB8F67D7AD22872EB95182042D3E7AFEF32D3FD2FE" },
{ name = "gleam_erlang", version = "0.25.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "054D571A7092D2A9727B3E5D183B7507DAB0DA41556EC9133606F09C15497373" },
{ name = "gleam_javascript", version = "0.8.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_javascript", source = "hex", outer_checksum = "14D5B7E1A70681E0776BF0A0357F575B822167960C844D3D3FA114D3A75F05A8" },
{ name = "gleam_json", version = "1.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib", "thoas"], otp_app = "gleam_json", source = "hex", outer_checksum = "9063D14D25406326C0255BDA0021541E797D8A7A12573D849462CAFED459F6EB" },
{ name = "gleam_otp", version = "0.10.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "0B04FE915ACECE539B317F9652CAADBBC0F000184D586AAAF2D94C100945D72B" },
{ name = "gleam_stdlib", version = "0.38.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "663CF11861179AF415A625307447775C09404E752FF99A24E2057C835319F1BE" },
{ name = "glint", version = "1.0.0-rc2", build_tools = ["gleam"], requirements = ["gleam_community_ansi", "gleam_community_colour", "gleam_stdlib", "snag"], otp_app = "glint", source = "hex", outer_checksum = "FD5C47CE237CA67121F3946ADE7C630750BB67F5E8A4717D2DF5B5EE758CCFDB" },
{ name = "ranger", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "ranger", source = "hex", outer_checksum = "1566C272B1D141B3BBA38B25CB761EF56E312E79EC0E2DFD4D3C19FB0CC1F98C" },
{ name = "simplifile", version = "1.7.0", build_tools = ["gleam"], requirements = ["filepath", "gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "1D5DFA3A2F9319EC85825F6ED88B8E449F381B0D55A62F5E61424E748E7DDEB0" },
{ name = "snag", version = "0.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "snag", source = "hex", outer_checksum = "54D32E16E33655346AA3E66CBA7E191DE0A8793D2C05284E3EFB90AD2CE92BCC" },
{ name = "startest", version = "0.2.4", build_tools = ["gleam"], requirements = ["argv", "bigben", "birl", "exception", "gleam_community_ansi", "gleam_erlang", "gleam_javascript", "gleam_stdlib", "glint", "simplifile", "tom"], otp_app = "startest", source = "hex", outer_checksum = "1734A986F2920DC69BF4639C10E5BEFEBA5A968D3F035C8A473A63D1F84E65BD" },
{ name = "thoas", version = "1.2.1", build_tools = ["rebar3"], requirements = [], otp_app = "thoas", source = "hex", outer_checksum = "E38697EDFFD6E91BD12CEA41B155115282630075C2A727E7A6B2947F5408B86A" },
{ name = "tom", version = "0.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "tom", source = "hex", outer_checksum = "0831C73E45405A2153091226BF98FB485ED16376988602CC01A5FD086B82D577" },
]

[requirements]
gleam_erlang = { version = ">= 0.25.0 and < 1.0.0"}
gleam_stdlib = { version = "~> 0.34 or ~> 1.0" }
startest = { version = ">= 0.2.4 and < 1.0.0" }
Loading

0 comments on commit 7f333f1

Please sign in to comment.