mirror of
https://github.com/nim-lang/Nim.git
synced 2025-12-28 17:04:41 +00:00
[backport] run nimpretty on numbers stuff
This commit is contained in:
@@ -172,8 +172,8 @@ proc binarySearch*[T, K](a: openArray[T], key: K,
|
||||
## ``cmp`` is the comparator function to use, the expected return values are
|
||||
## the same as that of system.cmp.
|
||||
runnableExamples:
|
||||
assert binarySearch(["a","b","c","d"], "d", system.cmp[string]) == 3
|
||||
assert binarySearch(["a","b","d","c"], "d", system.cmp[string]) == 2
|
||||
assert binarySearch(["a", "b", "c", "d"], "d", system.cmp[string]) == 3
|
||||
assert binarySearch(["a", "b", "d", "c"], "d", system.cmp[string]) == 2
|
||||
if a.len == 0:
|
||||
return -1
|
||||
|
||||
@@ -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
|
||||
@@ -244,12 +245,12 @@ proc lowerBound*[T, K](a: openArray[T], key: K, cmp: proc(x: T, k: K): int {.clo
|
||||
## * `upperBound proc<#upperBound,openArray[T],K,proc(T,K)>`_ sorted by ``cmp`` in the specified order
|
||||
## * `upperBound proc<#upperBound,openArray[T],T>`_
|
||||
runnableExamples:
|
||||
var arr = @[1,2,3,5,6,7,8,9]
|
||||
var arr = @[1, 2, 3, 5, 6, 7, 8, 9]
|
||||
assert arr.lowerBound(3, system.cmp[int]) == 2
|
||||
assert arr.lowerBound(4, system.cmp[int]) == 3
|
||||
assert arr.lowerBound(5, system.cmp[int]) == 3
|
||||
arr.insert(4, arr.lowerBound(4, system.cmp[int]))
|
||||
assert arr == [1,2,3,4,5,6,7,8,9]
|
||||
assert arr == [1, 2, 3, 4, 5, 6, 7, 8, 9]
|
||||
result = a.low
|
||||
var count = a.high - a.low + 1
|
||||
var step, pos: int
|
||||
@@ -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
|
||||
@@ -291,12 +293,12 @@ proc upperBound*[T, K](a: openArray[T], key: K, cmp: proc(x: T, k: K): int {.clo
|
||||
## * `lowerBound proc<#lowerBound,openArray[T],K,proc(T,K)>`_ sorted by ``cmp`` in the specified order
|
||||
## * `lowerBound proc<#lowerBound,openArray[T],T>`_
|
||||
runnableExamples:
|
||||
var arr = @[1,2,3,5,6,7,8,9]
|
||||
var arr = @[1, 2, 3, 5, 6, 7, 8, 9]
|
||||
assert arr.upperBound(2, system.cmp[int]) == 2
|
||||
assert arr.upperBound(3, system.cmp[int]) == 3
|
||||
assert arr.upperBound(4, system.cmp[int]) == 3
|
||||
arr.insert(4, arr.upperBound(3, system.cmp[int]))
|
||||
assert arr == [1,2,3,4,5,6,7,8,9]
|
||||
assert arr == [1, 2, 3, 4, 5, 6, 7, 8, 9]
|
||||
result = a.low
|
||||
var count = a.high - a.low + 1
|
||||
var step, pos: int
|
||||
@@ -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:**
|
||||
@@ -492,11 +495,13 @@ template sortedByIt*(seq1, op: untyped): untyped =
|
||||
p2: Person = (name: "p2", age: 20)
|
||||
p3: Person = (name: "p3", age: 30)
|
||||
p4: Person = (name: "p4", age: 30)
|
||||
people = @[p1,p2,p4,p3]
|
||||
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
|
||||
@@ -529,7 +534,7 @@ func isSorted*[T](a: openArray[T],
|
||||
assert isSorted(e) == false
|
||||
result = true
|
||||
for i in 0..<len(a)-1:
|
||||
if cmp(a[i],a[i+1]) * order > 0:
|
||||
if cmp(a[i], a[i+1]) * order > 0:
|
||||
return false
|
||||
|
||||
proc isSorted*[T](a: openArray[T], order = SortOrder.Ascending): bool =
|
||||
@@ -665,19 +670,19 @@ proc prevPermutation*[T](x: var openArray[T]): bool {.discardable.} =
|
||||
|
||||
when isMainModule:
|
||||
# Tests for lowerBound
|
||||
var arr = @[1,2,3,5,6,7,8,9]
|
||||
var arr = @[1, 2, 3, 5, 6, 7, 8, 9]
|
||||
assert arr.lowerBound(0) == 0
|
||||
assert arr.lowerBound(4) == 3
|
||||
assert arr.lowerBound(5) == 3
|
||||
assert arr.lowerBound(10) == 8
|
||||
arr = @[1,5,10]
|
||||
arr = @[1, 5, 10]
|
||||
assert arr.lowerBound(4) == 1
|
||||
assert arr.lowerBound(5) == 1
|
||||
assert arr.lowerBound(6) == 2
|
||||
# Tests for isSorted
|
||||
var srt1 = [1,2,3,4,4,4,4,5]
|
||||
var srt2 = ["iello","hello"]
|
||||
var srt3 = [1.0,1.0,1.0]
|
||||
var srt1 = [1, 2, 3, 4, 4, 4, 4, 5]
|
||||
var srt2 = ["iello", "hello"]
|
||||
var srt3 = [1.0, 1.0, 1.0]
|
||||
var srt4: seq[int]
|
||||
assert srt1.isSorted(cmp) == true
|
||||
assert srt2.isSorted(cmp) == false
|
||||
@@ -686,8 +691,8 @@ when isMainModule:
|
||||
var srtseq = newSeq[int]()
|
||||
assert srtseq.isSorted(cmp) == true
|
||||
# Tests for reversed
|
||||
var arr1 = @[0,1,2,3,4]
|
||||
assert arr1.reversed() == @[4,3,2,1,0]
|
||||
var arr1 = @[0, 1, 2, 3, 4]
|
||||
assert arr1.reversed() == @[4, 3, 2, 1, 0]
|
||||
for i in 0 .. high(arr1):
|
||||
assert arr1.reversed(0, i) == arr1.reversed()[high(arr1) - i .. high(arr1)]
|
||||
assert arr1.reversed(i, high(arr1)) == arr1.reversed()[0 .. high(arr1) - i]
|
||||
@@ -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.
|
||||
##
|
||||
@@ -858,7 +865,7 @@ when isMainModule:
|
||||
doAssert list == expected
|
||||
doAssert list2 == @expected
|
||||
|
||||
var s0,s1,s2,s3,s4,s5 = "xxxabcdefgxxx"
|
||||
var s0, s1, s2, s3, s4, s5 = "xxxabcdefgxxx"
|
||||
|
||||
doAssert s0.rotateLeft(3 ..< 10, 3) == 7
|
||||
doAssert s0 == "xxxdefgabcxxx"
|
||||
@@ -876,20 +883,22 @@ when isMainModule:
|
||||
block product:
|
||||
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], @[]]) == newSeq[seq[int]](), "two elements, but one empty"
|
||||
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], @[]]) == newSeq[seq[int]](), "two elements, but one empty"
|
||||
|
||||
block lowerBound:
|
||||
doAssert lowerBound([1,2,4], 3, system.cmp[int]) == 2
|
||||
doAssert lowerBound([1,2,2,3], 4, system.cmp[int]) == 4
|
||||
doAssert lowerBound([1,2,3,10], 11) == 4
|
||||
doAssert lowerBound([1, 2, 4], 3, system.cmp[int]) == 2
|
||||
doAssert lowerBound([1, 2, 2, 3], 4, system.cmp[int]) == 4
|
||||
doAssert lowerBound([1, 2, 3, 10], 11) == 4
|
||||
|
||||
block upperBound:
|
||||
doAssert upperBound([1,2,4], 3, system.cmp[int]) == 2
|
||||
doAssert upperBound([1,2,2,3], 3, system.cmp[int]) == 4
|
||||
doAssert upperBound([1,2,3,5], 3) == 3
|
||||
doAssert upperBound([1, 2, 4], 3, system.cmp[int]) == 2
|
||||
doAssert upperBound([1, 2, 2, 3], 3, system.cmp[int]) == 4
|
||||
doAssert upperBound([1, 2, 3, 5], 3) == 3
|
||||
|
||||
block fillEmptySeq:
|
||||
var s = newSeq[int]()
|
||||
@@ -901,16 +910,16 @@ when isMainModule:
|
||||
let oneData = @[1]
|
||||
doAssert binarySearch(oneData, 1) == 0
|
||||
doAssert binarySearch(onedata, 7) == -1
|
||||
let someData = @[1,3,4,7]
|
||||
let someData = @[1, 3, 4, 7]
|
||||
doAssert binarySearch(someData, 1) == 0
|
||||
doAssert binarySearch(somedata, 7) == 3
|
||||
doAssert binarySearch(someData, -1) == -1
|
||||
doAssert binarySearch(someData, 5) == -1
|
||||
doAssert binarySearch(someData, 13) == -1
|
||||
let moreData = @[1,3,5,7,4711]
|
||||
let moreData = @[1, 3, 5, 7, 4711]
|
||||
doAssert binarySearch(moreData, -1) == -1
|
||||
doAssert binarySearch(moreData, 1) == 0
|
||||
doAssert binarySearch(moreData, 5) == 2
|
||||
doAssert binarySearch(moreData, 6) == -1
|
||||
doAssert binarySearch(moreData, 4711) == 4
|
||||
doAssert binarySearch(moreData, 4712) == -1
|
||||
doAssert binarySearch(moreData, 1) == 0
|
||||
doAssert binarySearch(moreData, 5) == 2
|
||||
doAssert binarySearch(moreData, 6) == -1
|
||||
doAssert binarySearch(moreData, 4711) == 4
|
||||
doAssert binarySearch(moreData, 4712) == -1
|
||||
|
||||
@@ -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,18 +352,18 @@ 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
|
||||
z: Complex64 = complex(0.0, 0.0)
|
||||
oo: Complex64 = complex(1.0, 1.0)
|
||||
a: Complex64 = complex(1.0, 2.0)
|
||||
b: Complex64 = complex(-1.0, -2.0)
|
||||
m1: Complex64 = complex(-1.0, 0.0)
|
||||
i: Complex64 = complex(0.0, 1.0)
|
||||
z: Complex64 = complex(0.0, 0.0)
|
||||
oo: Complex64 = complex(1.0, 1.0)
|
||||
a: Complex64 = complex(1.0, 2.0)
|
||||
b: Complex64 = complex(-1.0, -2.0)
|
||||
m1: Complex64 = complex(-1.0, 0.0)
|
||||
i: Complex64 = complex(0.0, 1.0)
|
||||
one: Complex64 = complex(1.0, 0.0)
|
||||
tt: Complex64 = complex(10.0, 20.0)
|
||||
tt: Complex64 = complex(10.0, 20.0)
|
||||
ipi: Complex64 = complex(0.0, -PI)
|
||||
|
||||
doAssert(a/2.0 =~ complex(0.5, 1.0))
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
## Floating-point environment. Handling of floating-point rounding and
|
||||
## exceptions (overflow, division by zero, etc.).
|
||||
|
||||
{.deadCodeElim: on.} # dce option deprecated
|
||||
{.deadCodeElim: on.} # dce option deprecated
|
||||
|
||||
when defined(Posix) and not defined(genode):
|
||||
{.passl: "-lm".}
|
||||
@@ -103,26 +103,26 @@ proc feupdateenv*(envp: ptr Tfenv): cint {.importc, header: "<fenv.h>".}
|
||||
## according to saved exceptions.
|
||||
|
||||
const
|
||||
FLT_RADIX = 2 ## the radix of the exponent representation
|
||||
FLT_RADIX = 2 ## the radix of the exponent representation
|
||||
|
||||
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_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
|
||||
FLT_MAX = 3.40282347e+38'f32 ## the maximum value of a float
|
||||
FLT_EPSILON = 1.19209290e-07'f32 ## the difference between 1 and the least value greater than 1 of a float
|
||||
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_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
|
||||
FLT_MAX = 3.40282347e+38'f32 ## the maximum value of a float
|
||||
FLT_EPSILON = 1.19209290e-07'f32 ## the difference between 1 and the least value greater than 1 of a float
|
||||
|
||||
DBL_MANT_DIG = 53 ## the number of base FLT_RADIX digits in the mantissa part of a double
|
||||
DBL_DIG = 15 ## the number of digits of precision of a double
|
||||
DBL_MIN_EXP = -1021 ## the minimum value of base FLT_RADIX in the exponent part of a double
|
||||
DBL_MAX_EXP = 1024 ## the maximum value of base FLT_RADIX in the exponent part of a double
|
||||
DBL_MIN_10_EXP = -307 ## the minimum value in base 10 of the exponent part of a double
|
||||
DBL_MAX_10_EXP = 308 ## the maximum value in base 10 of the exponent part of a double
|
||||
DBL_MIN = 2.2250738585072014E-308 ## the minimal value of a double
|
||||
DBL_MAX = 1.7976931348623157E+308 ## the minimal value of a double
|
||||
DBL_MANT_DIG = 53 ## the number of base FLT_RADIX digits in the mantissa part of a double
|
||||
DBL_DIG = 15 ## the number of digits of precision of a double
|
||||
DBL_MIN_EXP = -1021 ## the minimum value of base FLT_RADIX in the exponent part of a double
|
||||
DBL_MAX_EXP = 1024 ## the maximum value of base FLT_RADIX in the exponent part of a double
|
||||
DBL_MIN_10_EXP = -307 ## the minimum value in base 10 of the exponent part of a double
|
||||
DBL_MAX_10_EXP = 308 ## the maximum value in base 10 of the exponent part of a double
|
||||
DBL_MIN = 2.2250738585072014E-308 ## the minimal value of a double
|
||||
DBL_MAX = 1.7976931348623157E+308 ## the minimal value of a double
|
||||
DBL_EPSILON = 2.2204460492503131E-16 ## the difference between 1 and the least value greater than 1 of a double
|
||||
|
||||
template fpRadix*: int = FLT_RADIX
|
||||
|
||||
@@ -51,7 +51,7 @@
|
||||
|
||||
|
||||
include "system/inclrtl"
|
||||
{.push debugger:off .} # the user does not want to trace a part
|
||||
{.push debugger: off.} # the user does not want to trace a part
|
||||
# of the standard library!
|
||||
|
||||
import bitops
|
||||
@@ -93,39 +93,39 @@ proc fac*(n: int): int =
|
||||
assert(n < factTable.len, $n & " is too large to look up in the table")
|
||||
factTable[n]
|
||||
|
||||
{.push checks:off, line_dir:off, stack_trace:off.}
|
||||
{.push checks: off, line_dir: off, stack_trace: off.}
|
||||
|
||||
when defined(Posix) and not defined(genode):
|
||||
{.passl: "-lm".}
|
||||
|
||||
const
|
||||
PI* = 3.1415926535897932384626433 ## The circle constant PI (Ludolph's number)
|
||||
TAU* = 2.0 * PI ## The circle constant TAU (= 2 * PI)
|
||||
E* = 2.71828182845904523536028747 ## Euler's number
|
||||
PI* = 3.1415926535897932384626433 ## The circle constant PI (Ludolph's number)
|
||||
TAU* = 2.0 * PI ## The circle constant TAU (= 2 * PI)
|
||||
E* = 2.71828182845904523536028747 ## Euler's number
|
||||
|
||||
MaxFloat64Precision* = 16 ## Maximum number of meaningful digits
|
||||
## after the decimal point for Nim's
|
||||
## ``float64`` type.
|
||||
MaxFloat32Precision* = 8 ## Maximum number of meaningful digits
|
||||
## after the decimal point for Nim's
|
||||
## ``float32`` type.
|
||||
MaxFloat64Precision* = 16 ## Maximum number of meaningful digits
|
||||
## after the decimal point for Nim's
|
||||
## ``float64`` type.
|
||||
MaxFloat32Precision* = 8 ## Maximum number of meaningful digits
|
||||
## after the decimal point for Nim's
|
||||
## ``float32`` type.
|
||||
MaxFloatPrecision* = MaxFloat64Precision ## Maximum number of
|
||||
## meaningful digits
|
||||
## after the decimal point
|
||||
## for Nim's ``float`` type.
|
||||
RadPerDeg = PI / 180.0 ## Number of radians per degree
|
||||
RadPerDeg = PI / 180.0 ## Number of radians per degree
|
||||
|
||||
type
|
||||
FloatClass* = enum ## Describes the class a floating point value belongs to.
|
||||
## This is the type that is returned by
|
||||
## `classify proc <#classify,float>`_.
|
||||
fcNormal, ## value is an ordinary nonzero floating point value
|
||||
fcSubnormal, ## value is a subnormal (a very small) floating point value
|
||||
fcZero, ## value is zero
|
||||
fcNegZero, ## value is the negative zero
|
||||
fcNan, ## value is Not-A-Number (NAN)
|
||||
fcInf, ## value is positive infinity
|
||||
fcNegInf ## value is negative infinity
|
||||
fcNormal, ## value is an ordinary nonzero floating point value
|
||||
fcSubnormal, ## value is a subnormal (a very small) floating point value
|
||||
fcZero, ## value is zero
|
||||
fcNegZero, ## value is the negative zero
|
||||
fcNan, ## value is Not-A-Number (NAN)
|
||||
fcInf, ## value is positive infinity
|
||||
fcNegInf ## value is negative infinity
|
||||
|
||||
proc classify*(x: float): FloatClass =
|
||||
## Classifies a floating point value.
|
||||
@@ -186,7 +186,7 @@ proc nextPowerOfTwo*(x: int): int {.noSideEffect.} =
|
||||
result = result or (result shr 4)
|
||||
result = result or (result shr 2)
|
||||
result = result or (result shr 1)
|
||||
result += 1 + ord(x<=0)
|
||||
result += 1 + ord(x <= 0)
|
||||
|
||||
proc countBits32*(n: int32): int {.noSideEffect, deprecated:
|
||||
"Deprecated since v0.20.0; use 'bitops.countSetBits' instead".} =
|
||||
@@ -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
|
||||
|
||||
@@ -78,10 +78,10 @@
|
||||
## <lib.html#pure-libraries-cryptography-and-hashing>`_
|
||||
## in the standard library
|
||||
|
||||
import algorithm #For upperBound
|
||||
import algorithm #For upperBound
|
||||
|
||||
include "system/inclrtl"
|
||||
{.push debugger:off.}
|
||||
{.push debugger: off.}
|
||||
|
||||
when defined(JS):
|
||||
type ui = uint32
|
||||
@@ -94,18 +94,18 @@ else:
|
||||
|
||||
type
|
||||
Rand* = object ## State of a random number generator.
|
||||
##
|
||||
## Create a new Rand state using the `initRand proc<#initRand,int64>`_.
|
||||
##
|
||||
## The module contains a default Rand state for convenience.
|
||||
## It corresponds to the default random number generator's state.
|
||||
## The default Rand state always starts with the same values, but the
|
||||
## `randomize proc<#randomize>`_ can be used to seed the default generator
|
||||
## with a value based on the current time.
|
||||
##
|
||||
## Many procs have two variations: one that takes in a Rand parameter and
|
||||
## another that uses the default generator. The procs that use the default
|
||||
## generator are **not** thread-safe!
|
||||
##
|
||||
## Create a new Rand state using the `initRand proc<#initRand,int64>`_.
|
||||
##
|
||||
## The module contains a default Rand state for convenience.
|
||||
## It corresponds to the default random number generator's state.
|
||||
## The default Rand state always starts with the same values, but the
|
||||
## `randomize proc<#randomize>`_ can be used to seed the default generator
|
||||
## with a value based on the current time.
|
||||
##
|
||||
## Many procs have two variations: one that takes in a Rand parameter and
|
||||
## another that uses the default generator. The procs that use the default
|
||||
## generator are **not** thread-safe!
|
||||
a0, a1: ui
|
||||
|
||||
when defined(JS):
|
||||
@@ -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.
|
||||
##
|
||||
@@ -509,14 +509,14 @@ proc sample*[T, U](r: var Rand; a: openArray[T], cdf: openArray[U]): T =
|
||||
doAssert r.sample(marbles, cdf) == "red"
|
||||
doAssert r.sample(marbles, cdf) == "green"
|
||||
doAssert r.sample(marbles, cdf) == "blue"
|
||||
assert(cdf.len == a.len) # Two basic sanity checks.
|
||||
assert(cdf.len == a.len) # Two basic sanity checks.
|
||||
assert(float(cdf[^1]) > 0.0)
|
||||
#While we could check cdf[i-1] <= cdf[i] for i in 1..cdf.len, that could get
|
||||
#awfully expensive even in debugging modes.
|
||||
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
|
||||
@@ -67,9 +68,9 @@ proc toRational*(x: float, n: int = high(int) shr (sizeof(int) div 2 * 8)): Rati
|
||||
swap m22, m21
|
||||
m11 = m12 * ai + m11
|
||||
m21 = m22 * ai + m21
|
||||
if x == float(ai): break # division by zero
|
||||
if x == float(ai): break # division by zero
|
||||
x = 1/(x - float(ai))
|
||||
if x > float(high(int32)): break # representation failure
|
||||
if x > float(high(int32)): break # representation failure
|
||||
ai = int(x)
|
||||
result = m11 // m21
|
||||
|
||||
@@ -282,69 +283,69 @@ proc hash*[T](x: Rational[T]): Hash =
|
||||
when isMainModule:
|
||||
var
|
||||
z = Rational[int](num: 0, den: 1)
|
||||
o = initRational(num=1, den=1)
|
||||
o = initRational(num = 1, den = 1)
|
||||
a = initRational(1, 2)
|
||||
b = -1 // -2
|
||||
m1 = -1 // 1
|
||||
tt = 10 // 2
|
||||
|
||||
assert( a == a )
|
||||
assert( (a-a) == z )
|
||||
assert( (a+b) == o )
|
||||
assert( (a/b) == o )
|
||||
assert( (a*b) == 1 // 4 )
|
||||
assert( (3/a) == 6 // 1 )
|
||||
assert( (a/3) == 1 // 6 )
|
||||
assert( a*b == 1 // 4 )
|
||||
assert( tt*z == z )
|
||||
assert( 10*a == tt )
|
||||
assert( a*10 == tt )
|
||||
assert( tt/10 == a )
|
||||
assert( a-m1 == 3 // 2 )
|
||||
assert( a+m1 == -1 // 2 )
|
||||
assert( m1+tt == 16 // 4 )
|
||||
assert( m1-tt == 6 // -1 )
|
||||
assert(a == a)
|
||||
assert( (a-a) == z)
|
||||
assert( (a+b) == o)
|
||||
assert( (a/b) == o)
|
||||
assert( (a*b) == 1 // 4)
|
||||
assert( (3/a) == 6 // 1)
|
||||
assert( (a/3) == 1 // 6)
|
||||
assert(a*b == 1 // 4)
|
||||
assert(tt*z == z)
|
||||
assert(10*a == tt)
|
||||
assert(a*10 == tt)
|
||||
assert(tt/10 == a)
|
||||
assert(a-m1 == 3 // 2)
|
||||
assert(a+m1 == -1 // 2)
|
||||
assert(m1+tt == 16 // 4)
|
||||
assert(m1-tt == 6 // -1)
|
||||
|
||||
assert( z < o )
|
||||
assert( z <= o )
|
||||
assert( z == z )
|
||||
assert( cmp(z, o) < 0 )
|
||||
assert( cmp(o, z) > 0 )
|
||||
assert(z < o)
|
||||
assert(z <= o)
|
||||
assert(z == z)
|
||||
assert(cmp(z, o) < 0)
|
||||
assert(cmp(o, z) > 0)
|
||||
|
||||
assert( o == o )
|
||||
assert( o >= o )
|
||||
assert( not(o > o) )
|
||||
assert( cmp(o, o) == 0 )
|
||||
assert( cmp(z, z) == 0 )
|
||||
assert( hash(o) == hash(o) )
|
||||
assert(o == o)
|
||||
assert(o >= o)
|
||||
assert(not(o > o))
|
||||
assert(cmp(o, o) == 0)
|
||||
assert(cmp(z, z) == 0)
|
||||
assert(hash(o) == hash(o))
|
||||
|
||||
assert( a == b )
|
||||
assert( a >= b )
|
||||
assert( not(b > a) )
|
||||
assert( cmp(a, b) == 0 )
|
||||
assert( hash(a) == hash(b) )
|
||||
assert(a == b)
|
||||
assert(a >= b)
|
||||
assert(not(b > a))
|
||||
assert(cmp(a, b) == 0)
|
||||
assert(hash(a) == hash(b))
|
||||
|
||||
var x = 1//3
|
||||
|
||||
x *= 5//1
|
||||
assert( x == 5//3 )
|
||||
assert(x == 5//3)
|
||||
x += 2 // 9
|
||||
assert( x == 17//9 )
|
||||
assert(x == 17//9)
|
||||
x -= 9//18
|
||||
assert( x == 25//18 )
|
||||
assert(x == 25//18)
|
||||
x /= 1//2
|
||||
assert( x == 50//18 )
|
||||
assert(x == 50//18)
|
||||
|
||||
var y = 1//3
|
||||
|
||||
y *= 4
|
||||
assert( y == 4//3 )
|
||||
assert(y == 4//3)
|
||||
y += 5
|
||||
assert( y == 19//3 )
|
||||
assert(y == 19//3)
|
||||
y -= 2
|
||||
assert( y == 13//3 )
|
||||
assert(y == 13//3)
|
||||
y /= 9
|
||||
assert( y == 13//27 )
|
||||
assert(y == 13//27)
|
||||
|
||||
assert toRational(5) == 5//1
|
||||
assert abs(toFloat(y) - 0.4814814814814815) < 1.0e-7
|
||||
|
||||
Reference in New Issue
Block a user