squareWave function

int squareWave(
  1. double x
)

Square wave function

The square wave function generates a periodic waveform which alternates between two levels.

Example:

print(squareWave(2.7));  // Output: -1.0
print(squareWave(3.2));  // Output: 1.0

The output for 2.7 is 1.0 and for 3.2 is -1.0 because the square wave function oscillates between 1.0 and -1.0.

Implementation

int squareWave(double x) {
  return (x % 2 < 1) ? -1 : 1;
}