mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-25 00:58:45 +05:00
Fix issue where fib function overflows
This commit is contained in:
committed by
Dan Christian Bogos
parent
6a17ed8c99
commit
09c6261903
@@ -424,11 +424,13 @@ func copyFile(rc io.ReadCloser, path string, fm os.FileMode) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
// Fib returns successive Fibonacci numbers
|
||||
// Fib returns successive Fibonacci numbers.
|
||||
func Fib() func() int {
|
||||
a, b := 0, 1
|
||||
return func() int {
|
||||
a, b = b, a+b
|
||||
if b > 0 {
|
||||
a, b = b, a+b // only increment Fibonacci numbers while b doesn't overflow
|
||||
}
|
||||
return a
|
||||
}
|
||||
}
|
||||
@@ -438,7 +440,12 @@ func Fib() func() int {
|
||||
func FibDuration(durationUnit, maxDuration time.Duration) func() time.Duration {
|
||||
fib := Fib()
|
||||
return func() time.Duration {
|
||||
fibNrAsDuration := time.Duration(fib()) * durationUnit
|
||||
fibNrAsDuration := time.Duration(fib())
|
||||
if fibNrAsDuration > (AbsoluteMaxDuration / durationUnit) { // check if the current fibonacci nr. in the sequence would exceed the absolute maximum duration if multiplied by the duration unit value
|
||||
fibNrAsDuration = AbsoluteMaxDuration
|
||||
} else {
|
||||
fibNrAsDuration *= durationUnit
|
||||
}
|
||||
if maxDuration > 0 && maxDuration < fibNrAsDuration {
|
||||
return maxDuration
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user