This mrbgem provides complex number support for mruby, offering mathematical functions that can handle complex numbers, similar to Ruby's standard cmath library.
This gem utilizes C99 _Complex features. Therefore, you will need a C compiler that supports C99 or a later version to build and use this gem.
The CMath module includes the following mathematical functions:
exp(z): Computes the exponential ofz.log(z): Computes the natural logarithm ofz.log(z, b): Computes the logarithm ofzwith baseb.log2(z): Computes the base-2 logarithm ofz.log10(z): Computes the base-10 logarithm ofz.sqrt(z): Computes the square root ofz.sin(z): Computes the sine ofz.cos(z): Computes the cosine ofz.tan(z): Computes the tangent ofz.asin(z): Computes the arc sine ofz.acos(z): Computes the arc cosine ofz.atan(z): Computes the arc tangent ofz.sinh(z): Computes the hyperbolic sine ofz.cosh(z): Computes the hyperbolic cosine ofz.tanh(z): Computes the hyperbolic tangent ofz.asinh(z): Computes the inverse hyperbolic sine ofz.acosh(z): Computes the inverse hyperbolic cosine ofz.atanh(z): Computes the inverse hyperbolic tangent ofz.
Here's a simple example of how to use CMath to calculate the square root of a negative number:
# Assuming mruby is built with mruby-cmath
# Calculate the square root of -4
result = CMath.sqrt(-4)
# result will be a Complex number (0+2i)
puts result.real # Output: 0.0
puts result.imaginary # Output: 2.0