Skip to content

Commit

Permalink
* move up runnableExamples definition so can be used more in system.n…
Browse files Browse the repository at this point in the history
…im (nim-lang#10196)

* document that toInt, toBiggestInt round towards 0 and add runnableExamples
* minor doc fixes
  • Loading branch information
timotheecour authored and Araq committed Jan 5, 2019
1 parent e5ca57d commit ffea3fb
Showing 1 changed file with 30 additions and 28 deletions.
58 changes: 30 additions & 28 deletions lib/system.nim
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,22 @@ proc defined*(x: untyped): bool {.magic: "Defined", noSideEffect, compileTime.}
## # Do here programmer friendly expensive sanity checks.
## # 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.}
## Special compile-time procedure that checks whether `x` is
## declared. `x` has to be an identifier or a qualified identifier.
Expand Down Expand Up @@ -1792,23 +1808,26 @@ proc toFloat*(i: int): float {.

proc toBiggestFloat*(i: BiggestInt): BiggestFloat {.
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
## conversion cannot fail.

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
## rounds `f` if it does not contain an integer value.
## Note that some floating point numbers (e.g. infinity) cannot be
## accurately converted.
## 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 or even 1e19)
## 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 {.
magic: "ToBiggestInt", noSideEffect, importc: "toBiggestInt".}
## converts a biggestfloat `f` into a ``biggestint``. Conversion
## rounds `f` if it does not contain an integer value.
## Note that some floating point numbers (e.g. infinity) cannot be
## accurately converted.
magic: "ToBiggestInt", noSideEffect, importc: "toBiggestInt".} =
## Same as `toInt` but for BiggestFloat to ``BiggestInt``.
runnableExamples:
doAssert toBiggestInt(0.49) == 0

proc addQuitProc*(quitProc: proc() {.noconv.}) {.
importc: "atexit", header: "<stdlib.h>".}
Expand Down Expand Up @@ -3084,7 +3103,7 @@ when not defined(JS): #and not defined(nimscript):
proc initStackBottom() {.inline, compilerproc.} =
# 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
# way round.
# way around.
when declared(nimGC_setStackBottom):
var locals {.volatile.}: pointer
locals = addr(locals)
Expand Down Expand Up @@ -4336,23 +4355,6 @@ when defined(windows) and appType == "console" and defined(nimSetUtf8CodePage):
importc: "SetConsoleOutputCP".}
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 =
## Raises ``AssertionError`` if specified ``code`` does not raise the
## specified exception. Example:
Expand Down

0 comments on commit ffea3fb

Please sign in to comment.