Where is the sine function?

A simple question: Where is sin() ? I searched and found only in the Rust docs that there are traits like std::num::Float that require sin but not implementation.

+7
math rust sin
source share
2 answers

The Float attribute has been removed, and now methods are integral type implementations. This means that accessing the math functions requires a little less input:

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

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

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

You can also use the unambiguous syntax for calling a function:

 fn main() { println!("{}", f32::sin(3.14159)); } 
+11
source share

Float is Trait, enable implementation, import it for application for f32 or f64.

 use std::num::Float; fn main() { println!("{}", 1.0f64.sin()); } 
+1
source share

All Articles