Pure Programmer

# Math Library

While the arithmetic and logic operators represent the basic operations that our [[CPU]] can perform, there are many other common math functions that com in handy. Since they are so common, programming languages usually have a math library that provides these functions. Logarithms, trigonometry and random number generation are just of few of the types of functions typically provided.

## Math Constants

Math constants provide commonly used mathematical constanst to the highest precesion available. Some of the more useful math constants are summarized below.

Rust Math Constants
Constant Description

## Math Functions

These most useful math functions are summarized below.

Rust Math Functions
Function Description

Math1.rs
```/******************************************************************************
* This program demonstrates the math library.
*
*****************************************************************************/

use std::f64::consts;

fn main() {
let a:f64 = PI / f64::from(6);
let b:f64 = PI / f64::from(4);
let c:f64 = -a * 2;
let d:f64 = -b * 2;
let e:f64 = E;

println!("{}", format!("pi = {0:f}", PI));
println!("{}", format!("e = {0:f}", E));

// abs, floor, ceil, round, trunc, min, max
println!("{}", format!("abs({0:f}) = {1:f}", a, fabs(a)));
println!("{}", format!("abs({0:f}) = {1:f}", c, fabs(c)));
println!("{}", format!("floor({0:f}) = {1:f}", a, floor(a)));
println!("{}", format!("floor({0:f}) = {1:f}", c, floor(c)));
println!("{}", format!("ceil({0:f}) = {1:f}", a, ceil(a)));
println!("{}", format!("ceil({0:f}) = {1:f}", c, ceil(c)));
println!("{}", format!("round({0:f}) = {1:f}", a, round(a)));
println!("{}", format!("round({0:f}) = {1:f}", c, round(c)));
println!("{}", format!("trunc({0:f}) = {1:f}", a, trunc(a)));
println!("{}", format!("trunc({0:f}) = {1:f}", c, trunc(c)));
println!("{}", format!("min({0:f}, {1:f}) = {2:f}", a, c, fmin(a,c)));
println!("{}", format!("max({0:f}, {1:f}) = {2:f}", a, c, fmax(a,c)));

// sin, cos, tan, atan, atan2, acos, asin
println!("{}", format!("sin({0:f}) = {1:f}", a, sin(a)));
println!("{}", format!("sin({0:f}) = {1:f}", b, sin(b)));
println!("{}", format!("sin({0:f}) = {1:f}", c, sin(c)));
println!("{}", format!("sin({0:f}) = {1:f}", d, sin(d)));
println!("{}", format!("cos({0:f}) = {1:f}", a, cos(a)));
println!("{}", format!("cos({0:f}) = {1:f}", b, cos(b)));
println!("{}", format!("cos({0:f}) = {1:f}", c, cos(c)));
println!("{}", format!("cos({0:f}) = {1:f}", d, cos(d)));
println!("{}", format!("tan({0:f}) = {1:f}", a, tan(a)));
println!("{}", format!("tan({0:f}) = {1:f}", b, tan(b)));
println!("{}", format!("tan({0:f}) = {1:f}", c, tan(c)));
println!("{}", format!("asin({0:f}) = {1:f}", sin(a), asin(sin(a))));
println!("{}", format!("asin({0:f}) = {1:f}", sin(b), asin(sin(b))));
println!("{}", format!("asin({0:f}) = {1:f}", sin(c), asin(sin(c))));
println!("{}", format!("asin({0:f}) = {1:f}", sin(d), asin(sin(d))));
println!("{}", format!("acos({0:f}) = {1:f}", cos(a), acos(cos(a))));
println!("{}", format!("acos({0:f}) = {1:f}", cos(b), acos(cos(b))));
println!("{}", format!("acos({0:f}) = {1:f}", cos(c), acos(cos(c))));
println!("{}", format!("acos({0:f}) = {1:f}", cos(d), acos(cos(d))));
println!("{}", format!("atan({0:f}) = {1:f}", tan(a), atan(tan(a))));
println!("{}", format!("atan({0:f}) = {1:f}", tan(b), atan(tan(b))));
println!("{}", format!("atan({0:f}) = {1:f}", tan(c), atan(tan(c))));
// 45 degrees
println!("{}", format!("atan2({0:f}, {1:f}) = {2:f}", 1.0f64, 1.0f64, atan2(1.0f64,1.0f64)));
// 30 degrees
println!("{}", format!("atan2({0:f}, {1:f}) = {2:f}", 1.0f64, sqrt(3.0f64), atan2(1.0f64,sqrt(3.0f64))));

// sinh, cosh, tanh, atanh, acosh, asinh
println!("{}", format!("sinh({0:f}) = {1:f}", a, sinh(a)));
println!("{}", format!("sinh({0:f}) = {1:f}", b, sinh(b)));
println!("{}", format!("sinh({0:f}) = {1:f}", c, sinh(c)));
println!("{}", format!("sinh({0:f}) = {1:f}", d, sinh(d)));
println!("{}", format!("cosh({0:f}) = {1:f}", a, cosh(a)));
println!("{}", format!("cosh({0:f}) = {1:f}", b, cosh(b)));
println!("{}", format!("cosh({0:f}) = {1:f}", c, cosh(c)));
println!("{}", format!("cosh({0:f}) = {1:f}", d, cosh(d)));
println!("{}", format!("tanh({0:f}) = {1:f}", a, tanh(a)));
println!("{}", format!("tanh({0:f}) = {1:f}", b, tanh(b)));
println!("{}", format!("tanh({0:f}) = {1:f}", c, tanh(c)));
println!("{}", format!("tanh({0:f}) = {1:f}", d, tanh(d)));
println!("{}", format!("asinh({0:f}) = {1:f}", sinh(a), asinh(sinh(a))));
println!("{}", format!("asinh({0:f}) = {1:f}", sinh(b), asinh(sinh(b))));
println!("{}", format!("asinh({0:f}) = {1:f}", sinh(c), asinh(sinh(c))));
println!("{}", format!("asinh({0:f}) = {1:f}", sinh(d), asinh(sinh(d))));
println!("{}", format!("acosh({0:f}) = {1:f}", cosh(a), acosh(cosh(a))));
println!("{}", format!("acosh({0:f}) = {1:f}", cosh(b), acosh(cosh(b))));
println!("{}", format!("acosh({0:f}) = {1:f}", cosh(c), acosh(cosh(c))));
println!("{}", format!("acosh({0:f}) = {1:f}", cosh(d), acosh(cosh(d))));
println!("{}", format!("atanh({0:f}) = {1:f}", tanh(a), atanh(tanh(a))));
println!("{}", format!("atanh({0:f}) = {1:f}", tanh(b), atanh(tanh(b))));
println!("{}", format!("atanh({0:f}) = {1:f}", tanh(c), atanh(tanh(c))));
println!("{}", format!("atanh({0:f}) = {1:f}", tanh(d), atanh(tanh(d))));

// log, log10, exp, pow, sqrt
println!("{}", format!("log({0:f}) = {1:f}", a, log(a)));
println!("{}", format!("log({0:f}) = {1:f}", b, log(b)));
println!("{}", format!("log({0:f}) = {1:f}", -c, log(-c)));
println!("{}", format!("log({0:f}) = {1:f}", -d, log(-d)));
println!("{}", format!("log({0:f}) = {1:f}", e, log(e)));
println!("{}", format!("log10({0:f}) = {1:f}", a, log10(a)));
println!("{}", format!("log10({0:f}) = {1:f}", b, log10(b)));
println!("{}", format!("log10({0:f}) = {1:f}", -c, log10(-c)));
println!("{}", format!("log10({0:f}) = {1:f}", -d, log10(-d)));
println!("{}", format!("log10({0:f}) = {1:f}", e, log10(e)));
println!("{}", format!("exp({0:f}) = {1:f}", 0.5f64, exp(0.5f64)));
println!("{}", format!("exp({0:f}) = {1:f}", 1.0f64, exp(1.0f64)));
println!("{}", format!("exp({0:f}) = {1:f}", 2.0f64, exp(2.0f64)));
println!("{}", format!("pow({0:f}, {1:f}) = {2:f}", 10.0f64, 0.5f64, pow(10.0f64,0.5f64)));
println!("{}", format!("pow({0:f}, {1:f}) = {2:f}", 10.0f64, 1.0f64, pow(10.0f64,1.0f64)));
println!("{}", format!("pow({0:f}, {1:f}) = {2:f}", 10.0f64, 2.0f64, pow(10.0f64,2.0f64)));
println!("{}", format!("sqrt({0:f}) = {1:f}", 0.5f64, sqrt(0.5f64)));
println!("{}", format!("sqrt({0:f}) = {1:f}", 2.0f64, sqrt(2.0f64)));
println!("{}", format!("sqrt({0:f}) = {1:f}", 10.0f64, sqrt(10.0f64)));

// random numbers
println!("{}", format!("random() = {0:f}", random()));
println!("{}", format!("random() = {0:f}", random()));
println!("{}", format!("random() = {0:f}", random()));
}

```
Output
Math2.rs
```/******************************************************************************
* This program demonstrates the math integer functions.
*
*****************************************************************************/

fn main() {
let a:isize = 5;
let b:isize = 10;
let c:isize = -2;

// abs, floor, ceil, round, trunc, min, max
println!("{}", format!("abs({0:d}) = {1:d}", a, abs(a)));
println!("{}", format!("abs({0:d}) = {1:d}", c, abs(c)));
println!("{}", format!("min({0:d}, {1:d}) = {2:d}", a, b, min(a,b)));
println!("{}", format!("max({0:d}, {1:d}) = {2:d}", a, b, max(a,b)));
println!("{}", format!("min({0:d}, {1:d}) = {2:d}", b, c, min(b,c)));
println!("{}", format!("max({0:d}, {1:d}) = {2:d}", b, c, max(b,c)));

// random numbers
println!("{}", format!("random({0:d}) = {1:d}", a, irandom(a)));
println!("{}", format!("random({0:d}) = {1:d}", a, irandom(a)));
println!("{}", format!("random({0:d}) = {1:d}", a, irandom(a)));
println!("{}", format!("random({0:d}) = {1:d}", a, irandom(a)));
println!("{}", format!("random({0:d}) = {1:d}", a, irandom(a)));
println!("{}", format!("random({0:d}) = {1:d}", b, irandom(b)));
println!("{}", format!("random({0:d}) = {1:d}", b, irandom(b)));
println!("{}", format!("random({0:d}) = {1:d}", b, irandom(b)));
println!("{}", format!("random({0:d}) = {1:d}", b, irandom(b)));
println!("{}", format!("random({0:d}) = {1:d}", b, irandom(b)));
println!("{}", format!("random(2) = {0:d}", irandom(2)));
println!("{}", format!("random(2) = {0:d}", irandom(2)));
println!("{}", format!("random(2) = {0:d}", irandom(2)));
println!("{}", format!("random(2) = {0:d}", irandom(2)));
println!("{}", format!("random(2) = {0:d}", irandom(2)));
println!("{}", format!("random() = {0:f}", random()));
println!("{}", format!("random() = {0:f}", random()));
println!("{}", format!("random() = {0:f}", random()));
println!("{}", format!("random() = {0:f}", random()));
println!("{}", format!("random() = {0:f}", random()));
}

```
Output

## Random Numbers

Explain how to generate uniform random numbers, int and fp.

## Questions

• {{Write an expression that yields √5.}}
• {{Write an expression that yields 3√5.}}
• {{Write an expression that yields the secant of π/4.}}
• {{Write an expression that yields log16 100.}}

## Projects

More ★'s indicate higher difficulty level.

## References

• [[Rust Language Reference]]
• [[Rust Compiler]]