2022-01-04 12:21:08 +03:00
|
|
|
// Copyright (c) 2019-2022 Alexander Medvednikov. All rights reserved.
|
2020-05-17 12:00:29 +03:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
module fractions
|
|
|
|
|
|
|
|
import math
|
|
|
|
|
|
|
|
const (
|
|
|
|
default_eps = 1.0e-4
|
|
|
|
max_iterations = 50
|
|
|
|
zero = fraction(0, 1)
|
|
|
|
)
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
// Unwrapped evaluation methods for fast evaluation of continued fractions.
|
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
// We need these functions because the evaluation of continued fractions
|
|
|
|
// always has to be done from the end. Also, the numerator-denominator pairs
|
|
|
|
// are generated from front to end. This means building a result from a
|
|
|
|
// previous one isn't possible. So we need unrolled versions to ensure that
|
|
|
|
// we don't take too much of a performance penalty by calling eval_cf
|
|
|
|
// several times.
|
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
// eval_1 returns the result of evaluating a continued fraction series of length 1
|
|
|
|
fn eval_1(whole i64, d []i64) Fraction {
|
|
|
|
return fraction(whole * d[0] + 1, d[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
// eval_2 returns the result of evaluating a continued fraction series of length 2
|
|
|
|
fn eval_2(whole i64, d []i64) Fraction {
|
|
|
|
den := d[0] * d[1] + 1
|
|
|
|
return fraction(whole * den + d[1], den)
|
|
|
|
}
|
|
|
|
|
|
|
|
// eval_3 returns the result of evaluating a continued fraction series of length 3
|
|
|
|
fn eval_3(whole i64, d []i64) Fraction {
|
|
|
|
d1d2_plus_n2 := d[1] * d[2] + 1
|
|
|
|
den := d[0] * d1d2_plus_n2 + d[2]
|
|
|
|
return fraction(whole * den + d1d2_plus_n2, den)
|
|
|
|
}
|
|
|
|
|
|
|
|
// eval_cf evaluates a continued fraction series and returns a Fraction.
|
|
|
|
fn eval_cf(whole i64, den []i64) Fraction {
|
|
|
|
count := den.len
|
|
|
|
// Offload some small-scale calculations
|
|
|
|
// to dedicated functions
|
|
|
|
match count {
|
|
|
|
1 {
|
|
|
|
return eval_1(whole, den)
|
|
|
|
}
|
|
|
|
2 {
|
|
|
|
return eval_2(whole, den)
|
|
|
|
}
|
|
|
|
3 {
|
|
|
|
return eval_3(whole, den)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
last := count - 1
|
2020-05-24 22:07:32 +03:00
|
|
|
mut n := i64(1)
|
2020-05-17 12:00:29 +03:00
|
|
|
mut d := den[last]
|
|
|
|
// The calculations are done from back to front
|
|
|
|
for index := count - 2; index >= 0; index-- {
|
|
|
|
t := d
|
|
|
|
d = den[index] * d + n
|
|
|
|
n = t
|
|
|
|
}
|
|
|
|
return fraction(d * whole + n, d)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// approximate returns a Fraction that approcimates the given value to
|
|
|
|
// within the default epsilon value (1.0e-4). This means the result will
|
|
|
|
// be accurate to 3 places after the decimal.
|
|
|
|
pub fn approximate(val f64) Fraction {
|
2021-05-08 13:32:29 +03:00
|
|
|
return approximate_with_eps(val, fractions.default_eps)
|
2020-05-17 12:00:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// approximate_with_eps returns a Fraction
|
2020-10-21 12:23:03 +03:00
|
|
|
pub fn approximate_with_eps(val f64, eps f64) Fraction {
|
2020-05-17 12:00:29 +03:00
|
|
|
if val == 0.0 {
|
2021-05-08 13:32:29 +03:00
|
|
|
return fractions.zero
|
2020-05-17 12:00:29 +03:00
|
|
|
}
|
|
|
|
if eps < 0.0 {
|
|
|
|
panic('Epsilon value cannot be negative.')
|
|
|
|
}
|
|
|
|
if math.fabs(val) > math.max_i64 {
|
|
|
|
panic('Value out of range.')
|
|
|
|
}
|
|
|
|
// The integer part is separated first. Then we process the fractional
|
|
|
|
// part to generate numerators and denominators in tandem.
|
|
|
|
whole := i64(val)
|
2020-05-24 22:07:32 +03:00
|
|
|
mut frac := val - f64(whole)
|
2020-05-17 12:00:29 +03:00
|
|
|
// Quick exit for integers
|
|
|
|
if frac == 0.0 {
|
|
|
|
return fraction(whole, 1)
|
|
|
|
}
|
|
|
|
mut d := []i64{}
|
2021-05-08 13:32:29 +03:00
|
|
|
mut partial := fractions.zero
|
2020-05-17 12:00:29 +03:00
|
|
|
// We must complete the approximation within the maximum number of
|
|
|
|
// itertations allowed. If we can't panic.
|
|
|
|
// Empirically tested: the hardest constant to approximate is the
|
|
|
|
// golden ratio (math.phi) and for f64s, it only needs 38 iterations.
|
2021-05-08 13:32:29 +03:00
|
|
|
for _ in 0 .. fractions.max_iterations {
|
2020-05-17 12:00:29 +03:00
|
|
|
// We calculate the reciprocal. That's why the numerator is
|
|
|
|
// always 1.
|
|
|
|
frac = 1.0 / frac
|
|
|
|
den := i64(frac)
|
|
|
|
d << den
|
|
|
|
// eval_cf is called often so it needs to be performant
|
|
|
|
partial = eval_cf(whole, d)
|
|
|
|
// Check if we're done
|
|
|
|
if math.fabs(val - partial.f64()) < eps {
|
|
|
|
return partial
|
|
|
|
}
|
2020-05-24 22:07:32 +03:00
|
|
|
frac -= f64(den)
|
2020-05-17 12:00:29 +03:00
|
|
|
}
|
2021-05-08 13:32:29 +03:00
|
|
|
panic("Couldn't converge. Please create an issue on https://github.com/vlang/v")
|
2020-05-17 12:00:29 +03:00
|
|
|
}
|