import { assert } from "../assert.js";
import { PI2 } from "../math/PI2.js";

/**
 * Force angle to [-PI, +PI] range
 *
 * @param {number} angle in radians
 * @return {number} in radians
 */
export function normalize_angle_rad(angle) {
    assert.isNumber(angle, 'angle');

    // based on answer https://stackoverflow.com/a/2323034/3973586

    let r = angle % PI2;
    r = (r + PI2) % PI2;

    if (r > Math.PI) {
        r -= PI2;
    }

    return r;
}
