-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Add BigFloat constructors with precision and rounding mode #13155
Add BigFloat constructors with precision and rounding mode #13155
Conversation
I forgot to say that it also changes convert for This was implemented by calling an MPFR, but could alternatively implemented by multiplying |
Bump. This is ready for comments. Cc @simonbyrne @pabloferz |
cc: @stevengj
|
I'm inclined to agree with @simonbyrne on adding this to |
@simonbyrne and @stevengj Thanks for the comments. The idea is to provide a less wordy version via
I would also be happy to add (An alternative that I would not like so much would be to add a new function |
function eps(x::BigFloat)
with_bigfloat_precision(precision(x)) do
nextfloat(x) - x
end
end
eps(::Type{BigFloat}) = eps(BigFloat(1))
julia> eps(prevfloat(BigFloat(Inf)))
inf This seems like a reasonable answer to me. On master it gives
By the way, I often find myself wanting to throw a |
672d9ad
to
391729c
Compare
@simonbyrne I have implemented the suggestion that In the process, I realised that in fact part of the problem was in For these kinds of things, we are in need of a |
|
BigFloat(100) is already taken for converting 100 to a I was trying via a keyword argument with BigFloat(; prec=get_bigfloat_precision()) but so far this has led to some kind of inference recursion and stack overflows that I haven't been able to get rid of.
In any case, I'll move the
which seems to me to just be wrong --
|
d85a97e
to
ca34377
Compare
Also adds big(x, precision), including for x a string, and big(x, precision, rounding_mode)
This gives the epsilon *using the precision of `x`*.
db2a1b2
to
8d29ad0
Compare
Write prevfloat and nextfloat in terms of it.
Before we merge this, it would be useful to hash out the rules for BigFloat precision: when do we use the precision of the current value, and when do we use the global precision? (and document it all, cf #7535). |
I agree that this needs thinking about. For example, to me it seems natural that one-input functions like However, for two-input functions like |
MPFR requires that you specify the precision of every output. |
Yes, you're right of course, sorry. |
Perhaps a useful distinction is whether the function is about the number (e.g. |
That's certainly a useful distinction to have in mind. |
|
Some edge cases that might be worth thinking about:
|
IIRC, as @simonbyrne said, precision is mostly kept global because MPFR requires the precision of the output in order to do almost any operation. I once tried using the precision of the arguments whenever possible, but it got out of control pretty fast, and ended up being harder to understand than using In this other comment I also talk a bit about related issues. |
return z | ||
end | ||
|
||
eps(::Type{BigFloat}) = nextfloat(BigFloat(1)) - BigFloat(1) | ||
function eps(x::BigFloat) | ||
BigFloat(nextfloat(x) - x, prec=precision(x)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You might be doing double rounding in this case, no? The result of the subtraction is calculated using the global precision, and then the result is converted down to precision of x
.
@andrioni, nice to have you back around here! |
Bump. @dpsanders any interest in revisiting this? |
@tkelman Yes, I certainly think this should be revisited, thanks for the bump. I won't be able to get to it for the next couple of weeks, though. |
what part of this was not covered by #17217 ? |
The part where |
All content here is now superseded. |
This adds new constructors for
BigFloat
of the formIt also changes the behaviour of
eps
,prevfloat
andnextfloat
acting onBigFloat
s so that they are calculated using the precision ofx
itself. Note that this is one of the ambiguities forced on us by the fact that the precision is not a parameter of theBigFloat
type; c.f. #10040