Documentation
¶
Overview ¶
Package cmath is a pure-Go (no cgo) reimplementation of Ruby's CMath standard library — the complex-aware trigonometric and transcendental functions of MRI 4.0.5's `cmath` gem.
CMath accepts real or complex arguments and returns a real result when the input lies on the real branch of the function (so CMath.sqrt(4) is 2.0) and a complex result otherwise (CMath.sqrt(-4) is (0+2i)). This package mirrors that real-or-complex value model with the Number type and reproduces MRI's exact branch-cut decisions and float formulas, so it can back a Ruby runtime such as go-embedded-ruby without any interpreter.
The mapping to MRI is faithful to the reference implementation: every function here uses the same real-vs-complex test and the same closed-form expression that the cmath gem's Ruby source does (e.g. asin is built from this package's own Log and Sqrt, exactly as CMath.asin is), so results agree with the `ruby` binary to within floating-point rounding.
Index ¶
- type Number
- func Acos(z Number) Number
- func Acosh(z Number) Number
- func Asin(z Number) Number
- func Asinh(z Number) Number
- func Atan(z Number) Number
- func Atan2(y, x Number) Number
- func Atanh(z Number) Number
- func Cbrt(z Number) Number
- func Complex(re, im float64) Number
- func Cos(z Number) Number
- func Cosh(z Number) Number
- func Exp(z Number) Number
- func Log(z Number, base ...Number) Number
- func Log2(z Number) Number
- func Log10(z Number) Number
- func Real(x float64) Number
- func Sin(z Number) Number
- func Sinh(z Number) Number
- func Sqrt(z Number) Number
- func Tan(z Number) Number
- func Tanh(z Number) Number
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Number ¶
Number is a real-or-complex value, the Go analogue of a Ruby Float-or-Complex. A real Number has IsComplex false and carries its value in Real; a complex one has IsComplex true and uses both Real and Imag. The CMath functions take and return Number, choosing the real shape exactly where MRI's CMath does.
func Atan2 ¶
Atan2 returns the arc tangent of y/x using the signs of both to select the quadrant. CMath.atan2(y, x). Real when both arguments are real.
func Atanh ¶
Atanh returns the inverse hyperbolic tangent of z. CMath.atanh(z). Real for z in [-1, 1].
func Complex ¶
Complex builds a complex Number (a Ruby Complex argument). It stays complex even when the imaginary part is zero, matching Ruby: Complex(1,0).real? is false, so CMath treats it as complex.
func Log ¶
Log returns the natural logarithm of z, or the base-b logarithm when a base is supplied. CMath.log(z[, b]). A non-negative real z with a non-negative base stays real; anything else is complex.
