github.com/goki/ki@v1.1.17/floats/floats.go (about) 1 // Copyright (c) 2018, The GoKi Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 /* 6 package floats provides a standard Floater interface and all the std math functions 7 8 defined on Floater types. Furthermore, fully generic slice sort and 9 conversion methods in the kit type kit package attempt to use this interface, 10 before falling back on reflection. If you have a struct that can be converted 11 into an float64, then this is the only way to allow it to be sorted using those 12 generic functions, as the reflect.Kind fallback will fail. 13 */ 14 package floats 15 16 import "math" 17 18 // Floater converts a type from a float64, used in kit.ToFloat function and in 19 // sorting comparisons -- tried first in sorting 20 type Floater interface { 21 Float() float64 22 } 23 24 // FloatSetter is Floater that also supports setting the value from a float64. 25 // Satisfying this interface requires a pointer to the underlying type. 26 type FloatSetter interface { 27 Floater 28 FromFloat(val float64) 29 } 30 31 /////////////////////////////////////////////////////// 32 // math wrappers 33 34 func Abs(x Floater) float64 { 35 return math.Abs(x.Float()) 36 } 37 func Acos(x Floater) float64 { 38 return math.Acos(x.Float()) 39 } 40 func Acosh(x Floater) float64 { 41 return math.Acosh(x.Float()) 42 } 43 func Asin(x Floater) float64 { 44 return math.Asin(x.Float()) 45 } 46 func Asinh(x Floater) float64 { 47 return math.Asinh(x.Float()) 48 } 49 func Atan(x Floater) float64 { 50 return math.Atan(x.Float()) 51 } 52 func Atan2(y, x Floater) float64 { 53 return math.Atan2(x.Float(), y.Float()) 54 } 55 func Atanh(x Floater) float64 { 56 return math.Atanh(x.Float()) 57 } 58 func Cbrt(x Floater) float64 { 59 return math.Cbrt(x.Float()) 60 } 61 func Ceil(x Floater) float64 { 62 return math.Ceil(x.Float()) 63 } 64 func Copysign(x, y Floater) float64 { 65 return math.Copysign(x.Float(), y.Float()) 66 } 67 func Cos(x Floater) float64 { 68 return math.Cos(x.Float()) 69 } 70 func Cosh(x Floater) float64 { 71 return math.Cosh(x.Float()) 72 } 73 func Dim(x, y Floater) float64 { 74 return math.Dim(x.Float(), y.Float()) 75 } 76 func Erf(x Floater) float64 { 77 return math.Erf(x.Float()) 78 } 79 func Erfc(x Floater) float64 { 80 return math.Erfc(x.Float()) 81 } 82 func Erfcinv(x Floater) float64 { 83 return math.Erfcinv(x.Float()) 84 } 85 func Erfinv(x Floater) float64 { 86 return math.Erfinv(x.Float()) 87 } 88 func Exp(x Floater) float64 { 89 return math.Exp(x.Float()) 90 } 91 func Exp2(x Floater) float64 { 92 return math.Exp2(x.Float()) 93 } 94 func Expm1(x Floater) float64 { 95 return math.Expm1(x.Float()) 96 } 97 func Floor(x Floater) float64 { 98 return math.Floor(x.Float()) 99 } 100 func Frexp(f Floater) (frac float64, exp int) { 101 return math.Frexp(f.Float()) 102 } 103 func Gamma(x Floater) float64 { 104 return math.Gamma(x.Float()) 105 } 106 func Hypot(p, q Floater) float64 { 107 return math.Hypot(p.Float(), q.Float()) 108 } 109 func Ilogb(x Floater) int { 110 return math.Ilogb(x.Float()) 111 } 112 func IsInf(f Floater, sign int) bool { 113 return math.IsInf(f.Float(), sign) 114 } 115 func IsNaN(f Floater) (is bool) { 116 return math.IsNaN(f.Float()) 117 } 118 func J0(x Floater) float64 { 119 return math.J0(x.Float()) 120 } 121 func J1(x Floater) float64 { 122 return math.J1(x.Float()) 123 } 124 func Jn(n int, x Floater) float64 { 125 return math.Jn(n, x.Float()) 126 } 127 func Ldexp(frac Floater, exp int) float64 { 128 return math.Ldexp(frac.Float(), exp) 129 } 130 func Lgamma(x Floater) (lgamma float64, sign int) { 131 return math.Lgamma(x.Float()) 132 } 133 func Log(x Floater) float64 { 134 return math.Log(x.Float()) 135 } 136 func Log10(x Floater) float64 { 137 return math.Log10(x.Float()) 138 } 139 func Log1p(x Floater) float64 { 140 return math.Log1p(x.Float()) 141 } 142 func Log2(x Floater) float64 { 143 return math.Log2(x.Float()) 144 } 145 func Logb(x Floater) float64 { 146 return math.Logb(x.Float()) 147 } 148 func Max(x, y Floater) float64 { 149 return math.Max(x.Float(), y.Float()) 150 } 151 func Min(x, y Floater) float64 { 152 return math.Min(x.Float(), y.Float()) 153 } 154 func Mod(x, y Floater) float64 { 155 return math.Mod(x.Float(), y.Float()) 156 } 157 func Modf(f Floater) (int float64, frac float64) { 158 return math.Modf(f.Float()) 159 } 160 func Nextafter(x, y Floater) (r float64) { 161 return math.Nextafter(x.Float(), y.Float()) 162 } 163 func Pow(x, y Floater) float64 { 164 return math.Pow(x.Float(), y.Float()) 165 } 166 func Remainder(x, y Floater) float64 { 167 return math.Remainder(x.Float(), y.Float()) 168 } 169 func Round(x Floater) float64 { 170 return math.Round(x.Float()) 171 } 172 func RoundToEven(x Floater) float64 { 173 return math.RoundToEven(x.Float()) 174 } 175 func Signbit(x Floater) bool { 176 return math.Signbit(x.Float()) 177 } 178 func Sin(x Floater) float64 { 179 return math.Sin(x.Float()) 180 } 181 func Sincos(x Floater) (sin, cos float64) { 182 return math.Sincos(x.Float()) 183 } 184 func Sinh(x Floater) float64 { 185 return math.Sinh(x.Float()) 186 } 187 func Sqrt(x Floater) float64 { 188 return math.Sqrt(x.Float()) 189 } 190 func Tan(x Floater) float64 { 191 return math.Tan(x.Float()) 192 } 193 func Tanh(x Floater) float64 { 194 return math.Tanh(x.Float()) 195 } 196 func Trunc(x Floater) float64 { 197 return math.Trunc(x.Float()) 198 } 199 func Y0(x Floater) float64 { 200 return math.Y0(x.Float()) 201 } 202 func Y1(x Floater) float64 { 203 return math.Y1(x.Float()) 204 } 205 func Yn(n int, x Floater) float64 { 206 return math.Yn(n, x.Float()) 207 }