Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

floating point log methods do not round correctly #47273

Closed
tspiteri opened this issue Jan 8, 2018 · 1 comment
Closed

floating point log methods do not round correctly #47273

tspiteri opened this issue Jan 8, 2018 · 1 comment
Labels
A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools

Comments

@tspiteri
Copy link
Contributor

tspiteri commented Jan 8, 2018

The methods f32::log and f64::log do not round correctly. As a trivial example, this code

fn main() {
    println!("{}, {}", 13f32.exp2().log2(), 13f32.exp2().log(2.0));
    println!("{}, {}", 29f64.exp2().log2(), 29f64.exp2().log(2.0));
}

outputs

13, 12.999999
29, 29.000000000000004
@tspiteri
Copy link
Contributor Author

tspiteri commented Jan 8, 2018

There is no LLVM intrinsic for logarithms to an arbitrary base, probably because IEEE754 does not include that in its list of recommended correctly rounded functions. So maybe it's not a good idea to try to implement this with correct rounding. However, considering that the other transcendental functions are correctly rounded, the documentation should clearly state that the log methods are not, maybe something like Python's documentation. I would write something like:

Returns the logarithm of the number with respect to an arbitrary base, calculated as self.ln() / base.ln().

If the base is 2 or 10, the log2 and log10 methods can yield more accurate results.

@Mark-Simulacrum Mark-Simulacrum added the A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools label Jan 10, 2018
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this issue Jan 14, 2018
doc: show that `f32::log` and `f64::log` are not correctly rounded

Fixes rust-lang#47273.

One thing I'm not sure about is whether the "calculated as `self.ln() / base.ln()`" bit is being too specific, maybe we do not want to make this such a strong commitment. I think it's fine, but we should not make commitments in the API documentation by accident.

In case that is removed, the added sentence "`self.log2()` can ... base 10." still makes it amply clear that the `log` methods can be more inaccurate than other methods. If the above clause is removed, this second sentence can be moved to the first paragraph, kind of like the accuracy comment for the [`mul_add`](https://doc.rust-lang.org/std/primitive.f32.html#method.mul_add) method.
kennytm added a commit to kennytm/rust that referenced this issue Jan 15, 2018
doc: show that `f32::log` and `f64::log` are not correctly rounded

Fixes rust-lang#47273.

One thing I'm not sure about is whether the "calculated as `self.ln() / base.ln()`" bit is being too specific, maybe we do not want to make this such a strong commitment. I think it's fine, but we should not make commitments in the API documentation by accident.

In case that is removed, the added sentence "`self.log2()` can ... base 10." still makes it amply clear that the `log` methods can be more inaccurate than other methods. If the above clause is removed, this second sentence can be moved to the first paragraph, kind of like the accuracy comment for the [`mul_add`](https://doc.rust-lang.org/std/primitive.f32.html#method.mul_add) method.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools
Projects
None yet
Development

No branches or pull requests

2 participants