64 bit trigonometry

Surely there is some middle ground here. The rust book could explain how to take the square root or logarithm of an f64.


Try looking at what other languages do. Since the book is meant as a learning resource this seems a good starting point:

Yeah math is a very wide thing technically. No, that is not what anyone in programming means though. Looking at this list I do spot a pattern:

  • Trigonometry
  • Logarithms
  • Constants (pi, e, etc)
  • Plus some varying extra stuff (abs, bessel, error, gamma, ...)

Even though math is indeed a very wide concept the programming world seems to agree surprisingly well on what a core subset of math suitable for a standard library is.

That might be a suitable topic for the rust book. With a heading at the end pointing to some more specialised crates if you want some of the more obscure things.

1 Like

Frankly I'm not sure what you are arguing about here.

Why do these languages have separate math module? That's easy: because when they were developed “math” wasn't something you would get automatically when you buy CPU in a box. You have to pay extra for this or that to get your share of “math”.

That's why it's separate in these old languages.

Now, what kind of math you are supposed to get? That's easy to answer, too: just take the list and go with it (later C99 did a mistake by adding bunch of random other functions to the list and C++11 duplicated it to become compatible but it's not clear why Rust have to duplicate that mistake).

And this is more-or-less what Rust is doing: you have primitive without any extra math and then there are version with expected math functions, too.

Thus the only discussion now is about why Rust doesn't follow naming conventions of these old legacy languages. I suspect it's because times when you had to purchase separate FPU are long gone: not only you don't need to do that for CPUs of XXI century, you don't even have such ability in case where thus is not provided by default (like in small embedded controllers). Having separate Math module stopped being sensible and thus Rust rolled it into std.

I'm not 100% sure whether that good idea or not, but I think now, few years after that decision was done trying to reverse that choice would just make things worse. For everyone.

It could have helped with adoption of Rust, maybe, but today that ship have sailed.

Everything that have support in hardware on some platform, just like other such intrinsics, right?

Because things that can be reduced to something implementable in hardware belongs to third-party crate, not core or std, but if something is one machine instruction then you would expect to find it in standard library for obvious reasons.

And that's precisely what Rust is doing, so what's the issue?

I don't think it belongs to Rust boot at all. You may write lots of programs without ever touching that stuff and when you do need to touch it you'll need another, entirely separate book.

Think about it: just to calculate average of bunch of floating point numbers you need to do a very specialized dance… do you think all that belongs to a book which teaches you how references work and how you may combine few iterators to achieve something useful?

Now, if someone would write book on how to do math in Rust properly… then The Book may include appropriate reference. But stuffing things like these in it? I don't think it's wise.

1 Like

Isn't this discussion about documentation? It's hard to find math stuff in the documentation if you don't know where to look:

  • You might need to know or guess the name of the function (e.g. acos or arccos).
  • You have to know that those functions are methods on a type. The main page for std alludes to this but doesn't give any examples and the syntax is weird.
  • The Rust book doesn't have any examples (as far as I know).
  • If it's in another crate you're out of luck. (What should I install to call erf or lgamma?)

I don't think the Rust book needs to turn into a math textbook but why not explain how to find the intersection of a line and a circle or how to compute the sine of an angle? The book includes a web server as a project.


The original post in this thread is about not being able to find acos and atan.

When I search for log1p rust on Google I get a link to the libm crate, and when I search in the documentation I get log10. Neither gives a link to the standard library function which has been renamed ln_1p.

As for erf, why should I use statrs and not libm? (This is the disadvantage of not standardizing a standard function.)

Where do you think content on Google comes from? Ultimately people have to write it. The Rust documentation could be more discoverable.

Some programmers need to do mathematical computations. It's one of the things computers were literally invented to do. Other programmers probably never make f64 variables; they can skip that content.


I for example, rarely do much string processing. Yet I was happy to read that section in the book.

Regarding which operations are available, it looks to me like it's basically just from some version (2008?) of the IEEE 754 recommended operations, which is as good a list as any.


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.