github.com/blend/go-sdk@v1.20220411.3/mathutil/normalize.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package mathutil
     9  
    10  // Normalize returns a set of numbers on the interval [0,1] for a given set of inputs.
    11  // An example: 4,3,2,1 => 0.4, 0.3, 0.2, 0.1
    12  // Caveat; the total may be < 1.0; there are going to be issues with irrational numbers etc.
    13  func Normalize(values ...float64) []float64 {
    14  	var total float64
    15  	for _, v := range values {
    16  		total += v
    17  	}
    18  	output := make([]float64, len(values))
    19  	for x, v := range values {
    20  		output[x] = RoundDown(v/total, 0.0001)
    21  	}
    22  	return output
    23  }