Skip to content

Commit 2c3fd88

Browse files
committed
feat(math): adding math:negate
1 parent d34d3fc commit 2c3fd88

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

Math.ark

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,11 @@
216216
# @author https://github.com/SuperFola
217217
(let decrement (fun (_x) (- _x 1)))
218218

219+
# @brief Multiply a number by -1, turning positive numbers negative and negative numbers positive
220+
# #param _x number
221+
# @author https://github.com/SuperFola
222+
(let negate (fun (_x) (* -1 _x)))
223+
219224
# @brief Get a number to a given power
220225
# @details Note that it's defined as exp(a * ln(x)), thus won't work for negative numbers
221226
# @param _x the number to pow

tests/math-tests.ark

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
(test:eq (math:abs -1) 1)
3636
(test:eq (math:abs 0) 0)
3737
(test:eq (math:abs math:Inf) math:Inf)
38+
(test:eq (math:abs (math:negate math:Inf)) math:Inf)
3839
(test:eq (math:abs 1) 1) })
3940

4041
(test:case "even?" {
@@ -71,6 +72,12 @@
7172
(test:eq (math:decrement 1) 0)
7273
(test:eq (math:decrement 1.5) 0.5) })
7374

75+
(test:case "negate" {
76+
(test:eq (math:negate 0) 0)
77+
(test:eq (math:negate 1) -1)
78+
(test:eq (math:negate -1) 1)
79+
(test:eq (math:negate (math:negate 1)) 1) })
80+
7481
(test:case "pow" {
7582
(test:eq (math:pow 2 2) 4)
7683
(test:eq (math:pow 4 0.5) 2) })

0 commit comments

Comments
 (0)