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