# 64 bit trigonometry

I am currently working on a project that requires trigonometry operations on relatively small distances and angles. The programme is supposed to run on 64 bit systems, and that length should be enough for the purpose. However, I am unable to find such functionality in the core or in the crate registry.

I am aware of the following crates:

• `rust_math`: only works on 32 bit floats and at least the arc-cosine function is currently failing.

• `astro_float`: implements its own float type, which beyond being overkill for my use case, can not be converted back to f64.

Would there be another option out there? Thank you.

I'm guessing you're asking this because the standard library methods don't have enough precision for your use-case? What formulas do you need?

I'm not knowledgeable in advanced trigonometry, but `f64` implements all basic trigonometric functions. What else is it you need?

3 Likes

The inverse functions: arc-cosine, arc-tangent, etc.

`f64::acos`

`f64::atan`

2 Likes

Thanks. Seems like all I need at this moment is here. I was searching for function names like `arccos` or `arctan` and never got to this documentation page.

1 Like

It may be reasonable to add those as aliases so they show up in the rustdoc search.

1 Like

I was just thinking that and will make a PR.

14 Likes

How small are the angles? In some cases the standard functions might not be enough, e.g. if you want to compute `1 - cos(x)` for `x` near zero, because of catastrophic cancellation.

1 Like

Aliases might help.
I found the docs confusing a bit at first. I understand the math functions are "attached" to the "types" so their docs are in the docs for the types (f32, etc). I think there also should be a section somewhere in the std docs with "math" functions. Linked to by the top std module page. Of course after I figured out where to look (in the types) finding the math functions was easy.
NOTE: searching the std docs for "math" finds stuff for "path" and "trig" goes even more sideways trignored

I don't think anything screams "math" better than a function being literally associated with a numeric type.

Why the obsession with transcendental functions? If they should have a separate section in the docs, why shouldn't any other kind of function? I mean, the first place I would look for math functions is numeric types, that'sā¦ not exactly hidden.

4 Likes

This is possibly a case of expectations depending on your language background. C, C++ and Python at least don't have math functions (or any functions) on primitive types. In fact I can't think of any language that I know that does. So it seems reasonable if you have such a background to assume it is the same in Rust too.

It can be insightful to look at the world from the perspective of others sometimes.

8 Likes

It doesn't help that the Rust standard library renamed some functions (e.g. the logarithm, `log1p`, etc.) and is missing some others (Bessel functions, the Gamma function, the error function, etc.).

I'm not aware of any renaming, that would have broken backwards compatibility big time.

If you mean they don't have exactly the same name as they have in C, then: it's a different language. It's easy enough to find `ln_1p` if you scroll the left sidebar a little bit.

The standard library is intentionally minimalistic. Bessel and Gamma functions are pretty niche, but you could probably make a case for `erf` in an RFC. Otherwise, for statistics-specific functions, use the `statrs` crate.

2 Likes

When you're searching by a name, there's nothing to scroll. Searching for `log1p` just returns `log10`. As for special functions, how are you supposed to know about what other crates to use? Why `statrs` and not `libm`? It's something the Rust documentation could include. ("Looking for `erf`? See the `libm` crate.")

1 Like

Well a search for numeric sure doesn't help either.
If you know where to look, then yes it is obvious.

Not literally the word "numeric". Numeric types are the various representations of numbers, eg. `i32` or `f64`.

The documentation could include a whole bunch of stuff. It's not a realistic expectation, though. There's always Google, isn't it?

Yep. The "Book" gives a whole chapter to strings. If you search for "Math" in The Book, you still find nothing. Yes, you and I know to look in the primitive types for the functions that work on those types. But "math" is so common, newbies will look for the math functions first. It takes a step in the rust learning process to get to the point where you understand that the operations are tied to the types. Almost like "objects" but called "types" and technically different.

2 Likes

Yeah. Strings are a small, self-contained concept directly relevant to programming.

"Math" is a whole scientific discipline. How exactly would you expect Rust documentation to cover "math"? That seems absolutely unrealistic. There has been a lot of talk abut Bessel functions and the Gamma function above, which are solutions to advanced differential equations that come up in university-level complex analysis and statistics. Do you really think that Rust documentation should include differential equations, probability theory and statistics, and complex analysis as subchapters? That can't possibly be a serious demand.

1 Like