Skip to content

Use the float32 kernel for nextafter on float32 inputs - #577

Open
raashish1601 wants to merge 1 commit into
pydata:masterfrom
raashish1601:fix/nextafter-float32
Open

raashish1601 wants to merge 1 commit into
pydata:masterfrom
raashish1601:fix/nextafter-float32

Conversation

@raashish1601

Copy link
Copy Markdown

nextafter is registered with a minimum kind of double, so float32 inputs are upcast and the step is taken in float64. The result is a float64 array whose values are one float64 ulp away from the inputs, not the next float32 values:

import numpy as np, numexpr as ne
x = np.array([1.0, 0.0], dtype=np.float32); y = np.array([2.0, 1.0], dtype=np.float32)
ne.evaluate("nextafter(x, y)")  # array([1.0e+000, 4.9e-324])  (float64)
np.nextafter(x, y)              # array([1.0000001e+00, 1.4012985e-45], dtype=float32)

The nextafter_fff kernel already exists in functions.hpp (and in the MSVC stubs), so this registers nextafter with a minimum kind of float, like fmod and arctan2. Int inputs still go to double.

hypot, copysign, maximum and minimum are also registered with double even though float32 kernels exist. Their values are right there, only the result dtype is float64 instead of float32, so I left them alone. Happy to switch them too if you want.

Added test_nextafter_float32, which checks the dtype and the exact values against NumPy. It fails on master and passes with this change. Built in a python:3.12 Docker image and ran numexpr.tests.test_numexpr (6043 passed). Also added a line to the release notes.

nextafter was registered with a minimum kind of double, so float32
inputs were upcast and the step was taken in float64. The result was a
float64 array with values one float64 ulp away from the inputs instead
of the next float32 values. The nextafter_fff kernel already exists, so
register the function with a minimum kind of float like fmod and arctan2.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant