|
| 1 | +## Absolute value |
| 2 | +{.push raises: [].} |
| 3 | +import std/strutils |
| 4 | + |
| 5 | +runnableExamples: |
| 6 | + assert absVal(-5.1) == 5.1 |
| 7 | + assert absMin(@[-1, 2, -3]) == 1 |
| 8 | + assert absMax(@[-1, 2, -3]) == 3 |
| 9 | + assert signedMinAbs(@[3, -10, -2]) == -2 |
| 10 | + assert signedMaxAbs(@[3, -10, -2]) == -10 |
| 11 | + |
| 12 | +func absVal*[T: SomeFloat](num: T): T = |
| 13 | + ## Returns the absolute value of a number. |
| 14 | + ## Use `math.abs <https://nim-lang.org/docs/system.html#abs%2CT>`_ instead! |
| 15 | + return if num < 0.0: -num else: num |
| 16 | + |
| 17 | +# Same for Integers but returns a Natural |
| 18 | +func absVal*[T: SomeInteger](num: T): Natural = (if num < 0: -num else: num) |
| 19 | + |
| 20 | +func absMin*(x: openArray[int]): Natural {.raises: [ValueError].} = |
| 21 | + ## Returns the smallest element in absolute value in a sequence. |
| 22 | + if x.len == 0: |
| 23 | + raise newException(ValueError, """Cannot find absolute minimum |
| 24 | + of an empty sequence""".unindent) |
| 25 | + result = absVal(x[0]) |
| 26 | + for i in 1 ..< x.len: |
| 27 | + if absVal(x[i]) < result: |
| 28 | + result = absVal(x[i]) |
| 29 | + |
| 30 | +func absMax*(x: openArray[int]): Natural {.raises: [ValueError].} = |
| 31 | + ## Returns the largest element in absolute value in a sequence. |
| 32 | + if x.len == 0: |
| 33 | + raise newException(ValueError, """Cannot find absolute maximum of an empty |
| 34 | + sequence""".unindent) |
| 35 | + result = absVal(x[0]) |
| 36 | + for i in 1 ..< x.len: |
| 37 | + if absVal(x[i]) > result: |
| 38 | + result = absVal(x[i]) |
| 39 | + |
| 40 | +func signedMinAbs*(x: openArray[int]): int {.raises: [ValueError].} = |
| 41 | + ## Returns the first signed element whose absolute value |
| 42 | + ## is the smallest in a sequence. |
| 43 | + if x.len == 0: |
| 44 | + raise newException(ValueError, """Cannot find absolute maximum of an empty |
| 45 | + sequence""".unindent) |
| 46 | + var (min, minAbs) = (x[0], absVal(x[0])) |
| 47 | + for n in x: |
| 48 | + let nAbs = absVal(n) |
| 49 | + if nAbs < minAbs: (min, minAbs) = (n, nAbs) |
| 50 | + min |
| 51 | + |
| 52 | +func signedMaxAbs*(x: openArray[int]): int {.raises: [ValueError].} = |
| 53 | + ## Returns the first signed element whose absolute value |
| 54 | + ## is the largest in a sequence. |
| 55 | + if x.len == 0: |
| 56 | + raise newException(ValueError, """Cannot find absolute maximum of an empty |
| 57 | + sequence""".unindent) |
| 58 | + var (max, maxAbs) = (x[0], absVal(x[0])) |
| 59 | + for n in x: |
| 60 | + let nAbs = absVal(n) |
| 61 | + if nAbs > maxAbs: (max, maxAbs) = (n, nAbs) |
| 62 | + max |
| 63 | + |
| 64 | +when isMainModule: |
| 65 | + import std/[unittest, random] |
| 66 | + randomize() |
| 67 | + |
| 68 | + suite "Check absVal": |
| 69 | + test "Check absVal": |
| 70 | + check: |
| 71 | + absVal(11.2) == 11.2 |
| 72 | + absVal(5) == 5 |
| 73 | + absVal(-5.1) == 5.1 |
| 74 | + absVal(-5) == absVal(5) |
| 75 | + absVal(0) == 0 |
| 76 | + |
| 77 | + suite "Check absMin": |
| 78 | + test "Check absMin": |
| 79 | + check: |
| 80 | + absMin(@[-1, 2, -3]) == 1 |
| 81 | + absMin(@[0, 5, 1, 11]) == 0 |
| 82 | + absMin(@[3, -10, -2]) == 2 |
| 83 | + absMin([-1, 2, -3]) == 1 |
| 84 | + absMin([0, 5, 1, 11]) == 0 |
| 85 | + absMin([3, -10, -2]) == 2 |
| 86 | + |
| 87 | + test "absMin on empty sequence raises ValueError": |
| 88 | + doAssertRaises(ValueError): |
| 89 | + discard absMin(@[]) |
| 90 | + |
| 91 | + suite "Check absMax": |
| 92 | + test "Check absMax": |
| 93 | + check: |
| 94 | + absMax(@[0, 5, 1, 11]) == 11 |
| 95 | + absMax(@[3, -10, -2]) == 10 |
| 96 | + absMax(@[-1, 2, -3]) == 3 |
| 97 | + |
| 98 | + test "`absMax` on empty sequence raises ValueError": |
| 99 | + doAssertRaises(ValueError): |
| 100 | + discard absMax(@[]) |
| 101 | + |
| 102 | + suite "Check signedMinAbs": |
| 103 | + test "Check signedMinAbs": |
| 104 | + check: |
| 105 | + signedMinAbs(@[0, 5, 1, 11]) == 0 |
| 106 | + signedMinAbs(@[3, -2, 1, -4, 5, -6]) == 1 |
| 107 | + signedMinAbs(@[3, -2, -1, -4, 5, -6]) == -1 |
| 108 | + |
| 109 | + test "Among two minimal elements, the first one is returned": |
| 110 | + check signedMinAbs(@[3, -2, 1, -4, 5, -6, -1]) == 1 |
| 111 | + |
| 112 | + suite "Check signedMaxAbs": |
| 113 | + test "Check signedMaxAbs": |
| 114 | + check: |
| 115 | + signedMaxAbs(@[3, -2, 1, -4, 5, -6]) == -6 |
| 116 | + signedMaxAbs(@[0, 5, 1, 11]) == 11 |
| 117 | + |
| 118 | + test "signedMaxAbs on empty sequence raises ValueError": |
| 119 | + doAssertRaises(ValueError): |
| 120 | + discard signedMaxAbs(@[]) |
0 commit comments