rect function

Complex rect(
  1. double x
)

Rectangle function.

x is the input value.

Specification: http://mathworld.wolfram.com/RectangleFunction.html If |x| > 1/2, returns 0. If |x| == 1/2, returns 1/2. If |x| < 1/2, returns 1.

Example:

print(rect(0.5));  // Output: 0.5

Implementation

Complex rect(double x) {
  x = x.abs();
  if (x == 0.5) return Complex(x);
  if (x > 0.5) return Complex.zero();
  return Complex.one();
}