github.com/joomcode/cue@v0.4.4-0.20221111115225-539fe3512047/pkg/math/math.go (about)

     1  // Copyright 2018 The CUE Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // Copyright 2018 The Go Authors. All rights reserved.
    16  // Use of this source code is governed by a BSD-style
    17  // license that can be found in the LICENSE file.
    18  
    19  package math
    20  
    21  import (
    22  	"math"
    23  
    24  	"github.com/cockroachdb/apd/v2"
    25  
    26  	"github.com/joomcode/cue/internal"
    27  )
    28  
    29  var apdContext = apd.BaseContext.WithPrecision(24)
    30  
    31  // Abs returns the absolute value of x.
    32  //
    33  // Special case: Abs(±Inf) = +Inf
    34  func Abs(x *internal.Decimal) (*internal.Decimal, error) {
    35  	var d internal.Decimal
    36  	_, err := apdContext.Abs(&d, x)
    37  	return &d, err
    38  }
    39  
    40  // Acosh returns the inverse hyperbolic cosine of x.
    41  //
    42  // Special cases are:
    43  //	Acosh(+Inf) = +Inf
    44  //	Acosh(x) = NaN if x < 1
    45  //	Acosh(NaN) = NaN
    46  func Acosh(x float64) float64 {
    47  	return math.Acosh(x)
    48  }
    49  
    50  // Asin returns the arcsine, in radians, of x.
    51  //
    52  // Special cases are:
    53  //	Asin(±0) = ±0
    54  //	Asin(x) = NaN if x < -1 or x > 1
    55  func Asin(x float64) float64 {
    56  	return math.Asin(x)
    57  }
    58  
    59  // Acos returns the arccosine, in radians, of x.
    60  //
    61  // Special case is:
    62  //	Acos(x) = NaN if x < -1 or x > 1
    63  func Acos(x float64) float64 {
    64  	return math.Acos(x)
    65  }
    66  
    67  // Asinh returns the inverse hyperbolic sine of x.
    68  //
    69  // Special cases are:
    70  //	Asinh(±0) = ±0
    71  //	Asinh(±Inf) = ±Inf
    72  //	Asinh(NaN) = NaN
    73  func Asinh(x float64) float64 {
    74  	return math.Asinh(x)
    75  }
    76  
    77  // Atan returns the arctangent, in radians, of x.
    78  //
    79  // Special cases are:
    80  //      Atan(±0) = ±0
    81  //      Atan(±Inf) = ±Pi/2
    82  func Atan(x float64) float64 {
    83  	return math.Atan(x)
    84  }
    85  
    86  // Atan2 returns the arc tangent of y/x, using
    87  // the signs of the two to determine the quadrant
    88  // of the return value.
    89  //
    90  // Special cases are (in order):
    91  //	Atan2(y, NaN) = NaN
    92  //	Atan2(NaN, x) = NaN
    93  //	Atan2(+0, x>=0) = +0
    94  //	Atan2(-0, x>=0) = -0
    95  //	Atan2(+0, x<=-0) = +Pi
    96  //	Atan2(-0, x<=-0) = -Pi
    97  //	Atan2(y>0, 0) = +Pi/2
    98  //	Atan2(y<0, 0) = -Pi/2
    99  //	Atan2(+Inf, +Inf) = +Pi/4
   100  //	Atan2(-Inf, +Inf) = -Pi/4
   101  //	Atan2(+Inf, -Inf) = 3Pi/4
   102  //	Atan2(-Inf, -Inf) = -3Pi/4
   103  //	Atan2(y, +Inf) = 0
   104  //	Atan2(y>0, -Inf) = +Pi
   105  //	Atan2(y<0, -Inf) = -Pi
   106  //	Atan2(+Inf, x) = +Pi/2
   107  //	Atan2(-Inf, x) = -Pi/2
   108  func Atan2(y, x float64) float64 {
   109  	return math.Atan2(y, x)
   110  }
   111  
   112  // Atanh returns the inverse hyperbolic tangent of x.
   113  //
   114  // Special cases are:
   115  //	Atanh(1) = +Inf
   116  //	Atanh(±0) = ±0
   117  //	Atanh(-1) = -Inf
   118  //	Atanh(x) = NaN if x < -1 or x > 1
   119  //	Atanh(NaN) = NaN
   120  func Atanh(x float64) float64 {
   121  	return math.Atanh(x)
   122  }
   123  
   124  // Cbrt returns the cube root of x.
   125  //
   126  // Special cases are:
   127  //	Cbrt(±0) = ±0
   128  //	Cbrt(±Inf) = ±Inf
   129  //	Cbrt(NaN) = NaN
   130  func Cbrt(x *internal.Decimal) (*internal.Decimal, error) {
   131  	var d internal.Decimal
   132  	_, err := apdContext.Cbrt(&d, x)
   133  	return &d, err
   134  }
   135  
   136  // Mathematical constants.
   137  const (
   138  	E   = 2.71828182845904523536028747135266249775724709369995957496696763 // https://oeis.org/A001113
   139  	Pi  = 3.14159265358979323846264338327950288419716939937510582097494459 // https://oeis.org/A000796
   140  	Phi = 1.61803398874989484820458683436563811772030917980576286213544862 // https://oeis.org/A001622
   141  
   142  	Sqrt2   = 1.41421356237309504880168872420969807856967187537694807317667974 // https://oeis.org/A002193
   143  	SqrtE   = 1.64872127070012814684865078781416357165377610071014801157507931 // https://oeis.org/A019774
   144  	SqrtPi  = 1.77245385090551602729816748334114518279754945612238712821380779 // https://oeis.org/A002161
   145  	SqrtPhi = 1.27201964951406896425242246173749149171560804184009624861664038 // https://oeis.org/A139339
   146  
   147  	Ln2    = 0.693147180559945309417232121458176568075500134360255254120680009 // https://oeis.org/A002162
   148  	Log2E  = 1000000000000000000000000000000000000000000000000000000000000000 / 693147180559945309417232121458176568075500134360255254120680009
   149  	Ln10   = 2.30258509299404568401799145468436420760110148862877297603332790 // https://oeis.org/A002392
   150  	Log10E = 10000000000000000000000000000000000000000000000000000000000000 / 23025850929940456840179914546843642076011014886287729760333279
   151  )
   152  
   153  // Copysign returns a value with the magnitude
   154  // of x and the sign of y.
   155  func Copysign(x, y *internal.Decimal) *internal.Decimal {
   156  	var d internal.Decimal
   157  	d.Set(x)
   158  	d.Negative = y.Negative
   159  	return &d
   160  }
   161  
   162  var zero = apd.New(0, 0)
   163  
   164  // Dim returns the maximum of x-y or 0.
   165  //
   166  // Special cases are:
   167  //	Dim(+Inf, +Inf) = NaN
   168  //	Dim(-Inf, -Inf) = NaN
   169  //	Dim(x, NaN) = Dim(NaN, x) = NaN
   170  func Dim(x, y *internal.Decimal) (*internal.Decimal, error) {
   171  	var d internal.Decimal
   172  	_, err := apdContext.Sub(&d, x, y)
   173  	if err != nil {
   174  		return nil, err
   175  	}
   176  	if d.Negative {
   177  		return zero, nil
   178  	}
   179  	return &d, nil
   180  }
   181  
   182  // Erf returns the error function of x.
   183  //
   184  // Special cases are:
   185  //	Erf(+Inf) = 1
   186  //	Erf(-Inf) = -1
   187  //	Erf(NaN) = NaN
   188  func Erf(x float64) float64 {
   189  	return math.Erf(x)
   190  }
   191  
   192  // Erfc returns the complementary error function of x.
   193  //
   194  // Special cases are:
   195  //	Erfc(+Inf) = 0
   196  //	Erfc(-Inf) = 2
   197  //	Erfc(NaN) = NaN
   198  func Erfc(x float64) float64 {
   199  	return math.Erfc(x)
   200  }
   201  
   202  // Erfinv returns the inverse error function of x.
   203  //
   204  // Special cases are:
   205  //	Erfinv(1) = +Inf
   206  //	Erfinv(-1) = -Inf
   207  //	Erfinv(x) = NaN if x < -1 or x > 1
   208  //	Erfinv(NaN) = NaN
   209  func Erfinv(x float64) float64 {
   210  	return math.Erfinv(x)
   211  }
   212  
   213  // Erfcinv returns the inverse of Erfc(x).
   214  //
   215  // Special cases are:
   216  //	Erfcinv(0) = +Inf
   217  //	Erfcinv(2) = -Inf
   218  //	Erfcinv(x) = NaN if x < 0 or x > 2
   219  //	Erfcinv(NaN) = NaN
   220  func Erfcinv(x float64) float64 {
   221  	return math.Erfcinv(x)
   222  }
   223  
   224  // Exp returns e**x, the base-e exponential of x.
   225  //
   226  // Special cases are:
   227  //	Exp(+Inf) = +Inf
   228  //	Exp(NaN) = NaN
   229  // Very large values overflow to 0 or +Inf.
   230  // Very small values underflow to 1.
   231  func Exp(x *internal.Decimal) (*internal.Decimal, error) {
   232  	var d internal.Decimal
   233  	_, err := apdContext.Exp(&d, x)
   234  	return &d, err
   235  }
   236  
   237  var two = apd.New(2, 0)
   238  
   239  // Exp2 returns 2**x, the base-2 exponential of x.
   240  //
   241  // Special cases are the same as Exp.
   242  func Exp2(x *internal.Decimal) (*internal.Decimal, error) {
   243  	var d internal.Decimal
   244  	_, err := apdContext.Pow(&d, two, x)
   245  	return &d, err
   246  }
   247  
   248  // Expm1 returns e**x - 1, the base-e exponential of x minus 1.
   249  // It is more accurate than Exp(x) - 1 when x is near zero.
   250  //
   251  // Special cases are:
   252  //	Expm1(+Inf) = +Inf
   253  //	Expm1(-Inf) = -1
   254  //	Expm1(NaN) = NaN
   255  // Very large values overflow to -1 or +Inf.
   256  func Expm1(x float64) float64 {
   257  	return math.Expm1(x)
   258  }
   259  
   260  // Gamma returns the Gamma function of x.
   261  //
   262  // Special cases are:
   263  //	Gamma(+Inf) = +Inf
   264  //	Gamma(+0) = +Inf
   265  //	Gamma(-0) = -Inf
   266  //	Gamma(x) = NaN for integer x < 0
   267  //	Gamma(-Inf) = NaN
   268  //	Gamma(NaN) = NaN
   269  func Gamma(x float64) float64 {
   270  	return math.Gamma(x)
   271  }
   272  
   273  // Hypot returns Sqrt(p*p + q*q), taking care to avoid
   274  // unnecessary overflow and underflow.
   275  //
   276  // Special cases are:
   277  //	Hypot(±Inf, q) = +Inf
   278  //	Hypot(p, ±Inf) = +Inf
   279  //	Hypot(NaN, q) = NaN
   280  //	Hypot(p, NaN) = NaN
   281  func Hypot(p, q float64) float64 {
   282  	return math.Hypot(p, q)
   283  }
   284  
   285  // J0 returns the order-zero Bessel function of the first kind.
   286  //
   287  // Special cases are:
   288  //	J0(±Inf) = 0
   289  //	J0(0) = 1
   290  //	J0(NaN) = NaN
   291  func J0(x float64) float64 {
   292  	return math.J0(x)
   293  }
   294  
   295  // Y0 returns the order-zero Bessel function of the second kind.
   296  //
   297  // Special cases are:
   298  //	Y0(+Inf) = 0
   299  //	Y0(0) = -Inf
   300  //	Y0(x < 0) = NaN
   301  //	Y0(NaN) = NaN
   302  func Y0(x float64) float64 {
   303  	return math.Y0(x)
   304  }
   305  
   306  // J1 returns the order-one Bessel function of the first kind.
   307  //
   308  // Special cases are:
   309  //	J1(±Inf) = 0
   310  //	J1(NaN) = NaN
   311  func J1(x float64) float64 {
   312  	return math.J1(x)
   313  }
   314  
   315  // Y1 returns the order-one Bessel function of the second kind.
   316  //
   317  // Special cases are:
   318  //	Y1(+Inf) = 0
   319  //	Y1(0) = -Inf
   320  //	Y1(x < 0) = NaN
   321  //	Y1(NaN) = NaN
   322  func Y1(x float64) float64 {
   323  	return math.Y1(x)
   324  }
   325  
   326  // Jn returns the order-n Bessel function of the first kind.
   327  //
   328  // Special cases are:
   329  //	Jn(n, ±Inf) = 0
   330  //	Jn(n, NaN) = NaN
   331  func Jn(n int, x float64) float64 {
   332  	return math.Jn(n, x)
   333  }
   334  
   335  // Yn returns the order-n Bessel function of the second kind.
   336  //
   337  // Special cases are:
   338  //	Yn(n, +Inf) = 0
   339  //	Yn(n ≥ 0, 0) = -Inf
   340  //	Yn(n < 0, 0) = +Inf if n is odd, -Inf if n is even
   341  //	Yn(n, x < 0) = NaN
   342  //	Yn(n, NaN) = NaN
   343  func Yn(n int, x float64) float64 {
   344  	return math.Yn(n, x)
   345  }
   346  
   347  // Ldexp is the inverse of Frexp.
   348  // It returns frac × 2**exp.
   349  //
   350  // Special cases are:
   351  //	Ldexp(±0, exp) = ±0
   352  //	Ldexp(±Inf, exp) = ±Inf
   353  //	Ldexp(NaN, exp) = NaN
   354  func Ldexp(frac float64, exp int) float64 {
   355  	return math.Ldexp(frac, exp)
   356  }
   357  
   358  // Log returns the natural logarithm of x.
   359  //
   360  // Special cases are:
   361  //	Log(+Inf) = +Inf
   362  //	Log(0) = -Inf
   363  //	Log(x < 0) = NaN
   364  //	Log(NaN) = NaN
   365  func Log(x *internal.Decimal) (*internal.Decimal, error) {
   366  	var d internal.Decimal
   367  	_, err := apdContext.Ln(&d, x)
   368  	return &d, err
   369  }
   370  
   371  // Log10 returns the decimal logarithm of x.
   372  // The special cases are the same as for Log.
   373  func Log10(x *internal.Decimal) (*internal.Decimal, error) {
   374  	var d internal.Decimal
   375  	_, err := apdContext.Log10(&d, x)
   376  	return &d, err
   377  }
   378  
   379  // Log2 returns the binary logarithm of x.
   380  // The special cases are the same as for Log.
   381  func Log2(x *internal.Decimal) (*internal.Decimal, error) {
   382  	var d, ln2 internal.Decimal
   383  	_, _ = apdContext.Ln(&ln2, two)
   384  	_, err := apdContext.Ln(&d, x)
   385  	if err != nil {
   386  		return &d, err
   387  	}
   388  	_, err = apdContext.Quo(&d, &d, &ln2)
   389  	return &d, err
   390  }
   391  
   392  // Log1p returns the natural logarithm of 1 plus its argument x.
   393  // It is more accurate than Log(1 + x) when x is near zero.
   394  //
   395  // Special cases are:
   396  //	Log1p(+Inf) = +Inf
   397  //	Log1p(±0) = ±0
   398  //	Log1p(-1) = -Inf
   399  //	Log1p(x < -1) = NaN
   400  //	Log1p(NaN) = NaN
   401  func Log1p(x float64) float64 {
   402  	return math.Log1p(x)
   403  }
   404  
   405  // Logb returns the binary exponent of x.
   406  //
   407  // Special cases are:
   408  //	Logb(±Inf) = +Inf
   409  //	Logb(0) = -Inf
   410  //	Logb(NaN) = NaN
   411  func Logb(x float64) float64 {
   412  	return math.Logb(x)
   413  }
   414  
   415  // Ilogb returns the binary exponent of x as an integer.
   416  //
   417  // Special cases are:
   418  //	Ilogb(±Inf) = MaxInt32
   419  //	Ilogb(0) = MinInt32
   420  //	Ilogb(NaN) = MaxInt32
   421  func Ilogb(x float64) int {
   422  	return math.Ilogb(x)
   423  }
   424  
   425  // Mod returns the floating-point remainder of x/y.
   426  // The magnitude of the result is less than y and its
   427  // sign agrees with that of x.
   428  //
   429  // Special cases are:
   430  //	Mod(±Inf, y) = NaN
   431  //	Mod(NaN, y) = NaN
   432  //	Mod(x, 0) = NaN
   433  //	Mod(x, ±Inf) = x
   434  //	Mod(x, NaN) = NaN
   435  func Mod(x, y float64) float64 {
   436  	return math.Mod(x, y)
   437  }
   438  
   439  // Pow returns x**y, the base-x exponential of y.
   440  //
   441  // Special cases are (in order):
   442  //	Pow(x, ±0) = 1 for any x
   443  //	Pow(1, y) = 1 for any y
   444  //	Pow(x, 1) = x for any x
   445  //	Pow(NaN, y) = NaN
   446  //	Pow(x, NaN) = NaN
   447  //	Pow(±0, y) = ±Inf for y an odd integer < 0
   448  //	Pow(±0, -Inf) = +Inf
   449  //	Pow(±0, +Inf) = +0
   450  //	Pow(±0, y) = +Inf for finite y < 0 and not an odd integer
   451  //	Pow(±0, y) = ±0 for y an odd integer > 0
   452  //	Pow(±0, y) = +0 for finite y > 0 and not an odd integer
   453  //	Pow(-1, ±Inf) = 1
   454  //	Pow(x, +Inf) = +Inf for |x| > 1
   455  //	Pow(x, -Inf) = +0 for |x| > 1
   456  //	Pow(x, +Inf) = +0 for |x| < 1
   457  //	Pow(x, -Inf) = +Inf for |x| < 1
   458  //	Pow(+Inf, y) = +Inf for y > 0
   459  //	Pow(+Inf, y) = +0 for y < 0
   460  //	Pow(-Inf, y) = Pow(-0, -y)
   461  //	Pow(x, y) = NaN for finite x < 0 and finite non-integer y
   462  func Pow(x, y *internal.Decimal) (*internal.Decimal, error) {
   463  	var d internal.Decimal
   464  	_, err := apdContext.Pow(&d, x, y)
   465  	return &d, err
   466  }
   467  
   468  // Pow10 returns 10**n, the base-10 exponential of n.
   469  //
   470  func Pow10(n int32) *internal.Decimal {
   471  	return apd.New(1, n)
   472  }
   473  
   474  // Remainder returns the IEEE 754 floating-point remainder of x/y.
   475  //
   476  // Special cases are:
   477  //	Remainder(±Inf, y) = NaN
   478  //	Remainder(NaN, y) = NaN
   479  //	Remainder(x, 0) = NaN
   480  //	Remainder(x, ±Inf) = x
   481  //	Remainder(x, NaN) = NaN
   482  func Remainder(x, y float64) float64 {
   483  	return math.Remainder(x, y)
   484  }
   485  
   486  // Signbit reports whether x is negative or negative zero.
   487  func Signbit(x *internal.Decimal) bool {
   488  	return x.Negative
   489  }
   490  
   491  // Cos returns the cosine of the radian argument x.
   492  //
   493  // Special cases are:
   494  //	Cos(±Inf) = NaN
   495  //	Cos(NaN) = NaN
   496  func Cos(x float64) float64 {
   497  	return math.Cos(x)
   498  }
   499  
   500  // Sin returns the sine of the radian argument x.
   501  //
   502  // Special cases are:
   503  //	Sin(±0) = ±0
   504  //	Sin(±Inf) = NaN
   505  //	Sin(NaN) = NaN
   506  func Sin(x float64) float64 {
   507  	return math.Sin(x)
   508  }
   509  
   510  // Sinh returns the hyperbolic sine of x.
   511  //
   512  // Special cases are:
   513  //	Sinh(±0) = ±0
   514  //	Sinh(±Inf) = ±Inf
   515  //	Sinh(NaN) = NaN
   516  func Sinh(x float64) float64 {
   517  	return math.Sinh(x)
   518  }
   519  
   520  // Cosh returns the hyperbolic cosine of x.
   521  //
   522  // Special cases are:
   523  //	Cosh(±0) = 1
   524  //	Cosh(±Inf) = +Inf
   525  //	Cosh(NaN) = NaN
   526  func Cosh(x float64) float64 {
   527  	return math.Cosh(x)
   528  }
   529  
   530  // Sqrt returns the square root of x.
   531  //
   532  // Special cases are:
   533  //	Sqrt(+Inf) = +Inf
   534  //	Sqrt(±0) = ±0
   535  //	Sqrt(x < 0) = NaN
   536  //	Sqrt(NaN) = NaN
   537  func Sqrt(x float64) float64 {
   538  	return math.Sqrt(x)
   539  }
   540  
   541  // Tan returns the tangent of the radian argument x.
   542  //
   543  // Special cases are:
   544  //	Tan(±0) = ±0
   545  //	Tan(±Inf) = NaN
   546  //	Tan(NaN) = NaN
   547  func Tan(x float64) float64 {
   548  	return math.Tan(x)
   549  }
   550  
   551  // Tanh returns the hyperbolic tangent of x.
   552  //
   553  // Special cases are:
   554  //	Tanh(±0) = ±0
   555  //	Tanh(±Inf) = ±1
   556  //	Tanh(NaN) = NaN
   557  func Tanh(x float64) float64 {
   558  	return math.Tanh(x)
   559  }