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

round(x, ndigits) where x is like 1.0e+/-k does not work #26663

Closed
JeffreySarnoff opened this issue Mar 30, 2018 · 12 comments
Closed

round(x, ndigits) where x is like 1.0e+/-k does not work #26663

JeffreySarnoff opened this issue Mar 30, 2018 · 12 comments

Comments

@JeffreySarnoff
Copy link
Contributor

please see this thread for the details

@jmkuhn
Copy link
Contributor

jmkuhn commented Mar 30, 2018

julia> round(1.23456e1, 2)
12.35

julia> signif(1.23456e1, 2)
12.0

If you want to fold signif into round, I think you would need to do it using a keyword argument.

@simonbyrne
Copy link
Contributor

that's not a bad idea. We could use digits/sigdigits?

@JeffreySarnoff
Copy link
Contributor Author

it is nice to have round(x, abs(n)) indicate digits to the right of the decimal point and round(x, -abs(n)) indicate digits to the left of the decimal point. round uses digits as an arg name at the moment, so a keyword would be sigdigits. Would the logic be that sigdigits determines the value from which to round to +/- digits?

@simonbyrne
Copy link
Contributor

My idea would be to make both digits and sigdigits keyword arguments: you can specify at most one.

@JeffreySarnoff
Copy link
Contributor Author

is there a way to specify "only one of these may be given" in the signature or does that test have to occur inside the function?

@JeffreySarnoff
Copy link
Contributor Author

and .. having both as keywords makes sense to me

@simonbyrne
Copy link
Contributor

The main issue is that it makes dispatch and extensions a bit complicated. e.g. if I define a new number type MagicReal, then I can't just define round(::MagicReal). I end up needing a surrogate positional dispatch function (say _round) which gets overloaded instead.

@JeffreySarnoff
Copy link
Contributor Author

is there a way for us to supply a general purpose sensibly behaving fallback for just such occasions?

maybe an alternative, so other types do not have to stress it

# where {T} written in a white font

round(x::T; digits::Int) = round_with_digits(x, digits)
round(x::T; sigdigits::Int) = round_with_sigdigits(x, sigdigits)

round(x::T; digits::Int, sigdigits::Int) = throw(ErrorException("use either `digits` or `sigdigits`, use of both simultaneously is not supported")

round(x::T, digits::Int) = round_with_digits(x, digits)

@jmkuhn
Copy link
Contributor

jmkuhn commented Apr 4, 2018

One feature that I'd like to have in round is the ability to specify both digits and RoundingMode. Currently we can do round(x, digits) or round(x, roundingmode) but not round(x, digits, roundingmode) or signif(x, digits, roundingmode). Would it be possible to have this option in PR #26670?

@JeffreySarnoff
Copy link
Contributor Author

It is always beneficial to support RoundingMode. Note that for this situation, some wrapping logic may be needed because Round(x::F,RoundingMode) where {F<:IEEEFloat} usually piggybacks on some chiplevel instructions.

@simonbyrne
Copy link
Contributor

One feature that I'd like to have in round is the ability to specify both digits and RoundingMode.

One step ahead: https://github.com/JuliaLang/julia/pull/26670/files#diff-a6b2d98c6bbc2867449a6c3514854ff3R47

@simonbyrne
Copy link
Contributor

Was fixed by #26670

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants