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