Skip to content

Commit

Permalink
Add more fixedDecimal methods
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcelGarus committed Feb 23, 2023
1 parent ead65d0 commit 809462d
Showing 1 changed file with 86 additions and 18 deletions.
104 changes: 86 additions & 18 deletions packages/Core/fixedDecimal.candy
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,69 @@ is := int.is
fromInt a :=
needs (int.is a)
a | int.multiply decimalsPow

floorToInt a :=
needs (is a)
a | int.divideTruncating decimalsPow

isNonNegative a :=
needs (is a)
a | int.isNonNegative

add a b :=
needs (is a)
needs (is b)
a | int.add b
add summandA summandB :=
needs (is summandA)
needs (is summandB)
summandA | int.add summandB
subtract minuend subtrahend :=
needs (is minuend)
needs (is subtrahend)
minuend | int.subtract subtrahend
negate value :=
needs (is value)
value | int.negate
multiply factorA factorB :=
needs (is factorA)
needs (is factorB)
factorA | int.multiply factorB | int.divideTruncating decimalsPow
divide dividend divisor :=
needs (is dividend)
needs (is divisor)
needs (divisor | equals 0 | bool.not) "You can't divide by zero."
dividend | int.multiply decimalsPow | int.divideTruncating divisor

multiply a b :=
needs (is a)
needs (is b)
a | int.multiply b | int.divideTruncating decimalsPow
compareTo valueA valueB :=
needs (is valueA)
needs (is valueB)
result = valueA | int.compare valueB
check (equals result Equal | bool.implies (equals valueA valueB))
result
isLessThan valueA valueB :=
needs (is valueA)
needs (is valueB)
equals (compareTo valueA valueB) Less
isGreaterThan valueA valueB :=
needs (is valueA)
needs (is valueB)
equals (compareTo valueA valueB) Greater
isLessThanOrEqualTo valueA valueB :=
needs (is valueA)
needs (is valueB)
valueA | isGreaterThan valueB | bool.not
isGreaterThanOrEqualTo valueA valueB :=
needs (is valueA)
needs (is valueB)
valueA | isLessThan valueB | bool.not

divide a b :=
needs (is a)
needs (is b)
needs (b | equals 0 | bool.not) "You can't divide by zero."
a | int.multiply decimalsPow | int.divideTruncating b
isPositive value :=
needs (is value)
value | isGreaterThan 0
isNonPositive value :=
needs (is value)
value | isPositive | bool.not
isNegative value :=
needs (is value)
value | isLessThan 0
isNonNegative value :=
needs (is value)
value | isNegative | bool.not
absolute value :=
needs (is value)
ifElse (isNegative value) { negate value } { value }

approxEquals a b delta :=
needs (is a)
Expand All @@ -49,6 +88,35 @@ approxEquals a b delta :=
needs (isNonNegative delta)
a | int.subtract b | int.absolute | int.isLessThanOrEqualTo delta

min valueA valueB :=
needs (is valueA)
needs (is valueB)
ifElse
(valueA | isLessThanOrEqualTo valueB)
{ valueA }
{ valueB }
max valueA valueB :=
needs (is valueA)
needs (is valueB)
ifElse
(valueA | isGreaterThanOrEqualTo valueB)
{ valueA }
{ valueB }
coerceAtLeast value minimum :=
needs (is value)
needs (is minimum)
max value minimum
coerceAtMost value maximum :=
needs (is value)
needs (is maximum)
min value maximum
coerceIn value minimum maximum :=
needs (is value)
needs (is minimum)
needs (is maximum)
needs (minimum | isLessThanOrEqualTo maximum)
value | coerceAtLeast minimum | coerceAtMost maximum

toText a :=
needs (is a)
beforeDot = a | floorToInt | toDebugText
Expand Down

1 comment on commit 809462d

@jwbot
Copy link
Collaborator

@jwbot jwbot commented on 809462d Feb 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Compiler

Benchmark suite Current: 809462d Previous: 22fe978 Ratio
Time: Compiler/hello_world 31753030 ns/iter (± 1415762) 29031946 ns/iter (± 1363210) 1.09
Time: Compiler/fibonacci 792003091 ns/iter (± 6143678) 708862818 ns/iter (± 22941978) 1.12
Time: VM Runtime/hello_world 24358985 ns/iter (± 378849) 22144452 ns/iter (± 1511125) 1.10
Time: VM Runtime/fibonacci/15 243493448 ns/iter (± 1869288) 245913884 ns/iter (± 4986800) 0.99

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.