Does rust not have sin/cos/tan functions?

Right now I need to import: use js_sys::Math::sin;. But that doesn't seem very efficient since it needs to come from JS. Does Rust not have these functions itself?

Rust has trigonometric functions defined on the floating point types f32 and f64. For example, f64::sin().

3 Likes

Just out of curiosity. Have you tried googling rust sin function?
I am not trying to bash you or anything. I'm genuinly curious how it came that you asked this question here?
Somehow the discoveribility of the trig funtions (and others) in rust seems to trip people up.
I think I remeber myself searching for a standalone function and beeing surprised finding it defined on the numeric types themselfes.

2 Likes

For me it says: cannot find function sin in this scope, consider importing this function: use js_sys::Math::sin;`

What am I supposed to do than, how do I bring it in scope?

Can you post the actual code? Now it's getting interesting :slight_smile:

In case you might have missunderstood.

You don't write

cos(3.3);

but

3.3.cos();

or rather in most cases something like

let x = 3.14;
x.cos();
2 Likes

Im sorry I did misunderstand.. Thats exactly it :S
just had it figured out as well after you said

Already thought I did something stupid haha

1 Like

Not stupid at all. Glad we could help :slight_smile:

1 Like

Just to clarify, the first answer said

Emphasis mine. That is to say, they are associated functions (or methods). So you have to call them either with the method call syntax, or – and I prefer it that way – using uniform function call syntax:

let one = f64::sin(1.5707963268);
1 Like

Not only not stupid, I believe the compiler should have helped you overcome this bump without needing to ask for external help, in this case.

10 Likes

That's a good one, it definitely helped with the confusion.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.