Restrict x to the range from low to high.
low
high
static double clamp(double x, double low, double high) => x < low ? low : (x > high ? high : x);