diff --git a/vlib/math/const.v b/vlib/math/const.v index df1460dabb..61226f3c05 100644 --- a/vlib/math/const.v +++ b/vlib/math/const.v @@ -4,21 +4,30 @@ module math pub const ( - e = 2.71828182845904523536028747135266249775724709369995957496696763 - pi = 3.14159265358979323846264338327950288419716939937510582097494459 - pi_2 = pi / 2.0 - pi_4 = pi / 4.0 - phi = 1.61803398874989484820458683436563811772030917980576286213544862 - tau = 6.28318530717958647692528676655900576839433879875021164194988918 - sqrt2 = 1.41421356237309504880168872420969807856967187537694807317667974 - sqrt_e = 1.64872127070012814684865078781416357165377610071014801157507931 - sqrt_pi = 1.77245385090551602729816748334114518279754945612238712821380779 - sqrt_tau = 2.50662827463100050241576528481104525300698674060993831662992357 - sqrt_phi = 1.27201964951406896425242246173749149171560804184009624861664038 - ln2 = 0.693147180559945309417232121458176568075500134360255254120680009 - log2_e = 1.0 / ln2 - ln10 = 2.30258509299404568401799145468436420760110148862877297603332790 - log10_e = 1.0 / ln10 + epsilon = 2.2204460492503130808472633361816E-16 + e = 2.71828182845904523536028747135266249775724709369995957496696763 + pi = 3.14159265358979323846264338327950288419716939937510582097494459 + pi_2 = pi / 2.0 + pi_4 = pi / 4.0 + phi = 1.61803398874989484820458683436563811772030917980576286213544862 + tau = 6.28318530717958647692528676655900576839433879875021164194988918 + one_over_tau = 1.0 / tau + one_over_pi = 1.0 / pi + tau_over2 = tau / 2.0 + tau_over4 = tau / 4.0 + tau_over8 = tau / 8.0 + sqrt2 = 1.41421356237309504880168872420969807856967187537694807317667974 + sqrt_3 = 1.73205080756887729352744634150587236694280525381038062805580697 + sqrt_5 = 2.23606797749978969640917366873127623544061835961152572427089724 + sqrt_e = 1.64872127070012814684865078781416357165377610071014801157507931 + sqrt_pi = 1.77245385090551602729816748334114518279754945612238712821380779 + sqrt_tau = 2.50662827463100050241576528481104525300698674060993831662992357 + sqrt_phi = 1.27201964951406896425242246173749149171560804184009624861664038 + ln2 = 0.693147180559945309417232121458176568075500134360255254120680009 + log2_e = 1.0 / ln2 + ln10 = 2.30258509299404568401799145468436420760110148862877297603332790 + log10_e = 1.0 / ln10 + two_thirds = 0.66666666666666666666666666666666666666666666666666666666666667 ) // Floating-point limit values diff --git a/vlib/math/math.v b/vlib/math/math.v index 71562b04df..5c404d0a4e 100644 --- a/vlib/math/math.v +++ b/vlib/math/math.v @@ -42,6 +42,15 @@ pub fn degrees(radians f64) f64 { return radians * (180.0 / pi) } +// angle_diff calculates the difference between angles in radians +[inline] +pub fn angle_diff(radian_a f64, radian_b f64) f64 { + mut delta := fmod(radian_b - radian_a, tau) + delta = fmod(delta + 1.5 * tau, tau) + delta -= .5 * tau + return delta +} + [params] pub struct DigitParams { base int = 10 diff --git a/vlib/math/math_test.v b/vlib/math/math_test.v index aeaabf0a1f..fc2d16cd17 100644 --- a/vlib/math/math_test.v +++ b/vlib/math/math_test.v @@ -222,6 +222,16 @@ fn test_nan() { assert nan_f32 != nan_f32 } +fn test_angle_diff() { + for pair in [ + [pi, pi_2, -pi_2], + [pi_2 * 3.0, pi_2, -pi], + [pi / 6.0, two_thirds * pi, pi_2], + ] { + assert angle_diff(pair[0], pair[1]) == pair[2] + } +} + fn test_acos() { for i := 0; i < math.vf_.len; i++ { a := math.vf_[i] / 10