This module provides access to a series of mathematical functions.
This module requires the 0.5.18 version of CX.
After installing CX and all its dependencies clone this repository to by running:
$ git clone https://github.com/galah4d/math-cx.gitIt is probably a good idea to keep all your CX modules in once place so fell free to move the installation to wherever you see fit but make sure to remember it's location since it will be needed later.
After installing the module you must import it inside of your CX program in order to use it. This can be done by including the following import statement in your program:
import math
You should now have access to all functions inside the math module (a detailed explanation on how to use each on of these functions can be found bellow).
Currently CX isn't able to automatically manage dependencies so we have to manually do this at compile time. In order to do so you must specify the path to the math module before your own CX files while executing the cx command. This can be achieved by doing:
cx $CXPATH/pkg/math-cx/math.cx you_file.cxThe previous example is only valid if you have installed the module to "$CXPATH/pkg/math-cx/". If this isn't the case you must change it to your own custom installation path.
Returns the absolute value of x.
package main
import math
func main() {
f64.print(math.abs(-18.7D)) // Outputs 18.7
}
Returns the inverse value of x.
package main
import math
func main() {
f64.print(math.inv( 12.1D)) // Outputs -12.1
f64.print(math.inv(-12.1D)) // Outputs 12.1
}
Returns the smallest integer value greater than or equal to x.
package main
import math
func main() {
f64.print(math.ceil(8.1D)) // Outputs 9
f64.print(math.ceil(8.7D)) // Outputs 9
f64.print(math.ceil(-3.2D)) // Outputs -3
f64.print(math.ceil(-3.8D)) // Outputs -3
}
Returns the largest integer value less than or equal to x.
package main
import math
func main() {
f64.print(math.floor(8.1D)) // Outputs 8
f64.print(math.floor(8.7D)) // Outputs 8
f64.print(math.floor(-3.2D)) // Outputs -4
f64.print(math.floor(-3.8D)) // Outputs -4
}
Returns a raised to the power of b. (Note: b must be a positive integer)
package main
import math
func main() {
f64.print(math.pow(2.0D, 4L)) // Outputs 16.0
f64.print(math.pow(3.6D, 5L)) // Outputs 604.66176
f64.print(math.pow(-2.0D, 2L)) // Outputs 4.0
f64.print(math.pow(-2.0D, 3L)) // Outputs -8.0
}
Converts the angle x from radians to degrees.
package main
import math
func main() {
f64.print(math.degrees(math.PI)) // Outputs 180.0
}
Converts the angle x from degrees to radians.
package main
import math
func main() {
f64.print(math.radians(180.0D)) // Outputs 3.1415926536
}
The mathematical constant pi with 10 decimals precision.
package main
import math
func main() {
f64.print(math.PI) // Outputs 3.1415926536
}
The mathematical constant e with 10 decimals precision.
package main
import math
func main() {
f64.print(math.E) // Outputs 2.7182818284
}
Work in progress...
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.
This module is still in alpha, functions can contain bugs.