[backport] run nimpretty on numbers stuff
This commit is contained in:
parent
aa513d78e7
commit
6c994b2498
6 changed files with 183 additions and 167 deletions
|
|
@ -228,7 +228,8 @@ proc smartBinarySearch*[T](a: openArray[T], key: T): int {.deprecated:
|
|||
const
|
||||
onlySafeCode = true
|
||||
|
||||
proc lowerBound*[T, K](a: openArray[T], key: K, cmp: proc(x: T, k: K): int {.closure.}): int =
|
||||
proc lowerBound*[T, K](a: openArray[T], key: K, cmp: proc(x: T, k: K): int {.
|
||||
closure.}): int =
|
||||
## Returns a position to the first element in the ``a`` that is greater than
|
||||
## ``key``, or last if no such element is found.
|
||||
## In other words if you have a sorted sequence and you call
|
||||
|
|
@ -275,7 +276,8 @@ proc lowerBound*[T](a: openArray[T], key: T): int = lowerBound(a, key, cmp[T])
|
|||
## * `upperBound proc<#upperBound,openArray[T],K,proc(T,K)>`_ sorted by ``cmp`` in the specified order
|
||||
## * `upperBound proc<#upperBound,openArray[T],T>`_
|
||||
|
||||
proc upperBound*[T, K](a: openArray[T], key: K, cmp: proc(x: T, k: K): int {.closure.}): int =
|
||||
proc upperBound*[T, K](a: openArray[T], key: K, cmp: proc(x: T, k: K): int {.
|
||||
closure.}): int =
|
||||
## Returns a position to the first element in the ``a`` that is not less
|
||||
## (i.e. greater or equal to) than ``key``, or last if no such element is found.
|
||||
## In other words if you have a sorted sequence and you call
|
||||
|
|
@ -422,7 +424,8 @@ func sort*[T](a: var openArray[T],
|
|||
dec(m, s*2)
|
||||
s = s*2
|
||||
|
||||
proc sort*[T](a: var openArray[T], order = SortOrder.Ascending) = sort[T](a, system.cmp[T], order)
|
||||
proc sort*[T](a: var openArray[T], order = SortOrder.Ascending) = sort[T](a,
|
||||
system.cmp[T], order)
|
||||
## Shortcut version of ``sort`` that uses ``system.cmp[T]`` as the comparison function.
|
||||
##
|
||||
## **See also:**
|
||||
|
|
@ -494,9 +497,11 @@ template sortedByIt*(seq1, op: untyped): untyped =
|
|||
p4: Person = (name: "p4", age: 30)
|
||||
people = @[p1, p2, p4, p3]
|
||||
|
||||
assert people.sortedByIt(it.name) == @[(name: "p1", age: 60), (name: "p2", age: 20), (name: "p3", age: 30), (name: "p4", age: 30)]
|
||||
assert people.sortedByIt(it.name) == @[(name: "p1", age: 60), (name: "p2",
|
||||
age: 20), (name: "p3", age: 30), (name: "p4", age: 30)]
|
||||
# Nested sort
|
||||
assert people.sortedByIt((it.age, it.name)) == @[(name: "p2", age: 20), (name: "p3", age: 30), (name: "p4", age: 30), (name: "p1", age: 60)]
|
||||
assert people.sortedByIt((it.age, it.name)) == @[(name: "p2", age: 20),
|
||||
(name: "p3", age: 30), (name: "p4", age: 30), (name: "p1", age: 60)]
|
||||
var result = sorted(seq1, proc(x, y: type(seq1[0])): int =
|
||||
var it {.inject.} = x
|
||||
let a = op
|
||||
|
|
@ -745,7 +750,8 @@ proc rotatedInternal[T](arg: openArray[T]; first, middle, last: int): seq[T] =
|
|||
for i in last ..< arg.len:
|
||||
result[i] = arg[i]
|
||||
|
||||
proc rotateLeft*[T](arg: var openArray[T]; slice: HSlice[int, int]; dist: int): int {.discardable.} =
|
||||
proc rotateLeft*[T](arg: var openArray[T]; slice: HSlice[int, int];
|
||||
dist: int): int {.discardable.} =
|
||||
## Performs a left rotation on a range of elements. If you want to rotate
|
||||
## right, use a negative ``dist``. Specifically, ``rotateLeft`` rotates
|
||||
## the elements at ``slice`` by ``dist`` positions.
|
||||
|
|
@ -801,7 +807,8 @@ proc rotateLeft*[T](arg: var openArray[T]; dist: int): int {.discardable.} =
|
|||
let distLeft = ((dist mod arglen) + arglen) mod arglen
|
||||
arg.rotateInternal(0, distLeft, arglen)
|
||||
|
||||
proc rotatedLeft*[T](arg: openArray[T]; slice: HSlice[int, int], dist: int): seq[T] =
|
||||
proc rotatedLeft*[T](arg: openArray[T]; slice: HSlice[int, int],
|
||||
dist: int): seq[T] =
|
||||
## Same as ``rotateLeft``, just with the difference that it does
|
||||
## not modify the argument. It creates a new ``seq`` instead.
|
||||
##
|
||||
|
|
@ -877,8 +884,10 @@ when isMainModule:
|
|||
doAssert product(newSeq[seq[int]]()) == newSeq[seq[int]](), "empty input"
|
||||
doAssert product(@[newSeq[int](), @[], @[]]) == newSeq[seq[int]](), "bit more empty input"
|
||||
doAssert product(@[@[1, 2]]) == @[@[1, 2]], "a simple case of one element"
|
||||
doAssert product(@[@[1,2], @[3,4]]) == @[@[2,4],@[1,4],@[2,3],@[1,3]], "two elements"
|
||||
doAssert product(@[@[1,2], @[3,4], @[5,6]]) == @[@[2,4,6],@[1,4,6],@[2,3,6],@[1,3,6], @[2,4,5],@[1,4,5],@[2,3,5],@[1,3,5]], "three elements"
|
||||
doAssert product(@[@[1, 2], @[3, 4]]) == @[@[2, 4], @[1, 4], @[2, 3], @[1,
|
||||
3]], "two elements"
|
||||
doAssert product(@[@[1, 2], @[3, 4], @[5, 6]]) == @[@[2, 4, 6], @[1, 4, 6],
|
||||
@[2, 3, 6], @[1, 3, 6], @[2, 4, 5], @[1, 4, 5], @[2, 3, 5], @[1, 3, 5]], "three elements"
|
||||
doAssert product(@[@[1, 2], @[]]) == newSeq[seq[int]](), "two elements, but one empty"
|
||||
|
||||
block lowerBound:
|
||||
|
|
|
|||
|
|
@ -62,12 +62,12 @@ proc `==` *[T](x, y: Complex[T]): bool =
|
|||
## Compare two complex numbers ``x`` and ``y`` for equality.
|
||||
result = x.re == y.re and x.im == y.im
|
||||
|
||||
proc `+` *[T](x: T, y: Complex[T]): Complex[T] =
|
||||
proc `+` *[T](x: T; y: Complex[T]): Complex[T] =
|
||||
## Add a real number to a complex number.
|
||||
result.re = x + y.re
|
||||
result.im = y.im
|
||||
|
||||
proc `+` *[T](x: Complex[T], y: T): Complex[T] =
|
||||
proc `+` *[T](x: Complex[T]; y: T): Complex[T] =
|
||||
## Add a complex number to a real number.
|
||||
result.re = x.re + y
|
||||
result.im = x.im
|
||||
|
|
@ -82,11 +82,11 @@ proc `-` *[T](z: Complex[T]): Complex[T] =
|
|||
result.re = -z.re
|
||||
result.im = -z.im
|
||||
|
||||
proc `-` *[T](x: T, y: Complex[T]): Complex[T] =
|
||||
proc `-` *[T](x: T; y: Complex[T]): Complex[T] =
|
||||
## Subtract a complex number from a real number.
|
||||
x + (-y)
|
||||
|
||||
proc `-` *[T](x: Complex[T], y: T): Complex[T] =
|
||||
proc `-` *[T](x: Complex[T]; y: T): Complex[T] =
|
||||
## Subtract a real number from a complex number.
|
||||
result.re = x.re - y
|
||||
result.im = x.im
|
||||
|
|
@ -96,12 +96,12 @@ proc `-` *[T](x, y: Complex[T]): Complex[T] =
|
|||
result.re = x.re - y.re
|
||||
result.im = x.im - y.im
|
||||
|
||||
proc `/` *[T](x: Complex[T], y: T): Complex[T] =
|
||||
proc `/` *[T](x: Complex[T]; y: T): Complex[T] =
|
||||
## Divide complex number ``x`` by real number ``y``.
|
||||
result.re = x.re / y
|
||||
result.im = x.im / y
|
||||
|
||||
proc `/` *[T](x: T, y: Complex[T]): Complex[T] =
|
||||
proc `/` *[T](x: T; y: Complex[T]): Complex[T] =
|
||||
## Divide real number ``x`` by complex number ``y``.
|
||||
result = x * inv(y)
|
||||
|
||||
|
|
@ -119,12 +119,12 @@ proc `/` *[T](x, y: Complex[T]): Complex[T] =
|
|||
result.re = (x.re + r * x.im) / den
|
||||
result.im = (x.im - r * x.re) / den
|
||||
|
||||
proc `*` *[T](x: T, y: Complex[T]): Complex[T] =
|
||||
proc `*` *[T](x: T; y: Complex[T]): Complex[T] =
|
||||
## Multiply a real number and a complex number.
|
||||
result.re = x * y.re
|
||||
result.im = x * y.im
|
||||
|
||||
proc `*` *[T](x: Complex[T], y: T): Complex[T] =
|
||||
proc `*` *[T](x: Complex[T]; y: T): Complex[T] =
|
||||
## Multiply a complex number with a real number.
|
||||
result.re = x.re * y
|
||||
result.im = x.im * y
|
||||
|
|
@ -135,23 +135,23 @@ proc `*` *[T](x, y: Complex[T]): Complex[T] =
|
|||
result.im = x.im * y.re + x.re * y.im
|
||||
|
||||
|
||||
proc `+=` *[T](x: var Complex[T], y: Complex[T]) =
|
||||
proc `+=` *[T](x: var Complex[T]; y: Complex[T]) =
|
||||
## Add ``y`` to ``x``.
|
||||
x.re += y.re
|
||||
x.im += y.im
|
||||
|
||||
proc `-=` *[T](x: var Complex[T], y: Complex[T]) =
|
||||
proc `-=` *[T](x: var Complex[T]; y: Complex[T]) =
|
||||
## Subtract ``y`` from ``x``.
|
||||
x.re -= y.re
|
||||
x.im -= y.im
|
||||
|
||||
proc `*=` *[T](x: var Complex[T], y: Complex[T]) =
|
||||
proc `*=` *[T](x: var Complex[T]; y: Complex[T]) =
|
||||
## Multiply ``y`` to ``x``.
|
||||
let im = x.im * y.re + x.re * y.im
|
||||
x.re = x.re * y.re - x.im * y.im
|
||||
x.im = im
|
||||
|
||||
proc `/=` *[T](x: var Complex[T], y: Complex[T]) =
|
||||
proc `/=` *[T](x: var Complex[T]; y: Complex[T]) =
|
||||
## Divide ``x`` by ``y`` in place.
|
||||
x = x / y
|
||||
|
||||
|
|
@ -222,7 +222,7 @@ proc pow*[T](x, y: Complex[T]): Complex[T] =
|
|||
result.re = s * cos(r)
|
||||
result.im = s * sin(r)
|
||||
|
||||
proc pow*[T](x: Complex[T], y: T): Complex[T] =
|
||||
proc pow*[T](x: Complex[T]; y: T): Complex[T] =
|
||||
## Complex number ``x`` raised to the power ``y``.
|
||||
pow(x, complex[T](y))
|
||||
|
||||
|
|
@ -352,7 +352,7 @@ when isMainModule:
|
|||
proc `=~`[T](x, y: Complex[T]): bool =
|
||||
result = abs(x.re-y.re) < 1e-6 and abs(x.im-y.im) < 1e-6
|
||||
|
||||
proc `=~`[T](x: Complex[T], y: T): bool =
|
||||
proc `=~`[T](x: Complex[T]; y: T): bool =
|
||||
result = abs(x.re-y) < 1e-6 and abs(x.im) < 1e-6
|
||||
|
||||
var
|
||||
|
|
|
|||
|
|
@ -107,8 +107,8 @@ const
|
|||
|
||||
FLT_MANT_DIG = 24 ## the number of base FLT_RADIX digits in the mantissa part of a float
|
||||
FLT_DIG = 6 ## the number of digits of precision of a float
|
||||
FLT_MIN_EXP = -125 # the minimum value of base FLT_RADIX in the exponent part of a float
|
||||
FLT_MAX_EXP = 128 # the maximum value of base FLT_RADIX in the exponent part of a float
|
||||
FLT_MIN_EXP = -125 ## the minimum value of base FLT_RADIX in the exponent part of a float
|
||||
FLT_MAX_EXP = 128 ## the maximum value of base FLT_RADIX in the exponent part of a float
|
||||
FLT_MIN_10_EXP = -37 ## the minimum value in base 10 of the exponent part of a float
|
||||
FLT_MAX_10_EXP = 38 ## the maximum value in base 10 of the exponent part of a float
|
||||
FLT_MIN = 1.17549435e-38'f32 ## the minimum value of a float
|
||||
|
|
|
|||
|
|
@ -472,7 +472,8 @@ when not defined(JS): # C
|
|||
## .. code-block:: nim
|
||||
## echo arctan(1.0) ## 0.7853981633974483
|
||||
## echo radToDeg(arctan(1.0)) ## 45.0
|
||||
proc arctan2*(y, x: float32): float32 {.importc: "atan2f", header: "<math.h>".}
|
||||
proc arctan2*(y, x: float32): float32 {.importc: "atan2f",
|
||||
header: "<math.h>".}
|
||||
proc arctan2*(y, x: float64): float64 {.importc: "atan2", header: "<math.h>".}
|
||||
## Calculate the arc tangent of ``y`` / ``x``.
|
||||
##
|
||||
|
|
@ -603,9 +604,11 @@ when not defined(JS): # C
|
|||
## echo gamma(11.0) # 3628800.0
|
||||
## echo gamma(-1.0) # nan
|
||||
proc tgamma*(x: float32): float32
|
||||
{.deprecated: "Deprecated since v0.19.0; use 'gamma' instead", importc: "tgammaf", header: "<math.h>".}
|
||||
{.deprecated: "Deprecated since v0.19.0; use 'gamma' instead",
|
||||
importc: "tgammaf", header: "<math.h>".}
|
||||
proc tgamma*(x: float64): float64
|
||||
{.deprecated: "Deprecated since v0.19.0; use 'gamma' instead", importc: "tgamma", header: "<math.h>".}
|
||||
{.deprecated: "Deprecated since v0.19.0; use 'gamma' instead",
|
||||
importc: "tgamma", header: "<math.h>".}
|
||||
## The gamma function
|
||||
proc lgamma*(x: float32): float32 {.importc: "lgammaf", header: "<math.h>".}
|
||||
proc lgamma*(x: float64): float64 {.importc: "lgamma", header: "<math.h>".}
|
||||
|
|
@ -739,8 +742,10 @@ when not defined(JS): # C
|
|||
## echo trunc(PI) # 3.0
|
||||
## echo trunc(-1.85) # -1.0
|
||||
|
||||
proc fmod*(x, y: float32): float32 {.deprecated: "Deprecated since v0.19.0; use 'mod' instead", importc: "fmodf", header: "<math.h>".}
|
||||
proc fmod*(x, y: float64): float64 {.deprecated: "Deprecated since v0.19.0; use 'mod' instead", importc: "fmod", header: "<math.h>".}
|
||||
proc fmod*(x, y: float32): float32 {.deprecated: "Deprecated since v0.19.0; use 'mod' instead",
|
||||
importc: "fmodf", header: "<math.h>".}
|
||||
proc fmod*(x, y: float64): float64 {.deprecated: "Deprecated since v0.19.0; use 'mod' instead",
|
||||
importc: "fmod", header: "<math.h>".}
|
||||
## Computes the remainder of ``x`` divided by ``y``.
|
||||
|
||||
proc `mod`*(x, y: float32): float32 {.importc: "fmodf", header: "<math.h>".}
|
||||
|
|
@ -779,7 +784,8 @@ else: # JS
|
|||
## ( 6.5 mod -2.5) == 1.5
|
||||
## (-6.5 mod -2.5) == -1.5
|
||||
|
||||
proc round*[T: float32|float64](x: T, places: int): T {.deprecated: "use strformat module instead".} =
|
||||
proc round*[T: float32|float64](x: T, places: int): T {.
|
||||
deprecated: "use strformat module instead".} =
|
||||
## Decimal rounding on a binary floating point number.
|
||||
##
|
||||
## This function is NOT reliable. Floating point numbers cannot hold
|
||||
|
|
|
|||
|
|
@ -481,7 +481,7 @@ proc sample*[T](a: openArray[T]): T =
|
|||
doAssert sample(marbles) == "red"
|
||||
result = a[rand(a.low..a.high)]
|
||||
|
||||
proc sample*[T, U](r: var Rand; a: openArray[T], cdf: openArray[U]): T =
|
||||
proc sample*[T, U](r: var Rand; a: openArray[T]; cdf: openArray[U]): T =
|
||||
## Returns an element from ``a`` using a cumulative distribution function
|
||||
## (CDF) and the given state.
|
||||
##
|
||||
|
|
@ -516,7 +516,7 @@ proc sample*[T, U](r: var Rand; a: openArray[T], cdf: openArray[U]): T =
|
|||
let u = r.rand(float(cdf[^1]))
|
||||
a[cdf.upperBound(U(u))]
|
||||
|
||||
proc sample*[T, U](a: openArray[T], cdf: openArray[U]): T =
|
||||
proc sample*[T, U](a: openArray[T]; cdf: openArray[U]): T =
|
||||
## Returns an element from ``a`` using a cumulative distribution function
|
||||
## (CDF).
|
||||
##
|
||||
|
|
|
|||
|
|
@ -39,7 +39,8 @@ proc toRational*[T: SomeInteger](x: T): Rational[T] =
|
|||
result.num = x
|
||||
result.den = 1
|
||||
|
||||
proc toRational*(x: float, n: int = high(int) shr (sizeof(int) div 2 * 8)): Rational[int] =
|
||||
proc toRational*(x: float,
|
||||
n: int = high(int) shr (sizeof(int) div 2 * 8)): Rational[int] =
|
||||
## Calculates the best rational numerator and denominator
|
||||
## that approximates to `x`, where the denominator is
|
||||
## smaller than `n` (default is the largest possible
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue