github.com/whatap/golib@v0.0.22/util/mathutil/MathUtil.go (about)

     1  package mathutil
     2  
     3  import (
     4  	"fmt"
     5  	"math"
     6  )
     7  
     8  func Round(v float64) float64 {
     9  	return float64(int64(v))
    10  }
    11  
    12  func RoundScale(value float64, scale int) float64 {
    13  	if scale == 0 {
    14  		return float64(int64(value))
    15  	}
    16  
    17  	var r int64 = Scale(scale)
    18  	var v int64 = int64(value * float64(r))
    19  	return float64(v) / float64(r)
    20  }
    21  
    22  func Scale(n int) int64 {
    23  	var r int64 = 0
    24  	switch n {
    25  	case 1:
    26  		r = 10
    27  	case 2:
    28  		r = 100
    29  	case 3:
    30  		r = 1000
    31  	default:
    32  		r = 10000
    33  	}
    34  	return r
    35  }
    36  
    37  func RoundString(value float64, scale int) string {
    38  	if scale <= 0 {
    39  		return fmt.Sprintf("%d", int64(value))
    40  	}
    41  	return fmt.Sprintf("%f", RoundScale(value, scale))
    42  }
    43  
    44  func Round2(value float64) float64 {
    45  	return RoundScale(value, 2)
    46  }
    47  
    48  func Round4(value float64) float64 {
    49  	return RoundScale(value, 4)
    50  }
    51  
    52  func GetStandardDeviation(count int, timeSum float64, timeSqrSum float64) float64 {
    53  	if count == 0 {
    54  		return 0
    55  	}
    56  	if timeSqrSum == 0 {
    57  		return 0
    58  	}
    59  
    60  	// 제곱의 평균 - 평균의 제곱
    61  	avg := timeSum / float64(count)
    62  	variation := (timeSqrSum / float64(count)) - (avg * avg)
    63  	ret := math.Sqrt(math.Abs(variation))
    64  	if math.IsNaN(ret) {
    65  		return 0
    66  	} else {
    67  		return ret
    68  	}
    69  }
    70  
    71  func GetPct90(avg, stdDev float64) float64 {
    72  	return (avg + stdDev*1.282)
    73  }
    74  func GetPct95(avg, stdDev float64) float64 {
    75  	return (avg + stdDev*1.645)
    76  }