* move up runnableExamples definition so can be used more in system.nim (#10196)

* document that toInt, toBiggestInt round towards 0 and add runnableExamples
* minor doc fixes
This commit is contained in:
Timothee Cour 2019-01-05 07:01:10 -08:00 • committed by Andreas Rumpf
commit ffea3fbfae

View file

@ -118,6 +118,22 @@ proc defined*(x: untyped): bool {.magic: "Defined", noSideEffect, compileTime.}
## # Do here programmer friendly expensive sanity checks. ## # Do here programmer friendly expensive sanity checks.
## # Put here the normal code ## # Put here the normal code
when defined(nimHasRunnableExamples):
proc runnableExamples*(body: untyped) {.magic: "RunnableExamples".}
## A section you should use to mark `runnable example`:idx: code with.
##
## - In normal debug and release builds code within
## a ``runnableExamples`` section is ignored.
## - The documentation generator is aware of these examples and considers them
## part of the ``##`` doc comment. As the last step of documentation
## generation the examples are put into an ``$file_example.nim`` file,
## compiled and tested. The collected examples are
## put into their own module to ensure the examples do not refer to
## non-exported symbols.
else:
template runnableExamples*(body: untyped) =
discard
proc declared*(x: untyped): bool {.magic: "Defined", noSideEffect, compileTime.} proc declared*(x: untyped): bool {.magic: "Defined", noSideEffect, compileTime.}
## Special compile-time procedure that checks whether `x` is ## Special compile-time procedure that checks whether `x` is
## declared. `x` has to be an identifier or a qualified identifier. ## declared. `x` has to be an identifier or a qualified identifier.
@ -1792,23 +1808,26 @@ proc toFloat*(i: int): float {.
proc toBiggestFloat*(i: BiggestInt): BiggestFloat {. proc toBiggestFloat*(i: BiggestInt): BiggestFloat {.
magic: "ToBiggestFloat", noSideEffect, importc: "toBiggestFloat".} magic: "ToBiggestFloat", noSideEffect, importc: "toBiggestFloat".}
## converts an biggestint `i` into a ``biggestfloat``. If the conversion ## converts a biggestint `i` into a ``biggestfloat``. If the conversion
## fails, `ValueError` is raised. However, on most platforms the ## fails, `ValueError` is raised. However, on most platforms the
## conversion cannot fail. ## conversion cannot fail.
proc toInt*(f: float): int {. proc toInt*(f: float): int {.
magic: "ToInt", noSideEffect, importc: "toInt".} magic: "ToInt", noSideEffect, importc: "toInt".} =
## converts a floating point number `f` into an ``int``. Conversion ## converts a floating point number `f` into an ``int``. Conversion
## rounds `f` if it does not contain an integer value. ## rounds `f` half away from 0, see https://en.wikipedia.org/wiki/Rounding#Round_half_away_from_zero
## Note that some floating point numbers (e.g. infinity) cannot be ## Note that some floating point numbers (e.g. infinity or even 1e19)
## accurately converted. ## cannot be accurately converted.
runnableExamples:
doAssert toInt(0.49) == 0
doAssert toInt(0.5) == 1
doAssert toInt(-0.5) == -1 ## rounding is symmetrical
proc toBiggestInt*(f: BiggestFloat): BiggestInt {. proc toBiggestInt*(f: BiggestFloat): BiggestInt {.
magic: "ToBiggestInt", noSideEffect, importc: "toBiggestInt".} magic: "ToBiggestInt", noSideEffect, importc: "toBiggestInt".} =
## converts a biggestfloat `f` into a ``biggestint``. Conversion ## Same as `toInt` but for BiggestFloat to ``BiggestInt``.
## rounds `f` if it does not contain an integer value. runnableExamples:
## Note that some floating point numbers (e.g. infinity) cannot be doAssert toBiggestInt(0.49) == 0
## accurately converted.
proc addQuitProc*(quitProc: proc() {.noconv.}) {. proc addQuitProc*(quitProc: proc() {.noconv.}) {.
importc: "atexit", header: "<stdlib.h>".} importc: "atexit", header: "<stdlib.h>".}
@ -3084,7 +3103,7 @@ when not defined(JS): #and not defined(nimscript):
proc initStackBottom() {.inline, compilerproc.} = proc initStackBottom() {.inline, compilerproc.} =
# WARNING: This is very fragile! An array size of 8 does not work on my # WARNING: This is very fragile! An array size of 8 does not work on my
# Linux 64bit system. -- That's because the stack direction is the other # Linux 64bit system. -- That's because the stack direction is the other
# way round. # way around.
when declared(nimGC_setStackBottom): when declared(nimGC_setStackBottom):
var locals {.volatile.}: pointer var locals {.volatile.}: pointer
locals = addr(locals) locals = addr(locals)
@ -4336,23 +4355,6 @@ when defined(windows) and appType == "console" and defined(nimSetUtf8CodePage):
importc: "SetConsoleOutputCP".} importc: "SetConsoleOutputCP".}
discard setConsoleOutputCP(65001) # 65001 - utf-8 codepage discard setConsoleOutputCP(65001) # 65001 - utf-8 codepage
when defined(nimHasRunnableExamples):
proc runnableExamples*(body: untyped) {.magic: "RunnableExamples".}
## A section you should use to mark `runnable example`:idx: code with.
##
## - In normal debug and release builds code within
## a ``runnableExamples`` section is ignored.
## - The documentation generator is aware of these examples and considers them
## part of the ``##`` doc comment. As the last step of documentation
## generation the examples are put into an ``$file_example.nim`` file,
## compiled and tested. The collected examples are
## put into their own module to ensure the examples do not refer to
## non-exported symbols.
else:
template runnableExamples*(body: untyped) =
discard
template doAssertRaises*(exception: typedesc, code: untyped): typed = template doAssertRaises*(exception: typedesc, code: untyped): typed =
## Raises ``AssertionError`` if specified ``code`` does not raise the ## Raises ``AssertionError`` if specified ``code`` does not raise the
## specified exception. Example: ## specified exception. Example: