sgn

constexpr double sgn(const double x) noexcept

Evaluates the sign function [1] for a real input.

Parameters

const double x

A real number.

Returns

double

A real number.

The sign function is the derivative of the absolute value function and is defined as:

\[ \begin{align}\begin{aligned}\DeclareMathOperator\sgn{sgn}\\\begin{split} \sgn(x) = \begin{cases} -1 & \text{if } x < 0 \\ 0 & \text{if } x = 0 \\ 1 & \text{if } x > 0 \end{cases}\end{split}\end{aligned}\end{align} \]

Example

double x = 1;
std::cout << sgn(x) << "\n";

Output:

1

References