gitee.com/quant1x/num@v0.3.2/type_float64.go (about)

     1  package num
     2  
     3  import (
     4  	"gitee.com/quant1x/gox/logger"
     5  	"gitee.com/quant1x/num/x64"
     6  	"math"
     7  	"reflect"
     8  	"strconv"
     9  )
    10  
    11  const (
    12  	errorFloat64Base = errorTypeBase + int(reflect.Float64)*100
    13  
    14  	MaxFloat64          float64 = float64(math.MaxFloat64)             // float64最大值
    15  	MinFloat64          float64 = float64(math.SmallestNonzeroFloat64) // float64最小值
    16  	True2Float64        float64 = float64(1)                           // true转float64
    17  	False2Float64       float64 = float64(0)                           // false转float64
    18  	StringNil2Float     float64 = float64(0)                           // deprecated: 字符串空指针转float64
    19  	StringBad2Float     float64 = float64(0)                           // deprecated: 字符串解析float64异常
    20  	StringTrue2Float64  float64 = float64(1)                           // 字符串true转float64
    21  	StringFalse2Float64 float64 = float64(0)                           // 字符串false转float64
    22  )
    23  
    24  var (
    25  	__nilToFloat64 = math.NaN()
    26  )
    27  
    28  // Float64NaN returns an IEEE 754 “not-a-number” value.
    29  func Float64NaN() float64 {
    30  	return math.NaN()
    31  }
    32  
    33  // Float64IsNaN 判断float64是否NaN
    34  func Float64IsNaN(f float64) bool {
    35  	return math.IsNaN(f) || math.IsInf(f, 0)
    36  }
    37  
    38  // number类型切片转成float64切片
    39  func sliceNumberToFloat64[T Number](s []T) []float64 {
    40  	count := len(s)
    41  	if count == 0 {
    42  		return []float64{}
    43  	}
    44  	d := make([]float64, count)
    45  	for idx, iv := range s {
    46  		d[idx] = float64(iv)
    47  	}
    48  	return d
    49  }
    50  
    51  // ParseFloat64 字符串转float64
    52  // 任意组合的nan字符串都会被解析成NaN
    53  func ParseFloat64(s string, v any) float64 {
    54  	defer func() {
    55  		// 解析失败以后输出日志, 以备检查
    56  		if err := recover(); err != nil {
    57  			logger.Errorf("ParseFloat64 %+v, error=%+v\n", v, err)
    58  		}
    59  	}()
    60  	if IsEmpty(s) {
    61  		return __nilToFloat64
    62  	}
    63  	if StringIsTrue(s) {
    64  		return StringTrue2Float64
    65  	} else if StringIsFalse(s) {
    66  		return StringFalse2Float64
    67  	}
    68  	f, err := strconv.ParseFloat(s, 64)
    69  	if err == nil {
    70  		return f
    71  	}
    72  	if IgnoreParseExceptions {
    73  		return __nilToFloat64
    74  	}
    75  	_ = v.(float64) // Intentionally panic
    76  	return __nilToFloat64
    77  }
    78  
    79  func AnyToFloat64(v any) float64 {
    80  	if vv, ok := ExtractValueFromPointer(v); ok {
    81  		v = vv
    82  	}
    83  
    84  	f := valueToNumber(v, __nilToFloat64, BoolToFloat64, ParseFloat64)
    85  	return f
    86  }
    87  
    88  // SliceToFloat64 any输入只能是一维slice或者数组
    89  func SliceToFloat64(v any) []float64 {
    90  	switch values := v.(type) {
    91  	case []int8:
    92  		return sliceNumberToFloat64(values)
    93  	case []uint8:
    94  		return sliceNumberToFloat64(values)
    95  	case []int16:
    96  		return sliceNumberToFloat64(values)
    97  	case []uint16:
    98  		return sliceNumberToFloat64(values)
    99  	case []int32: // 加速
   100  		return x64.FromInt32(values)
   101  	case []uint32:
   102  		return sliceNumberToFloat64(values)
   103  	case []int64: // 加速
   104  		return x64.FromInt64(values)
   105  	case []uint64:
   106  		return sliceNumberToFloat64(values)
   107  	case []int:
   108  		return sliceNumberToFloat64(values)
   109  	case []uint:
   110  		return sliceNumberToFloat64(values)
   111  	case []float32: // 加速
   112  		return x64.FromFloat32(values)
   113  	case []float64: // 克隆
   114  		//return slices.Clone(values)
   115  		return values
   116  	case []bool:
   117  		count := len(values)
   118  		if count == 0 {
   119  			return []float64{}
   120  		}
   121  		// 加速
   122  		return x64.FromBool(values)
   123  	case []string:
   124  		count := len(values)
   125  		if count == 0 {
   126  			return []float64{}
   127  		}
   128  		vs := make([]float64, count)
   129  		for idx, iv := range values {
   130  			vs[idx] = AnyToFloat64(iv)
   131  		}
   132  		return vs
   133  	default:
   134  		panic(TypeError(values))
   135  	}
   136  	return []float64{}
   137  }