Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is the sine function?

Simple question: Where is sin()? I've searched and only found in the Rust docs that there are traits like std::num::Float that require sin, but no implementation.

like image 460
Kapichu Avatar asked Jan 18 '15 14:01

Kapichu


People also ask

Where is sin located in math?

The sine of one of the angles of a right triangle (often abbreviated “sin”) is the ratio of the length of the side of the triangle opposite the angle to the length of the triangle's hypotenuse.

Where is sine and cosine?

Sine and cosine — a.k.a., sin(θ) and cos(θ) — are functions revealing the shape of a right triangle. Looking out from a vertex with angle θ, sin(θ) is the ratio of the opposite side to the hypotenuse , while cos(θ) is the ratio of the adjacent side to the hypotenuse .

Where is sine used?

The sine rule is used when we are given either a) two angles and one side, or b) two sides and a non-included angle. The cosine rule is used when we are given either a) three sides or b) two sides and the included angle.


1 Answers

The Float trait was removed, and the methods are inherent implementations on the types now (f32, f64). That means there's a bit less typing to access math functions:

fn main() {
    let val: f32 = 3.14159;
    println!("{}", val.sin());
}

However, it's ambiguous if 3.14159.sin() refers to a 32- or 64-bit number, so you need to specify it explicitly. Above, I set the type of the variable, but you can also use a type suffix:

fn main() {
    println!("{}", 3.14159_f64.sin());
}

You can also use fully qualified syntax:

fn main() {
    println!("{}", f32::sin(3.14159));
}

Real code should use the PI constant; I've used an inline number to avoid complicating the matter.

like image 129
Shepmaster Avatar answered Oct 11 '22 21:10

Shepmaster