diff options
author | Andreas Rumpf <andreas@andreas-desktop> | 2009-12-14 01:38:05 +0100 |
---|---|---|
committer | Andreas Rumpf <andreas@andreas-desktop> | 2009-12-14 01:38:05 +0100 |
commit | 3b7ef2288f60bc5c355ad9aeaa127431ec3aee7b (patch) | |
tree | 5e028b11cf337290942b3cf6ecc599854de36747 /lib | |
parent | 911c1cb301a8483e463772b785b0aee79cf2a68c (diff) | |
download | Nim-3b7ef2288f60bc5c355ad9aeaa127431ec3aee7b.tar.gz |
floating point checks
Diffstat (limited to 'lib')
-rwxr-xr-x | lib/pure/math.nim | 2 | ||||
-rwxr-xr-x | lib/system.nim | 4 | ||||
-rwxr-xr-x | lib/system/arithm.nim | 18 | ||||
-rwxr-xr-x | lib/wrappers/lua/lua.nim | 79 |
4 files changed, 64 insertions, 39 deletions
diff --git a/lib/pure/math.nim b/lib/pure/math.nim index bca45894c..50226f0f4 100755 --- a/lib/pure/math.nim +++ b/lib/pure/math.nim @@ -101,7 +101,7 @@ proc mean*(x: openarray[float]): float {.noSideEffect.} = result = sum(x) / toFloat(len(x)) proc variance*(x: openarray[float]): float {.noSideEffect.} = - ## computes the mean of the elements in `x`. + ## computes the variance of the elements in `x`. ## If `x` is empty, NaN is returned. result = 0.0 var m = mean(x) diff --git a/lib/system.nim b/lib/system.nim index 4dbc0a93d..e0f0b4fe5 100755 --- a/lib/system.nim +++ b/lib/system.nim @@ -222,10 +222,10 @@ type EFloatingPoint* = object of ESynch ## base class for floating point exceptions EFloatInvalidOp* {.compilerproc.} = object of EFloatingPoint ## Invalid operation according to IEEE: Raised by - ## 0.0/0.0, sqrt(-1.0), and log(-37.8) for example. + ## 0.0/0.0, for example. EFloatDivByZero* {.compilerproc.} = object of EFloatingPoint ## Division by zero. Divisor is zero and dividend - ## is a finite nonzero number + ## is a finite nonzero number. EFloatOverflow* {.compilerproc.} = object of EFloatingPoint ## Overflow. Operation produces a result ## that exceeds the range of the exponent diff --git a/lib/system/arithm.nim b/lib/system/arithm.nim index f097ee794..b93bb9844 100755 --- a/lib/system/arithm.nim +++ b/lib/system/arithm.nim @@ -314,3 +314,21 @@ when not defined(mulInt): if 32.0 * abs(resAsFloat - floatProd) <= abs(floatProd): return result raiseOverflow() + +# We avoid setting the FPU control word here for compatibility with libraries +# written in other languages. + +proc raiseFloatInvalidOp {.noinline, noreturn.} = + raise newException(EFloatInvalidOp, "FPU operation caused a NaN result") + +proc nanCheck(x: float64) {.compilerProc, inline.} = + if x != x: raiseFloatInvalidOp() + +proc raiseFloatOverflow(x: float64) {.noinline, noreturn.} = + if x > 0.0: + raise newException(EFloatOverflow, "FPU operation caused an overflow") + else: + raise newException(EFloatUnderflow, "FPU operations caused an underflow") + +proc infCheck(x: float64) {.compilerProc, inline.} = + if x != 0.0 and x*0.5 == x: raiseFloatOverflow(x) diff --git a/lib/wrappers/lua/lua.nim b/lib/wrappers/lua/lua.nim index 9bf4e673e..f12c5e5a4 100755 --- a/lib/wrappers/lua/lua.nim +++ b/lib/wrappers/lua/lua.nim @@ -37,16 +37,16 @@ when defined(MACOSX): const - LUA_NAME* = "liblua(5.2|5.1|5.0).dylib" - LUA_LIB_NAME* = "liblua(5.2|5.1|5.0).dylib" + LUA_NAME* = "liblua(|5.2|5.1|5.0).dylib" + LUA_LIB_NAME* = "liblua(|5.2|5.1|5.0).dylib" elif defined(UNIX): const - LUA_NAME* = "liblua(5.2|5.1|5.0).so.0" - LUA_LIB_NAME* = "liblua(5.2|5.1|5.0).so.0" + LUA_NAME* = "liblua(|5.2|5.1|5.0).so.(|0)" + LUA_LIB_NAME* = "liblua(|5.2|5.1|5.0).so.(|0)" else: const - LUA_NAME* = "lua(5.2|5.1|5.0).dll" - LUA_LIB_NAME* = "lua(5.2|5.1|5.0).dll" + LUA_NAME* = "lua(|5.2|5.1|5.0).dll" + LUA_LIB_NAME* = "lua(|5.2|5.1|5.0).dll" type size_t* = int Psize_t* = ptr size_t @@ -56,7 +56,8 @@ const LUA_RELEASE* = "Lua 5.1.1" LUA_VERSION_NUM* = 501 LUA_COPYRIGHT* = "Copyright (C) 1994-2006 Lua.org, PUC-Rio" - LUA_AUTHORS* = "R. Ierusalimschy, L. H. de Figueiredo & W. Celes" # option for multiple returns in `lua_pcall' and `lua_call' + LUA_AUTHORS* = "R. Ierusalimschy, L. H. de Figueiredo & W. Celes" + # option for multiple returns in `lua_pcall' and `lua_call' LUA_MULTRET* = - 1 # #** pseudo-indices # @@ -74,19 +75,18 @@ const # thread status; 0 is OK type Plua_State* = Pointer - lua_CFunction* = proc (L: Plua_State): int{.cdecl.} # - #** functions that read/write blocks when loading/dumping Lua chunks - # - + lua_CFunction* = proc (L: Plua_State): int{.cdecl.} + +# +#** functions that read/write blocks when loading/dumping Lua chunks +# type lua_Reader* = proc (L: Plua_State, ud: Pointer, sz: Psize_t): cstring{.cdecl.} lua_Writer* = proc (L: Plua_State, p: Pointer, sz: size_t, ud: Pointer): int{. - cdecl.} # - #** prototype for memory-allocation functions - # + cdecl.} lua_Alloc* = proc (ud, theptr: Pointer, osize, nsize: size_t){.cdecl.} -const +const LUA_TNONE* = - 1 LUA_TNIL* = 0 LUA_TBOOLEAN* = 1 @@ -218,9 +218,10 @@ proc lua_getallocf*(L: Plua_State, ud: ptr Pointer): lua_Alloc{.cdecl, dynlib: LUA_NAME, importc.} proc lua_setallocf*(L: Plua_State, f: lua_Alloc, ud: Pointer){.cdecl, dynlib: LUA_NAME, importc.} - # - #** Garbage-collection functions and options - # + +# +#** Garbage-collection functions and options +# const LUA_GCSTOP* = 0 LUA_GCRESTART* = 1 @@ -229,11 +230,13 @@ const LUA_GCCOUNTB* = 4 LUA_GCSTEP* = 5 LUA_GCSETPAUSE* = 6 - LUA_GCSETSTEPMUL* = 7 # - #** =============================================================== - #** some useful macros - #** =============================================================== - # + LUA_GCSETSTEPMUL* = 7 + +# +#** =============================================================== +#** some useful macros +#** =============================================================== +# proc lua_pop*(L: Plua_State, n: int) proc lua_newtable*(L: Plua_state) @@ -252,18 +255,20 @@ proc lua_pushliteral*(L: Plua_State, s: cstring) proc lua_setglobal*(L: Plua_State, s: cstring) proc lua_getglobal*(L: Plua_State, s: cstring) proc lua_tostring*(L: Plua_State, i: int): cstring - # - #** compatibility macros and functions - # +# +#** compatibility macros and functions +# proc lua_getregistry*(L: Plua_State) proc lua_getgccount*(L: Plua_State): int type lua_Chunkreader* = lua_Reader - lua_Chunkwriter* = lua_Writer # - #** {====================================================================== - #** Debug API - #** ======================================================================= - # + lua_Chunkwriter* = lua_Writer + +# +#** {====================================================================== +#** Debug API +#** ======================================================================= +# const LUA_HOOKCALL* = 0 @@ -297,11 +302,13 @@ type i_ci*: int # active function Plua_Debug* = ptr lua_Debug - lua_Hook* = proc (L: Plua_State, ar: Plua_Debug){.cdecl.} # - #** {====================================================================== - #** Debug API - #** ======================================================================= - # + lua_Hook* = proc (L: Plua_State, ar: Plua_Debug){.cdecl.} + +# +#** {====================================================================== +#** Debug API +#** ======================================================================= +# proc lua_getstack*(L: Plua_State, level: int, ar: Plua_Debug): int{.cdecl, dynlib: LUA_NAME, importc.} |