Skip to content

Commit

Permalink
Merge pull request #24 from NicklasXYZ/main
Browse files Browse the repository at this point in the history
Rounding functions only return a Float
  • Loading branch information
NicklasXYZ authored Aug 12, 2024
2 parents 42c4ae1 + 3704614 commit 43e5eec
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 151 deletions.
76 changes: 29 additions & 47 deletions src/gleam_community/maths/piecewise.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,13 @@ import gleam_community/maths/elementary
///
/// pub fn example() {
/// piecewise.ceiling(12.0654, option.Some(1))
/// |> should.equal(Ok(12.1))
/// |> should.equal(12.1)
///
/// piecewise.ceiling(12.0654, option.Some(2))
/// |> should.equal(Ok(12.07))
/// |> should.equal(12.07)
///
/// piecewise.ceiling(12.0654, option.Some(3))
/// |> should.equal(Ok(12.066))
/// |> should.equal(12.066)
/// }
/// </details>
///
Expand All @@ -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 @@ -151,13 +151,13 @@ pub fn ceiling(x: Float, digits: option.Option(Int)) -> Result(Float, String) {
///
/// pub fn example() {
/// piecewise.floor(12.0654, option.Some(1))
/// |> should.equal(Ok(12.0))
/// |> should.equal(12.0)
///
/// piecewise.floor(12.0654, option.Some(2))
/// |> should.equal(Ok(12.06))
/// |> should.equal(12.06)
///
/// piecewise.floor(12.0654, option.Some(3))
/// |> should.equal(Ok(12.065))
/// |> should.equal(12.065)
/// }
/// </details>
///
Expand All @@ -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 @@ -206,13 +206,13 @@ pub fn floor(x: Float, digits: option.Option(Int)) -> Result(Float, String) {
///
/// pub fn example() {
/// piecewise.truncate(12.0654, option.Some(1))
/// |> should.equal(Ok(12.0))
/// |> should.equal(12.0)
///
/// piecewise.truncate(12.0654, option.Some(2))
/// |> should.equal(Ok(12.0))
/// |> should.equal(12.06)
///
/// piecewise.truncate(12.0654, option.Some(3))
/// |> should.equal(Ok(12.0))
/// |> should.equal(12.065)
/// }
/// </details>
///
Expand All @@ -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 @@ -323,30 +323,30 @@ pub fn truncate(x: Float, digits: option.Option(Int)) -> Result(Float, String) {
/// pub fn example() {
/// // The default number of digits is 0 if None is provided
/// piecewise.round(12.0654, option.None, option.Some(piecewise.RoundNearest))
/// |> should.equal(Ok(12.0))
/// |> should.equal(12.0)
///
/// // The default rounding mode is "RoundNearest" if None is provided
/// piecewise.round(12.0654, option.None, option.None)
/// |> should.equal(Ok(12.0))
/// |> should.equal(12.0)
///
/// // Try different rounding modes
/// piecewise.round(12.0654, option.Some(2), option.Some(piecewise.RoundNearest))
/// |> should.equal(Ok(12.07))
/// |> should.equal(12.07)
///
/// piecewise.round(12.0654, option.Some(2), option.Some(piecewise.RoundTiesAway))
/// |> should.equal(Ok(12.07))
/// |> should.equal(12.07)
///
/// piecewise.round(12.0654, option.Some(2), option.Some(piecewise.RoundTiesUp))
/// |> should.equal(Ok(12.07))
/// |> should.equal(12.07)
///
/// piecewise.round(12.0654, option.Some(2), option.Some(piecewise.RoundToZero))
/// |> should.equal(Ok(12.06))
/// |> should.equal(12.06)
///
/// piecewise.round(12.0654, option.Some(2), option.Some(piecewise.RoundDown))
/// |> should.equal(Ok(12.06))
/// |> should.equal(12.06)
///
/// piecewise.round(12.0654, option.Some(2), option.Some(piecewise.RoundUp))
/// |> should.equal(Ok(12.07))
/// |> should.equal(12.07)
/// }
/// </details>
///
Expand All @@ -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 43e5eec

Please sign in to comment.