Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

signbit #505

Closed
timotheecour opened this issue Jan 2, 2021 · 4 comments
Closed

signbit #505

timotheecour opened this issue Jan 2, 2021 · 4 comments
Assignees
Labels

Comments

@timotheecour
Copy link
Owner

timotheecour commented Jan 2, 2021

/cc @xflywind

implementation for js:

when true:
  proc signbit(x: float): bool =
    asm """
    const buffer = new ArrayBuffer(8);
    const floatBuffer = new Float64Array(buffer);
    const uintBuffer = new Uint32Array(buffer);
    floatBuffer[0] = `x`;
    `result` = (uintBuffer[1] >> 31) != 0;
    """
  doAssert not signbit(0.0)
  doAssert signbit(-0.0)
  doAssert signbit(-0.1)
  doAssert not signbit(0.1)

for c: signbit importc
for vm: vmops

@ringabout ringabout self-assigned this Jan 2, 2021
@ringabout
Copy link
Collaborator

ringabout commented Jan 2, 2021

With nim-lang#16403

proc toBitsImpl(x: float): array[2, uint32] =
  asm """
  const buffer = new ArrayBuffer(8);
  const floatBuffer = new Float64Array(buffer);
  const uintBuffer = new Uint32Array(buffer);
  floatBuffer[0] = `x`;
  `result` = uintBuffer
  """


proc signbit(x: float): bool =
  let uintBuffer = toBitsImpl(x)
  result = (uintBuffer[1] shr 31) != 0;

doAssert not signbit(0.0)
doAssert signbit(-0.0)
doAssert signbit(-0.1)
doAssert not signbit(0.1)

@ringabout
Copy link
Collaborator

We could use signbit to refactor copySign if needed.

@timotheecour
Copy link
Owner Author

timotheecour commented Jan 6, 2021

We could use signbit to refactor copySign if needed.

ya i was just about to suggest that; it will also make copySign correct for NaN; let's do that after nim-lang#16592

EDIT:

  • and also this:
    when nimvm:
      discard
    else:
      when not defined(js):
        doAssert copySign(-1.0, -NaN) == 1.0
        doAssert copySign(10.0, -NaN) == 10.0
        doAssert copySign(1.0, copySign(NaN, -1.0)) == -1.0 # fails in VM

=> should become:

    doAssert copySign(-1.0, -NaN) == 1.0
    # doAssert copySign(10.0, -NaN) == -10.0 # pending https://github.com/timotheecour/Nim/issues/499
    doAssert copySign(1.0, copySign(NaN, -1.0)) == -1.0 # fails in VM

(ie one test is wrong, and the 2 other should work in js,vm,c)

  • and this: in runnableExamples:
    # fails in VM and JS backend
    doAssert copySign(1.0, copySign(NaN, -1.0)) == -1.0

=>

    doAssert copySign(1.0, copySign(NaN, -1.0)) == -1.0
    doAssert copySign(NaN, -1.0).signbit

@timotheecour
Copy link
Owner Author

@xflywind I think nim-lang#16592 closes this (but your PR mentioned Ref, not close; not sure why?)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants