I'm not sure if the SymPy docs mention it (you'd have to search). The
mpmath docs might, which is what SymPy is using under the hood to do
the rounding.

Aaron Meurer

On Tue, Dec 14, 2021 at 10:56 AM Damien Desfontaines
<[email protected]> wrote:
>
> Thanks! Round-to-even in base-2 is perfect for my use case. Is there a 
> document or a piece of source code somewhere I could use as a reference for 
> that fact?
>
> I can't use evalf() with a fixed number of digits because I don't know in 
> advance how many digits I need: if the number is close to 0, then I might 
> need a ton of precision (the exponent can be -126, possibly even less if 
> subnormals are supported) than if the number is large (e.g. if the number is 
> closed to 1, you only need a precision of 2^-53).
> On Thursday, December 2, 2021 at 10:55:36 PM UTC+1 [email protected] wrote:
>>
>> I believe it uses round-to-even, but note that the rounding is done in
>> base-2, not base-10. If you're worried about the rounding, you can
>> evaluate the number to more digits using evalf() to see exactly how it
>> rounded.
>>
>> If you want to use a specific base-10 rounding mode, I believe
>> currently the only way is to get a float with sufficient digits from
>> evalf() then use the methods in the standard library decimal module.
>>
>> Aaron Meurer
>>
>> On Thu, Dec 2, 2021 at 11:23 AM Damien Desfontaines
>> <[email protected]> wrote:
>> >
>> > Hi all,
>> >
>> > This feels like a very simple problem, but I couldn't find a clear answer 
>> > in the documentation. I have a sympy number x and I would like to round it 
>> > to the nearest float. Suppose I simply use float(x), and that the true 
>> > value of x is between two successive floats a and b.
>> > - If x is slightly closer to a than b, is sympy guaranteed to round to a? 
>> > Same question if x is slightly closer to b.
>> > - If x is exactly equal to (a+b)/2, will float(x) round it up or down?
>> > - Are there situations where sympy can't be sure whether to round up or 
>> > down? What happens then? Is there a way to detect when that happens?
>> >
>> > Thanks a ton in advance,
>> >
>> > Damien
>> >
>> > --
>> > You received this message because you are subscribed to the Google Groups 
>> > "sympy" group.
>> > To unsubscribe from this group and stop receiving emails from it, send an 
>> > email to [email protected].
>> > To view this discussion on the web visit 
>> > https://groups.google.com/d/msgid/sympy/aa33ca28-6a88-44f8-b872-9fa6717eb22cn%40googlegroups.com.
>
> --
> You received this message because you are subscribed to the Google Groups 
> "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to [email protected].
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/sympy/1a416ef3-9670-41ec-b43b-bead64bf8980n%40googlegroups.com.

-- 
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sympy/CAKgW%3D6JZ7cuYWq7gyobHnyYL8_zCLciiMMjR%3Dk1tj2AZJFGD-g%40mail.gmail.com.

Reply via email to