summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authormatkuki <kukovecmatic@hotmail.com>2016-06-17 21:52:41 +0200
committerGitHub <noreply@github.com>2016-06-17 21:52:41 +0200
commitf9a659f7abbc374f04cf40e37d073bb4b508473e (patch)
treea54039fb2a7b2b60b39bc572093a1c075434b9c3 /lib
parentb0e4c0ae26373edbeb73ae91a959ed460cc810b0 (diff)
downloadNim-f9a659f7abbc374f04cf40e37d073bb4b508473e.tar.gz
Windows MSVC < 2012 'round' function update
Microsoft Visual C++ compilers prior to 2012 do not implement the 'round', 'roundl' or 'roundf' functions.
This change is fixes this. Tested it with MSVC 2010.
Diffstat (limited to 'lib')
-rw-r--r--lib/pure/math.nim16
1 files changed, 11 insertions, 5 deletions
diff --git a/lib/pure/math.nim b/lib/pure/math.nim
index 2b903bedb..f31ad5db4 100644
--- a/lib/pure/math.nim
+++ b/lib/pure/math.nim
@@ -138,11 +138,6 @@ when not defined(JS):
   proc exp*(x: float64): float64 {.importc: "exp", header: "<math.h>".}
     ## Computes the exponential function of `x` (pow(E, x))
 
-  proc round0(x: float32): float32 {.importc: "roundf", header: "<math.h>".}
-  proc round0(x: float64): float64 {.importc: "round", header: "<math.h>".}
-    ## Converts a float to an int by rounding.  Used internally by the round
-    ## function when the specified number of places is 0.
-
   proc arccos*(x: float32): float32 {.importc: "acosf", header: "<math.h>".}
   proc arccos*(x: float64): float64 {.importc: "acos", header: "<math.h>".}
     ## Computes the arc cosine of `x`
@@ -224,6 +219,17 @@ when not defined(JS):
     ##
     ## .. code-block:: nim
     ##  echo ceil(-2.1) ## -2.0
+  
+  when defined(windows):
+    proc round0[T: float32|float64](x: T): T =
+      ## Windows compilers prior to MSVC 2012 do not implement 'round',
+      ## 'roundl' or 'roundf'.
+      result = if x < 0.0: ceil(x - T(0.5)) else: floor(x + T(0.5))
+  else:
+    proc round0(x: float32): float32 {.importc: "roundf", header: "<math.h>".}
+    proc round0(x: float64): float64 {.importc: "round", header: "<math.h>".}
+      ## Converts a float to an int by rounding.  Used internally by the round
+      ## function when the specified number of places is 0.
 
   proc fmod*(x, y: float32): float32 {.importc: "fmodf", header: "<math.h>".}
   proc fmod*(x, y: float64): float64 {.importc: "fmod", header: "<math.h>".}