Skip to content

1.0 / abs(-0.0) returns -inf instead of inf #16494

@timotheecour

Description

@timotheecour
  • abs(-0.0) returns -0.0 instead of 0.0, which causes further issues, eg:
    1.0 / abs(-0.0) returns -inf instead of inf
  • likewise, it should behave like fabs even for NaN (ie setting the sign bit)

Example1: -0.0

doAssert 1.0 / abs(-0.0) == Inf

Current Output

fails

Expected Output

works

Example 2: same issue with nans

when true:
  proc fabs(a: cdouble): cdouble {.importc.}
  proc signbit(a: cdouble): cint {.importc, header: "<math.h>".}
  proc copySign(a, b: cdouble): cdouble {.importc: "copysign", header: "<math.h>".}
  let nan2 = copySign(NaN, -1.0)
  doAssert nan2.fabs.signbit == 0 # ok
  doAssert nan2.abs.signbit == 0 # fails

Possible Solution

for implementation of abs(SomeFloat), use importc fabs and fabsf in RT + VM (via vmops), with a fallback for js as usual; the fallback for js can use the same logic as for copySign(see #16406)

Additional Information

devel 1.5.1 6d442a4

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions