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

Add hypot function #1344

Closed
FluxusMagna opened this issue May 11, 2021 · 1 comment · Fixed by #1346
Closed

Add hypot function #1344

FluxusMagna opened this issue May 11, 2021 · 1 comment · Fixed by #1346

Comments

@FluxusMagna
Copy link

FluxusMagna commented May 11, 2021

Hypot (https://en.wikipedia.org/wiki/Hypot) computes the hypotenuse of a right-angle triangle. The motivation essentially boils down to underflow/overflow problems with the naive implementation r = T.sqrt (x*x + y*y). It seems natural to add it since we already have atan2, and they are frequently used together for the purpose of transforming Cartesian coordinates to polar coordinates.

Implementation example:

let hypot x y =
    if x == 0 && y == 0
        then 0
        else
            let switch = f32.abs x < f32.abs y
            let (x, y) = if switch then (y, x) else (x, y)
            let square a = a * a
            in f32.abs x *  f32.sqrt (1 + square (y/x))

There are implementations with more cases to reduce the number of math operations, but I'm not sure they make sense in the context of execution masks.

@athas
Copy link
Member

athas commented May 11, 2021

The policy for including mathematical primitive functions is that we try to include everything that is available in the C math library. This is because I don't want the Futhark compiler itself to become distracted by the yak-shaving of implementing numerically high-quality primitives (although libraries can shave whichever yaks they want).

But hypot is actually part of the C11 math library, so we should definitely include it. It is also part of both OpenCL and CUDA, as far as I can see.

athas added a commit that referenced this issue May 11, 2021
athas added a commit that referenced this issue May 11, 2021
athas added a commit that referenced this issue May 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants