Skip to content

Commit

Permalink
Remove Result type from ieee_float.from_bytes_* functions
Browse files Browse the repository at this point in the history
  • Loading branch information
richard-viney committed Jun 13, 2024
1 parent 7b54575 commit 4b454e9
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 36 deletions.
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ The following code demonstrates some of the commonly used functionality of this
library.

```gleam
import gleam/result
import ieee_float.{finite}
pub fn main() {
Expand Down Expand Up @@ -59,9 +58,8 @@ pub fn main() {
let assert <<0x3F, 0x80, 0x00, 0x00>> = ieee_float.to_bytes_32_be(one)
// Create a value from raw bytes
let assert Ok(1.0) =
ieee_float.from_bytes_32_be(<<0x3F, 0x80, 0x00, 0x00>>)
|> result.map(ieee_float.to_finite(_, 0.0))
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)
Expand Down
44 changes: 20 additions & 24 deletions src/ieee_float.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,12 @@ pub fn to_bytes_32_le(f: IEEEFloat) -> BitArray {
/// `IEEEFloat`.
///
@external(javascript, "./ieee_float_js.mjs", "from_bytes_32_le")
pub fn from_bytes_32_le(bytes: BitArray) -> Result(IEEEFloat, Nil) {
pub fn from_bytes_32_le(bytes: BitArray) -> IEEEFloat {
case bytes {
<<value:32-float-little>> -> Ok(Finite(value))
<<0x7F800000:32-little>> -> Ok(Infinite(Positive))
<<0xFF800000:32-little>> -> Ok(Infinite(Negative))
<<_:16, 1:1, _:8, 0x7F:7>> -> Ok(NaN)
_ -> Error(Nil)
<<value:32-float-little>> -> Finite(value)
<<0x7F800000:32-little>> -> Infinite(Positive)
<<0xFF800000:32-little>> -> Infinite(Negative)
_ -> NaN
}
}

Expand All @@ -153,13 +152,12 @@ pub fn to_bytes_32_be(f: IEEEFloat) -> BitArray {
/// `IEEEFloat`.
///
@external(javascript, "./ieee_float_js.mjs", "from_bytes_32_be")
pub fn from_bytes_32_be(bytes: BitArray) -> Result(IEEEFloat, Nil) {
pub fn from_bytes_32_be(bytes: BitArray) -> IEEEFloat {
case bytes {
<<value:32-float-big>> -> Ok(Finite(value))
<<0x7F800000:32-big>> -> Ok(Infinite(Positive))
<<0xFF800000:32-big>> -> Ok(Infinite(Negative))
<<_:1, 0xFF:8, _:23>> -> Ok(NaN)
_ -> Error(Nil)
<<value:32-float-big>> -> Finite(value)
<<0x7F800000:32-big>> -> Infinite(Positive)
<<0xFF800000:32-big>> -> Infinite(Negative)
_ -> NaN
}
}

Expand All @@ -180,13 +178,12 @@ pub fn to_bytes_64_le(f: IEEEFloat) -> BitArray {
/// `IEEEFloat`.
///
@external(javascript, "./ieee_float_js.mjs", "from_bytes_64_le")
pub fn from_bytes_64_le(bytes: BitArray) -> Result(IEEEFloat, Nil) {
pub fn from_bytes_64_le(bytes: BitArray) -> IEEEFloat {
case bytes {
<<value:64-float-little>> -> Ok(Finite(value))
<<0x7FF0000000000000:64-little>> -> Ok(Infinite(Positive))
<<0xFFF0000000000000:64-little>> -> Ok(Infinite(Negative))
<<_:48, 0xF:4, _:4, 0x7F>> -> Ok(NaN)
_ -> Error(Nil)
<<value:64-float-little>> -> Finite(value)
<<0x7FF0000000000000:64-little>> -> Infinite(Positive)
<<0xFFF0000000000000:64-little>> -> Infinite(Negative)
_ -> NaN
}
}

Expand All @@ -207,13 +204,12 @@ pub fn to_bytes_64_be(f: IEEEFloat) -> BitArray {
/// `IEEEFloat`.
///
@external(javascript, "./ieee_float_js.mjs", "from_bytes_64_be")
pub fn from_bytes_64_be(bytes: BitArray) -> Result(IEEEFloat, Nil) {
pub fn from_bytes_64_be(bytes: BitArray) -> IEEEFloat {
case bytes {
<<value:64-float-big>> -> Ok(Finite(value))
<<0x7FF0000000000000:64-big>> -> Ok(Infinite(Positive))
<<0xFFF0000000000000:64-big>> -> Ok(Infinite(Negative))
<<0x7FF:12, _:52>> -> Ok(NaN)
_ -> Error(Nil)
<<value:64-float-big>> -> Finite(value)
<<0x7FF0000000000000:64-big>> -> Infinite(Positive)
<<0xFFF0000000000000:64-big>> -> Infinite(Negative)
_ -> NaN
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/ieee_float_js.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export function from_bytes_32_le(f) {

const view = new DataView(f.buffer.buffer);

return new Ok(view.getFloat32(0, true));
return view.getFloat32(0, true);
}

export function to_bytes_32_be(f) {
Expand All @@ -97,7 +97,7 @@ export function from_bytes_32_be(f) {

const view = new DataView(f.buffer.buffer);

return new Ok(view.getFloat32(0, false));
return view.getFloat32(0, false);
}

export function to_bytes_64_le(f) {
Expand All @@ -116,7 +116,7 @@ export function from_bytes_64_le(f) {

const view = new DataView(f.buffer.buffer);

return new Ok(view.getFloat64(0, true));
return view.getFloat64(0, true);
}

export function to_bytes_64_be(f) {
Expand All @@ -135,7 +135,7 @@ export function from_bytes_64_be(f) {

const view = new DataView(f.buffer.buffer);

return new Ok(view.getFloat64(0, false));
return view.getFloat64(0, false);
}

export function absolute_value(f) {
Expand Down
8 changes: 4 additions & 4 deletions test/ieee_float_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -193,19 +193,19 @@ pub fn fp64_bytes_serde_test() {
fn text_ieee_bytes_serde(
bytes: List(Int),
expected_value: IEEEFloat,
from_bytes: fn(BitArray) -> Result(IEEEFloat, Nil),
from_bytes: fn(BitArray) -> IEEEFloat,
to_bytes: fn(IEEEFloat) -> BitArray,
) {
let bits =
bytes
|> list.map(fn(x) { <<x>> })
|> bit_array.concat

let assert Ok(x) = from_bytes(bits)
let f = from_bytes(bits)

case ieee_float.is_nan(expected_value) {
True -> ieee_float.is_nan(x) |> expect.to_be_true
False -> x |> expect.to_equal(expected_value)
True -> ieee_float.is_nan(f) |> expect.to_be_true
False -> f |> expect.to_equal(expected_value)
}

expected_value
Expand Down

0 comments on commit 4b454e9

Please sign in to comment.