Skip to content

Commit 515cd45

Browse files
authored
Add math.copySign (#16406)
* add math.copySign * fix + tests
1 parent 73f778e commit 515cd45

File tree

4 files changed

+94
-1
lines changed

4 files changed

+94
-1
lines changed

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
- `echo` and `debugEcho` will now raise `IOError` if writing to stdout fails. Previous behavior
7070
silently ignored errors. See #16366. Use `-d:nimLegacyEchoNoRaise` for previous behavior.
7171

72+
- Added `math.copySign`.
7273
- Added new operations for singly- and doubly linked lists: `lists.toSinglyLinkedList`
7374
and `lists.toDoublyLinkedList` convert from `openArray`s; `lists.copy` implements
7475
shallow copying; `lists.add` concatenates two lists - an O(1) variation that consumes

compiler/vmops.nim

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ from math import sqrt, ln, log10, log2, exp, round, arccos, arcsin,
1313
arctan, arctan2, cos, cosh, hypot, sinh, sin, tan, tanh, pow, trunc,
1414
floor, ceil, `mod`
1515

16+
when declared(math.copySign):
17+
from math import copySign
18+
1619
from os import getEnv, existsEnv, dirExists, fileExists, putEnv, walkDir, getAppFilename
1720
from md5 import getMD5
1821
from sighashes import symBodyDigest
@@ -168,6 +171,9 @@ proc registerAdditionalOps*(c: PCtx) =
168171
wrap1f_math(floor)
169172
wrap1f_math(ceil)
170173

174+
when declared(copySign):
175+
wrap2f_math(copySign)
176+
171177
wrap1s(getMD5, md5op)
172178

173179
proc `mod Wrapper`(a: VmArgs) {.nimcall.} =

lib/pure/math.nim

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ when defined(c) or defined(cpp):
6262
proc c_isnan(x: float): bool {.importc: "isnan", header: "<math.h>".}
6363
# a generic like `x: SomeFloat` might work too if this is implemented via a C macro.
6464

65+
proc c_copysign(x, y: cfloat): cfloat {.importc: "copysignf", header: "<math.h>".}
66+
proc c_copysign(x, y: cdouble): cdouble {.importc: "copysign", header: "<math.h>".}
67+
6568
func binom*(n, k: int): int =
6669
## Computes the `binomial coefficient <https://en.wikipedia.org/wiki/Binomial_coefficient>`_.
6770
runnableExamples:
@@ -153,6 +156,40 @@ func isNaN*(x: SomeFloat): bool {.inline, since: (1,5,1).} =
153156
when defined(js): fn()
154157
else: result = c_isnan(x)
155158

159+
func copySign*[T: SomeFloat](x, y: T): T {.inline, since: (1, 5, 1).} =
160+
## Returns a value with the magnitude of `x` and the sign of `y`;
161+
## this works even if x or y are NaN or zero, both of which can carry a sign.
162+
runnableExamples:
163+
doAssert copySign(1.0, -0.0) == -1.0
164+
doAssert copySign(0.0, -0.0) == -0.0
165+
doAssert copySign(-1.0, 0.0) == 1.0
166+
doAssert copySign(10.0, 0.0) == 10.0
167+
168+
doAssert copySign(Inf, -1.0) == -Inf
169+
doAssert copySign(-Inf, 1.0) == Inf
170+
doAssert copySign(-1.0, NaN) == 1.0
171+
doAssert copySign(10.0, NaN) == 10.0
172+
173+
doAssert copySign(NaN, 0.0).isNaN
174+
doAssert copySign(NaN, -0.0).isNaN
175+
176+
# fails in VM and JS backend
177+
doAssert copySign(1.0, copySign(NaN, -1.0)) == -1.0
178+
179+
# TODO use signbit for examples
180+
template impl() =
181+
if y > 0.0 or (y == 0.0 and 1.0 / y > 0.0):
182+
result = abs(x)
183+
elif y <= 0.0:
184+
result = -abs(x)
185+
else: # must be NaN
186+
result = abs(x)
187+
188+
when defined(js): impl()
189+
else:
190+
when nimvm: impl()
191+
else: result = c_copysign(x, y)
192+
156193
func classify*(x: float): FloatClass =
157194
## Classifies a floating point value.
158195
##
@@ -1159,3 +1196,4 @@ func lcm*[T](x: openArray[T]): T {.since: (1, 1).} =
11591196
while i < x.len:
11601197
result = lcm(result, x[i])
11611198
inc(i)
1199+

tests/stdlib/tmath.nim

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,5 +308,53 @@ template main =
308308
doAssert not Inf.isNaN
309309
doAssert isNaN(Inf - Inf)
310310

311-
main()
311+
block: # copySign
312+
doAssert copySign(10.0, -1.0) == -10.0
313+
doAssert copySign(-10.0, -1.0) == -10.0
314+
doAssert copySign(-10.0, 1.0) == 10.0
315+
doAssert copySign(float(10), -1.0) == -10.0
316+
317+
doAssert copySign(10.0'f64, -1.0) == -10.0
318+
doAssert copySign(-10.0'f64, -1.0) == -10.0
319+
doAssert copySign(-10.0'f64, 1.0) == 10.0
320+
doAssert copySign(10'f64, -1.0) == -10.0
321+
322+
doAssert copySign(10.0'f32, -1.0) == -10.0
323+
doAssert copySign(-10.0'f32, -1.0) == -10.0
324+
doAssert copySign(-10.0'f32, 1.0) == 10.0
325+
doAssert copySign(10'f32, -1.0) == -10.0
326+
327+
doAssert copySign(Inf, -1.0) == -Inf
328+
doAssert copySign(-Inf, 1.0) == Inf
329+
doAssert copySign(Inf, 1.0) == Inf
330+
doAssert copySign(-Inf, -1.0) == -Inf
331+
doAssert copySign(Inf, 0.0) == Inf
332+
doAssert copySign(Inf, -0.0) == -Inf
333+
doAssert copySign(-Inf, 0.0) == Inf
334+
doAssert copySign(-Inf, -0.0) == -Inf
335+
doAssert copySign(1.0, -0.0) == -1.0
336+
doAssert copySign(0.0, -0.0) == -0.0
337+
doAssert copySign(-1.0, 0.0) == 1.0
338+
doAssert copySign(10.0, 0.0) == 10.0
339+
doAssert copySign(-1.0, NaN) == 1.0
340+
doAssert copySign(10.0, NaN) == 10.0
341+
342+
doAssert copySign(NaN, NaN).isNaN
343+
doAssert copySign(-NaN, NaN).isNaN
344+
doAssert copySign(NaN, -NaN).isNaN
345+
doAssert copySign(-NaN, -NaN).isNaN
346+
doAssert copySign(NaN, 0.0).isNaN
347+
doAssert copySign(NaN, -0.0).isNaN
348+
doAssert copySign(-NaN, 0.0).isNaN
349+
doAssert copySign(-NaN, -0.0).isNaN
350+
351+
when nimvm:
352+
discard
353+
else:
354+
when not defined(js):
355+
doAssert copySign(-1.0, -NaN) == 1.0
356+
doAssert copySign(10.0, -NaN) == 10.0
357+
doAssert copySign(1.0, copySign(NaN, -1.0)) == -1.0 # fails in VM
358+
312359
static: main()
360+
main()

0 commit comments

Comments
 (0)