summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorArne Döring <arne.doering@gmx.net>2018-10-09 19:37:53 +0200
committerAndreas Rumpf <rumpf_a@web.de>2018-10-09 19:37:53 +0200
commit5076fda2e25a7f14dac130b591de6cc1eebfcc06 (patch)
treedce617c5daab3e10f25fdd4e2b5c8cc2eeb43d8c
parentfe19670c5227e73733a1fe44d4bc6fb7c13d512e (diff)
downloadNim-5076fda2e25a7f14dac130b591de6cc1eebfcc06.tar.gz
fix for #9082 (#9089)
-rw-r--r--lib/pure/math.nim30
1 files changed, 16 insertions, 14 deletions
diff --git a/lib/pure/math.nim b/lib/pure/math.nim
index 909aa11b7..284424d91 100644
--- a/lib/pure/math.nim
+++ b/lib/pure/math.nim
@@ -366,13 +366,13 @@ when not defined(JS): # C
       if classify(x) in {fcZero, fcNegZero, fcNan, fcInf, fcNegInf}: return x
       result = truncImpl(x)
 
-    proc round0[T: float32|float64](x: T): T =
+    proc round*[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>".}
+    proc round*(x: float32): float32 {.importc: "roundf", header: "<math.h>".}
+    proc round*(x: float64): float64 {.importc: "round", header: "<math.h>".}
       ## Rounds a float to zero decimal places.  Used internally by the round
       ## function when the specified number of places is 0.
 
@@ -401,7 +401,7 @@ else: # JS
   proc floor*(x: float64): float64 {.importc: "Math.floor", nodecl.}
   proc ceil*(x: float32): float32 {.importc: "Math.ceil", nodecl.}
   proc ceil*(x: float64): float64 {.importc: "Math.ceil", nodecl.}
-  proc round0(x: float): float {.importc: "Math.round", nodecl.}
+  proc round*(x: float): float {.importc: "Math.round", nodecl.}
   proc trunc*(x: float32): float32 {.importc: "Math.trunc", nodecl.}
   proc trunc*(x: float64): float64 {.importc: "Math.trunc", nodecl.}
 
@@ -409,20 +409,22 @@ else: # JS
   proc `mod`*(x, y: float64): float64 {.importcpp: "# % #".}
   ## Computes the modulo operation for float operators.
 
-proc round*[T: float32|float64](x: T, places: int = 0): T =
-  ## Round a floating point number.
+proc round*[T: float32|float64](x: T, places: int): T {.deprecated: "use format instead".} =
+  ## Decimal rounding on a binary floating point number.
   ##
-  ## If `places` is 0 (or omitted), round to the nearest integral value
-  ## following normal mathematical rounding rules (e.g. `round(54.5) -> 55.0`).
-  ## If `places` is greater than 0, round to the given number of decimal
-  ## places, e.g. `round(54.346, 2) -> 54.35`.
-  ## If `places` is negative, round to the left of the decimal place, e.g.
-  ## `round(537.345, -1) -> 540.0`
+  ## This function is NOT reliable. Floating point numbers cannot hold
+  ## non integer decimals precisely.  If `places` is 0 (or omitted),
+  ## round to the nearest integral value following normal mathematical
+  ## rounding rules (e.g.  `round(54.5) -> 55.0`).  If `places` is
+  ## greater than 0, round to the given number of decimal places,
+  ## e.g. `round(54.346, 2) -> 54.350000000000001421...`.  If `places` is negative, round
+  ## to the left of the decimal place, e.g.  `round(537.345, -1) ->
+  ## 540.0`
   if places == 0:
-    result = round0(x)
+    result = round(x)
   else:
     var mult = pow(10.0, places.T)
-    result = round0(x*mult)/mult
+    result = round(x*mult)/mult
 
 proc floorDiv*[T: SomeInteger](x, y: T): T =
   ## Floor division is conceptually defined as ``floor(x / y)``.