Skip to content

Commit

Permalink
add missing docstrings; fix 1/x roundoff bugs in tests; disambiguate …
Browse files Browse the repository at this point in the history
…1/x in code; go fmt codebase
  • Loading branch information
soypat committed Dec 20, 2024
1 parent 7ba2c08 commit 912ef0b
Show file tree
Hide file tree
Showing 43 changed files with 209 additions and 37 deletions.
3 changes: 2 additions & 1 deletion abs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package math32
// Abs returns the absolute value of x.
//
// Special cases are:
//
// Abs(±Inf) = +Inf
// Abs(NaN) = NaN
func Abs(x float32) float32 {
return Float32frombits(Float32bits(x) &^ (1 << 31))
return Float32frombits(Float32bits(x) &^ signMask)
}
1 change: 1 addition & 0 deletions acos.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package math32
// Acos returns the arccosine, in radians, of x.
//
// Special case is:
//
// Acos(x) = NaN if x < -1 or x > 1
func Acos(x float32) float32 {
return Pi/2 - Asin(x)
Expand Down
3 changes: 2 additions & 1 deletion acosh.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ package math32
// Acosh returns the inverse hyperbolic cosine of x.
//
// Special cases are:
//
// Acosh(+Inf) = +Inf
// Acosh(x) = NaN if x < 1
// Acosh(NaN) = NaN
Expand All @@ -46,7 +47,7 @@ func Acosh(x float32) float32 {
case x >= Large:
return Log(x) + Ln2 // x > 2**28
case x > 2:
return Log(2*x - 1/(x+Sqrt(x*x-1))) // 2**28 > x > 2
return Log(2*x - 1./(x+Sqrt(x*x-1))) // 2**28 > x > 2
}
t := x - 1
return Log1p(t + Sqrt(2*t+t*t)) // 2 >= x > 1
Expand Down
8 changes: 4 additions & 4 deletions all_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1410,8 +1410,8 @@ var vfpowSC = [][2]float32{
{-1, Inf(-1)},
{-1, Inf(1)},
{-1, NaN()},
{-1 / 2, Inf(-1)},
{-1 / 2, Inf(1)},
{-1. / 2, Inf(-1)},
{-1. / 2, Inf(1)},
{Copysign(0, -1), Inf(-1)},
{Copysign(0, -1), -Pi},
{Copysign(0, -1), -3},
Expand All @@ -1429,8 +1429,8 @@ var vfpowSC = [][2]float32{
{0, Inf(1)},
{0, NaN()},

{1 / 2, Inf(-1)},
{1 / 2, Inf(1)},
{1. / 2, Inf(-1)},
{1. / 2, Inf(1)},
{1, Inf(-1)},
{1, Inf(1)},
{1, NaN()},
Expand Down
3 changes: 2 additions & 1 deletion asinh.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ package math32
// Asinh returns the inverse hyperbolic sine of x.
//
// Special cases are:
//
// Asinh(±0) = ±0
// Asinh(±Inf) = ±Inf
// Asinh(NaN) = NaN
Expand All @@ -52,7 +53,7 @@ func Asinh(x float32) float32 {
case x > Large:
temp = Log(x) + Ln2 // |x| > 2**28
case x > 2:
temp = Log(2*x + 1/(Sqrt(x*x+1)+x)) // 2**28 > |x| > 2.0
temp = Log(2*x + 1./(Sqrt(x*x+1)+x)) // 2**28 > |x| > 2.0
case x < NearZero:
temp = x // |x| < 2**-28
default:
Expand Down
2 changes: 1 addition & 1 deletion atan.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func satan(x float32) float32 {
return xatan(x)
}
if x > Tan3pio8 {
return Pi/2 - xatan(1/x) + Morebits
return Pi/2 - xatan(1./x) + Morebits
}
return Pi/4 + xatan((x-1)/(x+1)) + 0.5*Morebits
}
35 changes: 18 additions & 17 deletions atan2.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,24 @@ package math32

// Atan2 returns the arc tangent of y/x, using the signs of the two to determine the quadrant of the return value.
// Special cases are (in order):
// Atan2(y, NaN) = NaN
// Atan2(NaN, x) = NaN
// Atan2(+0, x>=0) = +0
// Atan2(-0, x>=0) = -0
// Atan2(+0, x<=-0) = +Pi
// Atan2(-0, x<=-0) = -Pi
// Atan2(y>0, 0) = +Pi/2
// Atan2(y<0, 0) = -Pi/2
// Atan2(+Inf, +Inf) = +Pi/4
// Atan2(-Inf, +Inf) = -Pi/4
// Atan2(+Inf, -Inf) = 3Pi/4
// Atan2(-Inf, -Inf) = -3Pi/4
// Atan2(y, +Inf) = 0
// Atan2(y>0, -Inf) = +Pi
// Atan2(y<0, -Inf) = -Pi
// Atan2(+Inf, x) = +Pi/2
// Atan2(-Inf, x) = -Pi/2
//
// Atan2(y, NaN) = NaN
// Atan2(NaN, x) = NaN
// Atan2(+0, x>=0) = +0
// Atan2(-0, x>=0) = -0
// Atan2(+0, x<=-0) = +Pi
// Atan2(-0, x<=-0) = -Pi
// Atan2(y>0, 0) = +Pi/2
// Atan2(y<0, 0) = -Pi/2
// Atan2(+Inf, +Inf) = +Pi/4
// Atan2(-Inf, +Inf) = -Pi/4
// Atan2(+Inf, -Inf) = 3Pi/4
// Atan2(-Inf, -Inf) = -3Pi/4
// Atan2(y, +Inf) = 0
// Atan2(y>0, -Inf) = +Pi
// Atan2(y<0, -Inf) = -Pi
// Atan2(+Inf, x) = +Pi/2
// Atan2(-Inf, x) = -Pi/2
func Atan2(y, x float32) float32 {
// special cases
switch {
Expand Down
1 change: 1 addition & 0 deletions atanh.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ package math32
// Atanh returns the inverse hyperbolic tangent of x.
//
// Special cases are:
//
// Atanh(1) = +Inf
// Atanh(±0) = ±0
// Atanh(-1) = -Inf
Expand Down
4 changes: 2 additions & 2 deletions bits.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ func Inf(sign int) float32 {
return Float32frombits(v)
}

// NaN returns an IEEE 754 ``not-a-number'' value.
// NaN returns an IEEE 754 not-a-number value.
func NaN() float32 { return Float32frombits(uvnan) }

// IsNaN reports whether f is an IEEE 754 ``not-a-number'' value.
// IsNaN reports whether f is an IEEE 754 not-a-number value.
func IsNaN(f float32) (is bool) {
// IEEE 754 says that only NaNs satisfy f != f.
// To avoid the floating-point hardware, could use:
Expand Down
7 changes: 7 additions & 0 deletions cbrt.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ package math32

import "math"

// Cbrt returns the cube root of x.
//
// Special cases are:
//
// Cbrt(±0) = ±0
// Cbrt(±Inf) = ±Inf
// Cbrt(NaN) = NaN
func Cbrt(x float32) float32 {
return float32(math.Cbrt(float64(x)))
}
1 change: 1 addition & 0 deletions copysign.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package math32

import "math"

// Copysign returns a value with the magnitude of f and the sign of sign.
func Copysign(x, y float32) float32 {
const sign = 1 << 31
return math.Float32frombits(math.Float32bits(x)&^sign | math.Float32bits(y)&sign)
Expand Down
2 changes: 1 addition & 1 deletion cosh.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ func Cosh(x float32) float32 {
return Exp(x) * 0.5
}
ex := Exp(x)
return (ex + 1/ex) * 0.5
return (ex + 1./ex) * 0.5
}
2 changes: 1 addition & 1 deletion doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Package math32 provides basic constants and mathematical functions for float32 types.
At its core, it's mostly just a wrapper in form of float32(math.XXX). This applies to the following functions:
Acos
Acosh
Asin
Expand Down Expand Up @@ -29,6 +30,5 @@ At its core, it's mostly just a wrapper in form of float32(math.XXX). This appli
Y1
Everything else is a float32 implementation. Implementation schedule is sporadic an uncertain. But eventually all functions will be replaced
*/
package math32
7 changes: 7 additions & 0 deletions erf.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ package math32

import "math"

// Erf returns the error function of x.
//
// Special cases are:
//
// Erf(+Inf) = 1
// Erf(-Inf) = -1
// Erf(NaN) = NaN
func Erf(x float32) float32 {
return float32(math.Erf(float64(x)))
}
7 changes: 7 additions & 0 deletions erfc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ package math32

import "math"

// Erfc returns the complementary error function of x.
//
// Special cases are:
//
// Erfc(+Inf) = 0
// Erfc(-Inf) = 2
// Erfc(NaN) = NaN
func Erfc(x float32) float32 {
return float32(math.Erfc(float64(x)))
}
9 changes: 9 additions & 0 deletions exp.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
package math32

// Exp returns e**x, the base-e exponential of x.
//
// Special cases are:
//
// Exp(+Inf) = +Inf
// Exp(NaN) = NaN
//
// Very large values overflow to 0 or +Inf.
// Very small values underflow to 1.
func Exp(x float32) float32 {
if haveArchExp {
return archExp(x)
Expand Down
2 changes: 2 additions & 0 deletions expm1f.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,11 @@ package math32
// It is more accurate than Exp(x) - 1 when x is near zero.
//
// Special cases are:
//
// Expm1(+Inf) = +Inf
// Expm1(-Inf) = -1
// Expm1(NaN) = NaN
//
// Very large values overflow to -1 or +Inf.
func Expm1(x float32) float32 {
return expm1(x)
Expand Down
3 changes: 3 additions & 0 deletions floor.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package math32
// Floor returns the greatest integer value less than or equal to x.
//
// Special cases are:
//
// Floor(±0) = ±0
// Floor(±Inf) = ±Inf
// Floor(NaN) = NaN
Expand All @@ -28,6 +29,7 @@ func floor(x float32) float32 {
// Ceil returns the least integer value greater than or equal to x.
//
// Special cases are:
//
// Ceil(±0) = ±0
// Ceil(±Inf) = ±Inf
// Ceil(NaN) = NaN
Expand All @@ -42,6 +44,7 @@ func ceil(x float32) float32 {
// Trunc returns the integer value of x.
//
// Special cases are:
//
// Trunc(±0) = ±0
// Trunc(±Inf) = ±Inf
// Trunc(NaN) = NaN
Expand Down
1 change: 1 addition & 0 deletions frexp.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package math32
// with the absolute value of frac in the interval [½, 1).
//
// Special cases are:
//
// Frexp(±0) = ±0, 0
// Frexp(±Inf) = ±Inf, 0
// Frexp(NaN) = NaN, 0
Expand Down
10 changes: 10 additions & 0 deletions gamma.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@ package math32

import "math"

// Gamma returns the Gamma function of x.
//
// Special cases are:
//
// Gamma(+Inf) = +Inf
// Gamma(+0) = +Inf
// Gamma(-0) = -Inf
// Gamma(x) = NaN for integer x < 0
// Gamma(-Inf) = NaN
// Gamma(NaN) = NaN
func Gamma(x float32) float32 {
return float32(math.Gamma(float64(x)))
}
1 change: 1 addition & 0 deletions hypot.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package math32
// unnecessary overflow and underflow.
//
// Special cases are:
//
// Hypot(±Inf, q) = +Inf
// Hypot(p, ±Inf) = +Inf
// Hypot(NaN, q) = NaN
Expand Down
7 changes: 7 additions & 0 deletions j0.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ package math32

import "math"

// J0 returns the order-zero Bessel function of the first kind.
//
// Special cases are:
//
// J0(±Inf) = 0
// J0(0) = 1
// J0(NaN) = NaN
func J0(x float32) float32 {
return float32(math.J0(float64(x)))
}
6 changes: 6 additions & 0 deletions j1.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ package math32

import "math"

// J1 returns the order-one Bessel function of the first kind.
//
// Special cases are:
//
// J1(±Inf) = 0
// J1(NaN) = NaN
func J1(x float32) float32 {
return float32(math.J1(float64(x)))
}
15 changes: 15 additions & 0 deletions jn.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,25 @@ package math32

import "math"

// Jn returns the order-n Bessel function of the first kind.
//
// Special cases are:
//
// Jn(n, ±Inf) = 0
// Jn(n, NaN) = NaN
func Jn(n int, x float32) float32 {
return float32(math.Jn(n, float64(x)))
}

// Yn returns the order-n Bessel function of the second kind.
//
// Special cases are:
//
// Yn(n, +Inf) = 0
// Yn(n ≥ 0, 0) = -Inf
// Yn(n < 0, 0) = +Inf if n is odd, -Inf if n is even
// Yn(n, x < 0) = NaN
// Yn(n, NaN) = NaN
func Yn(n int, x float32) float32 {
return float32(math.Yn(n, float64(x)))
}
1 change: 1 addition & 0 deletions ldexp.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package math32
// It returns frac × 2**exp.
//
// Special cases are:
//
// Ldexp(±0, exp) = ±0
// Ldexp(±Inf, exp) = ±Inf
// Ldexp(NaN, exp) = NaN
Expand Down
9 changes: 9 additions & 0 deletions lgamma.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ package math32

import "math"

// Lgamma returns the natural logarithm and sign (-1 or +1) of [Gamma](x).
//
// Special cases are:
//
// Lgamma(+Inf) = +Inf
// Lgamma(0) = +Inf
// Lgamma(-integer) = +Inf
// Lgamma(-Inf) = -Inf
// Lgamma(NaN) = NaN
func Lgamma(x float32) (lgamma float32, sign int) {
lg, sign := math.Lgamma(float64(x))
return float32(lg), sign
Expand Down
1 change: 1 addition & 0 deletions log.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ package math32
// Log returns the natural logarithm of x.
//
// Special cases are:
//
// Log(+Inf) = +Inf
// Log(0) = -Inf
// Log(x < 0) = NaN
Expand Down
2 changes: 2 additions & 0 deletions log10.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package math32

import "math"

// Log10 returns the decimal logarithm of x.
// The special cases are the same as for [Log].
func Log10(x float32) float32 {
return float32(math.Log10(float64(x)))
}
Loading

0 comments on commit 912ef0b

Please sign in to comment.