disable vcc specific code which doesn't work

This commit is contained in:
Araq 2015-06-26 02:29:58 +02:00
commit d46407caf3

View file

@ -8,11 +8,11 @@
# #
## Constructive mathematics is naturally typed. -- Simon Thompson ## Constructive mathematics is naturally typed. -- Simon Thompson
## ##
## Basic math routines for Nim. ## Basic math routines for Nim.
## This module is available for the `JavaScript target ## This module is available for the `JavaScript target
## <backends.html#the-javascript-target>`_. ## <backends.html#the-javascript-target>`_.
## ##
## Note that the trigonometric functions naturally operate on radians. ## Note that the trigonometric functions naturally operate on radians.
## The helper functions `degToRad` and `radToDeg` provide conversion ## The helper functions `degToRad` and `radToDeg` provide conversion
## between radians and degrees. ## between radians and degrees.
@ -38,9 +38,9 @@ const
MaxFloat32Precision* = 8 ## maximum number of meaningful digits MaxFloat32Precision* = 8 ## maximum number of meaningful digits
## after the decimal point for Nim's ## after the decimal point for Nim's
## ``float32`` type. ## ``float32`` type.
MaxFloatPrecision* = MaxFloat64Precision ## maximum number of MaxFloatPrecision* = MaxFloat64Precision ## maximum number of
## meaningful digits ## meaningful digits
## after the decimal point ## after the decimal point
## for Nim's ``float`` type. ## for Nim's ``float`` type.
RadPerDeg = PI / 180.0 ## number of radians per degree RadPerDeg = PI / 180.0 ## number of radians per degree
@ -55,10 +55,10 @@ type
fcInf, ## value is positive infinity fcInf, ## value is positive infinity
fcNegInf ## value is negative infinity fcNegInf ## value is negative infinity
proc classify*(x: float): FloatClass = proc classify*(x: float): FloatClass =
## classifies a floating point value. Returns `x`'s class as specified by ## classifies a floating point value. Returns `x`'s class as specified by
## `FloatClass`. ## `FloatClass`.
# JavaScript and most C compilers have no classify: # JavaScript and most C compilers have no classify:
if x == 0.0: if x == 0.0:
if 1.0/x == Inf: if 1.0/x == Inf:
@ -73,15 +73,15 @@ proc classify*(x: float): FloatClass =
# XXX: fcSubnormal is not detected! # XXX: fcSubnormal is not detected!
proc binom*(n, k: int): int {.noSideEffect.} = proc binom*(n, k: int): int {.noSideEffect.} =
## computes the binomial coefficient ## computes the binomial coefficient
if k <= 0: return 1 if k <= 0: return 1
if 2*k > n: return binom(n, n-k) if 2*k > n: return binom(n, n-k)
result = n result = n
for i in countup(2, k): for i in countup(2, k):
result = (result * (n + 1 - i)) div i result = (result * (n + 1 - i)) div i
proc fac*(n: int): int {.noSideEffect.} = proc fac*(n: int): int {.noSideEffect.} =
## computes the faculty/factorial function. ## computes the faculty/factorial function.
result = 1 result = 1
for i in countup(2, n): for i in countup(2, n):
@ -95,7 +95,7 @@ proc isPowerOfTwo*(x: int): bool {.noSideEffect.} =
proc nextPowerOfTwo*(x: int): int {.noSideEffect.} = proc nextPowerOfTwo*(x: int): int {.noSideEffect.} =
## returns `x` rounded up to the nearest power of two. ## returns `x` rounded up to the nearest power of two.
## Zero and negative numbers get rounded up to 1. ## Zero and negative numbers get rounded up to 1.
result = x - 1 result = x - 1
when defined(cpu64): when defined(cpu64):
result = result or (result shr 32) result = result or (result shr 32)
when sizeof(int) > 2: when sizeof(int) > 2:
@ -114,8 +114,8 @@ proc countBits32*(n: int32): int {.noSideEffect.} =
v = (v and 0x33333333'i32) +% ((v shr 2'i32) and 0x33333333'i32) v = (v and 0x33333333'i32) +% ((v shr 2'i32) and 0x33333333'i32)
result = ((v +% (v shr 4'i32) and 0xF0F0F0F'i32) *% 0x1010101'i32) shr 24'i32 result = ((v +% (v shr 4'i32) and 0xF0F0F0F'i32) *% 0x1010101'i32) shr 24'i32
proc sum*[T](x: openArray[T]): T {.noSideEffect.} = proc sum*[T](x: openArray[T]): T {.noSideEffect.} =
## computes the sum of the elements in `x`. ## computes the sum of the elements in `x`.
## If `x` is empty, 0 is returned. ## If `x` is empty, 0 is returned.
for i in items(x): result = result + i for i in items(x): result = result + i
@ -129,7 +129,7 @@ proc mean*[T](x: openArray[T]): float {.noSideEffect.} =
result = result / toFloat(len(x)) result = result / toFloat(len(x))
proc variance*[T](x: openArray[T]): float {.noSideEffect.} = proc variance*[T](x: openArray[T]): float {.noSideEffect.} =
## computes the variance of the elements in `x`. ## computes the variance of the elements in `x`.
## If `x` is empty, NaN is returned. ## If `x` is empty, NaN is returned.
## ``toFloat(x: T): float`` must be defined. ## ``toFloat(x: T): float`` must be defined.
result = 0.0 result = 0.0
@ -156,7 +156,7 @@ proc randomize*() {.benign.}
## initializes the random number generator with a "random" ## initializes the random number generator with a "random"
## number, i.e. a tickcount. Note: Does nothing for the JavaScript target, ## number, i.e. a tickcount. Note: Does nothing for the JavaScript target,
## as JavaScript does not support this. ## as JavaScript does not support this.
proc randomize*(seed: int) {.benign.} proc randomize*(seed: int) {.benign.}
## initializes the random number generator with a specific seed. ## initializes the random number generator with a specific seed.
## Note: Does nothing for the JavaScript target, ## Note: Does nothing for the JavaScript target,
@ -168,14 +168,14 @@ when not defined(JS):
## computes the square root of `x`. ## computes the square root of `x`.
proc cbrt*(x: float): float {.importc: "cbrt", header: "<math.h>".} proc cbrt*(x: float): float {.importc: "cbrt", header: "<math.h>".}
## computes the cubic root of `x` ## computes the cubic root of `x`
proc ln*(x: float): float {.importc: "log", header: "<math.h>".} proc ln*(x: float): float {.importc: "log", header: "<math.h>".}
## computes ln(x). ## computes ln(x).
proc log10*(x: float): float {.importc: "log10", header: "<math.h>".} proc log10*(x: float): float {.importc: "log10", header: "<math.h>".}
proc log2*(x: float): float = return ln(x) / ln(2.0) proc log2*(x: float): float = return ln(x) / ln(2.0)
proc exp*(x: float): float {.importc: "exp", header: "<math.h>".} proc exp*(x: float): float {.importc: "exp", header: "<math.h>".}
## computes e**x. ## computes e**x.
proc frexp*(x: float, exponent: var int): float {. proc frexp*(x: float, exponent: var int): float {.
importc: "frexp", header: "<math.h>".} importc: "frexp", header: "<math.h>".}
## Split a number into mantissa and exponent. ## Split a number into mantissa and exponent.
@ -183,10 +183,10 @@ when not defined(JS):
## and less than 1) and the integer value n such that `x` (the original ## and less than 1) and the integer value n such that `x` (the original
## float value) equals m * 2**n. frexp stores n in `exponent` and returns ## float value) equals m * 2**n. frexp stores n in `exponent` and returns
## m. ## m.
proc round*(x: float): int {.importc: "lrint", header: "<math.h>".} proc round*(x: float): int {.importc: "lrint", header: "<math.h>".}
## converts a float to an int by rounding. ## converts a float to an int by rounding.
proc arccos*(x: float): float {.importc: "acos", header: "<math.h>".} proc arccos*(x: float): float {.importc: "acos", header: "<math.h>".}
proc arcsin*(x: float): float {.importc: "asin", header: "<math.h>".} proc arcsin*(x: float): float {.importc: "asin", header: "<math.h>".}
proc arctan*(x: float): float {.importc: "atan", header: "<math.h>".} proc arctan*(x: float): float {.importc: "atan", header: "<math.h>".}
@ -195,31 +195,31 @@ when not defined(JS):
## `atan2` returns the arc tangent of `y` / `x`; it produces correct ## `atan2` returns the arc tangent of `y` / `x`; it produces correct
## results even when the resulting angle is near pi/2 or -pi/2 ## results even when the resulting angle is near pi/2 or -pi/2
## (`x` near 0). ## (`x` near 0).
proc cos*(x: float): float {.importc: "cos", header: "<math.h>".} proc cos*(x: float): float {.importc: "cos", header: "<math.h>".}
proc cosh*(x: float): float {.importc: "cosh", header: "<math.h>".} proc cosh*(x: float): float {.importc: "cosh", header: "<math.h>".}
proc hypot*(x, y: float): float {.importc: "hypot", header: "<math.h>".} proc hypot*(x, y: float): float {.importc: "hypot", header: "<math.h>".}
## same as ``sqrt(x*x + y*y)``. ## same as ``sqrt(x*x + y*y)``.
proc sinh*(x: float): float {.importc: "sinh", header: "<math.h>".} proc sinh*(x: float): float {.importc: "sinh", header: "<math.h>".}
proc sin*(x: float): float {.importc: "sin", header: "<math.h>".} proc sin*(x: float): float {.importc: "sin", header: "<math.h>".}
proc tan*(x: float): float {.importc: "tan", header: "<math.h>".} proc tan*(x: float): float {.importc: "tan", header: "<math.h>".}
proc tanh*(x: float): float {.importc: "tanh", header: "<math.h>".} proc tanh*(x: float): float {.importc: "tanh", header: "<math.h>".}
proc pow*(x, y: float): float {.importc: "pow", header: "<math.h>".} proc pow*(x, y: float): float {.importc: "pow", header: "<math.h>".}
## computes x to power raised of y. ## computes x to power raised of y.
proc erf*(x: float): float {.importc: "erf", header: "<math.h>".} proc erf*(x: float): float {.importc: "erf", header: "<math.h>".}
## The error function ## The error function
proc erfc*(x: float): float {.importc: "erfc", header: "<math.h>".} proc erfc*(x: float): float {.importc: "erfc", header: "<math.h>".}
## The complementary error function ## The complementary error function
proc lgamma*(x: float): float {.importc: "lgamma", header: "<math.h>".} proc lgamma*(x: float): float {.importc: "lgamma", header: "<math.h>".}
## Natural log of the gamma function ## Natural log of the gamma function
proc tgamma*(x: float): float {.importc: "tgamma", header: "<math.h>".} proc tgamma*(x: float): float {.importc: "tgamma", header: "<math.h>".}
## The gamma function ## The gamma function
# C procs: # C procs:
when defined(vcc): when defined(vcc) and false:
# The "secure" random, available from Windows XP # The "secure" random, available from Windows XP
# https://msdn.microsoft.com/en-us/library/sxtz2fa8.aspx # https://msdn.microsoft.com/en-us/library/sxtz2fa8.aspx
# Present in some variants of MinGW but not enough to justify # Present in some variants of MinGW but not enough to justify
@ -230,7 +230,7 @@ when not defined(JS):
else: else:
proc srand(seed: cint) {.importc: "srand", header: "<stdlib.h>".} proc srand(seed: cint) {.importc: "srand", header: "<stdlib.h>".}
proc rand(): cint {.importc: "rand", header: "<stdlib.h>".} proc rand(): cint {.importc: "rand", header: "<stdlib.h>".}
when not defined(windows): when not defined(windows):
proc srand48(seed: clong) {.importc: "srand48", header: "<stdlib.h>".} proc srand48(seed: clong) {.importc: "srand48", header: "<stdlib.h>".}
proc drand48(): float {.importc: "drand48", header: "<stdlib.h>".} proc drand48(): float {.importc: "drand48", header: "<stdlib.h>".}
@ -256,7 +256,7 @@ when not defined(JS):
# on MSDN and very unlikely to change # on MSDN and very unlikely to change
const rand_max = 32767 const rand_max = 32767
result = (float(rand()) / float(rand_max)) * max result = (float(rand()) / float(rand_max)) * max
when not defined(vcc): # the above code for vcc uses `discard` instead when not defined(vcc): # the above code for vcc uses `discard` instead
# this is either not Windows or is Windows without vcc # this is either not Windows or is Windows without vcc
proc randomize() = proc randomize() =
@ -264,7 +264,7 @@ when not defined(JS):
proc randomize(seed: int) = proc randomize(seed: int) =
srand(cint(seed)) # rand_s doesn't use srand srand(cint(seed)) # rand_s doesn't use srand
when declared(srand48): srand48(seed) when declared(srand48): srand48(seed)
proc random(max: int): int = proc random(max: int): int =
result = int(rand()) mod max result = int(rand()) mod max
@ -284,7 +284,7 @@ else:
result = float(mathrandom() * float(max)) result = float(mathrandom() * float(max))
proc randomize() = discard proc randomize() = discard
proc randomize(seed: int) = discard proc randomize(seed: int) = discard
proc sqrt*(x: float): float {.importc: "Math.sqrt", nodecl.} proc sqrt*(x: float): float {.importc: "Math.sqrt", nodecl.}
proc ln*(x: float): float {.importc: "Math.log", nodecl.} proc ln*(x: float): float {.importc: "Math.log", nodecl.}
proc log10*(x: float): float = return ln(x) / ln(10.0) proc log10*(x: float): float = return ln(x) / ln(10.0)
@ -293,7 +293,7 @@ else:
proc exp*(x: float): float {.importc: "Math.exp", nodecl.} proc exp*(x: float): float {.importc: "Math.exp", nodecl.}
proc round*(x: float): int {.importc: "Math.round", nodecl.} proc round*(x: float): int {.importc: "Math.round", nodecl.}
proc pow*(x, y: float): float {.importc: "Math.pow", nodecl.} proc pow*(x, y: float): float {.importc: "Math.pow", nodecl.}
proc frexp*(x: float, exponent: var int): float = proc frexp*(x: float, exponent: var int): float =
if x == 0.0: if x == 0.0:
exponent = 0 exponent = 0
@ -309,7 +309,7 @@ else:
proc arcsin*(x: float): float {.importc: "Math.asin", nodecl.} proc arcsin*(x: float): float {.importc: "Math.asin", nodecl.}
proc arctan*(x: float): float {.importc: "Math.atan", nodecl.} proc arctan*(x: float): float {.importc: "Math.atan", nodecl.}
proc arctan2*(y, x: float): float {.importc: "Math.atan2", nodecl.} proc arctan2*(y, x: float): float {.importc: "Math.atan2", nodecl.}
proc cos*(x: float): float {.importc: "Math.cos", nodecl.} proc cos*(x: float): float {.importc: "Math.cos", nodecl.}
proc cosh*(x: float): float = return (exp(x)+exp(-x))*0.5 proc cosh*(x: float): float = return (exp(x)+exp(-x))*0.5
proc hypot*(x, y: float): float = return sqrt(x*x + y*y) proc hypot*(x, y: float): float = return sqrt(x*x + y*y)
@ -334,7 +334,7 @@ proc `mod`*(x, y: float): float =
## Computes the modulo operation for float operators. Equivalent ## Computes the modulo operation for float operators. Equivalent
## to ``x - y * floor(x/y)``. Note that the remainder will always ## to ``x - y * floor(x/y)``. Note that the remainder will always
## have the same sign as the divisor. ## have the same sign as the divisor.
## ##
## .. code-block:: nim ## .. code-block:: nim
## echo (4.0 mod -3.1) # -2.2 ## echo (4.0 mod -3.1) # -2.2
result = if y == 0.0: x else: x - y * (x/y).floor result = if y == 0.0: x else: x - y * (x/y).floor
@ -355,7 +355,7 @@ type
{.deprecated: [TFloatClass: FloatClass, TRunningStat: RunningStat].} {.deprecated: [TFloatClass: FloatClass, TRunningStat: RunningStat].}
proc push*(s: var RunningStat, x: float) = proc push*(s: var RunningStat, x: float) =
## pushes a value `x` for processing ## pushes a value `x` for processing
inc(s.n) inc(s.n)
# See Knuth TAOCP vol 2, 3rd edition, page 232 # See Knuth TAOCP vol 2, 3rd edition, page 232
@ -375,17 +375,17 @@ proc push*(s: var RunningStat, x: float) =
s.oldM = s.mean s.oldM = s.mean
s.oldS = s.newS s.oldS = s.newS
s.sum = s.sum + x s.sum = s.sum + x
proc push*(s: var RunningStat, x: int) = proc push*(s: var RunningStat, x: int) =
## pushes a value `x` for processing. `x` is simply converted to ``float`` ## pushes a value `x` for processing. `x` is simply converted to ``float``
## and the other push operation is called. ## and the other push operation is called.
push(s, toFloat(x)) push(s, toFloat(x))
proc variance*(s: RunningStat): float = proc variance*(s: RunningStat): float =
## computes the current variance of `s` ## computes the current variance of `s`
if s.n > 1: result = s.newS / (toFloat(s.n - 1)) if s.n > 1: result = s.newS / (toFloat(s.n - 1))
proc standardDeviation*(s: RunningStat): float = proc standardDeviation*(s: RunningStat): float =
## computes the current standard deviation of `s` ## computes the current standard deviation of `s`
result = sqrt(variance(s)) result = sqrt(variance(s))
@ -442,7 +442,7 @@ when isMainModule and not defined(JS):
# Check for no side effect annotation # Check for no side effect annotation
proc mySqrt(num: float): float {.noSideEffect.} = proc mySqrt(num: float): float {.noSideEffect.} =
return sqrt(num) return sqrt(num)
# check gamma function # check gamma function
assert(tgamma(5.0) == 24.0) # 4! assert(tgamma(5.0) == 24.0) # 4!
assert(lgamma(1.0) == 0.0) # ln(1.0) == 0.0 assert(lgamma(1.0) == 0.0) # ln(1.0) == 0.0