Skip to content

Commit

Permalink
Fix rounding functions and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
NicklasXYZ committed Aug 12, 2024
1 parent 42c4ae1 commit 7ecaad5
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 134 deletions.
42 changes: 12 additions & 30 deletions src/gleam_community/maths/piecewise.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ import gleam_community/maths/elementary
/// </a>
/// </div>
///
pub fn ceiling(x: Float, digits: option.Option(Int)) -> Result(Float, String) {
pub fn ceiling(x: Float, digits: option.Option(Int)) -> Float {
round(x, digits, option.Some(RoundUp))
}

Expand Down Expand Up @@ -167,7 +167,7 @@ pub fn ceiling(x: Float, digits: option.Option(Int)) -> Result(Float, String) {
/// </a>
/// </div>
///
pub fn floor(x: Float, digits: option.Option(Int)) -> Result(Float, String) {
pub fn floor(x: Float, digits: option.Option(Int)) -> Float {
round(x, digits, option.Some(RoundDown))
}

Expand Down Expand Up @@ -222,7 +222,7 @@ pub fn floor(x: Float, digits: option.Option(Int)) -> Result(Float, String) {
/// </a>
/// </div>
///
pub fn truncate(x: Float, digits: option.Option(Int)) -> Result(Float, String) {
pub fn truncate(x: Float, digits: option.Option(Int)) -> Float {
round(x, digits, option.Some(RoundToZero))
}

Expand Down Expand Up @@ -360,7 +360,7 @@ pub fn round(
x: Float,
digits: option.Option(Int),
mode: option.Option(RoundingMode),
) -> Result(Float, String) {
) -> Float {
case digits {
option.Some(a) -> {
let assert Ok(p) = elementary.power(10.0, conversion.int_to_float(a))
Expand All @@ -381,35 +381,17 @@ pub type RoundingMode {
RoundUp
}

fn do_round(
p: Float,
x: Float,
mode: option.Option(RoundingMode),
) -> Result(Float, String) {
fn do_round(p: Float, x: Float, mode: option.Option(RoundingMode)) -> Float {
case mode {
// Determine the rounding mode
option.Some(RoundNearest) ->
round_to_nearest(p, x)
|> Ok
option.Some(RoundTiesAway) ->
round_ties_away(p, x)
|> Ok
option.Some(RoundTiesUp) ->
round_ties_up(p, x)
|> Ok
option.Some(RoundToZero) ->
round_to_zero(p, x)
|> Ok
option.Some(RoundDown) ->
round_down(p, x)
|> Ok
option.Some(RoundUp) ->
round_up(p, x)
|> Ok
option.Some(RoundNearest) -> round_to_nearest(p, x)
option.Some(RoundTiesAway) -> round_ties_away(p, x)
option.Some(RoundTiesUp) -> round_ties_up(p, x)
option.Some(RoundToZero) -> round_to_zero(p, x)
option.Some(RoundDown) -> round_down(p, x)
option.Some(RoundUp) -> round_up(p, x)
// Otherwise, use the default rounding mode
option.None ->
round_to_nearest(p, x)
|> Ok
option.None -> round_to_nearest(p, x)
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/gleam_community/maths/predicates.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -247,9 +247,9 @@ fn do_ceiling(a: Float) -> Float
pub fn is_power(x: Int, y: Int) -> Bool {
let assert Ok(value) =
elementary.logarithm(int.to_float(x), option.Some(int.to_float(y)))
let assert Ok(truncated) = piecewise.truncate(value, option.Some(0))
let rem = value -. truncated
rem == 0.0
let truncated = piecewise.truncate(value, option.Some(0))
let remainder = value -. truncated
remainder == 0.0
}

/// <div style="text-align: right;">
Expand Down
Loading

0 comments on commit 7ecaad5

Please sign in to comment.