Float precision

73 views
Skip to first unread message

Paul Royik

unread,
Jul 12, 2015, 12:18:26 PM7/12/15
to sy...@googlegroups.com
Is it possible to set for float precision of 50, but to cut trailing zeros for finite decimals?
a=Float('0.5',prec=50)
print a/7
Output: 0.071428571428571428571428571428571428571428571428571


a=Float('0.5',prec=50)
print a
Output: 0.50000000000000000000000000000000000000000000000000 (but I want a)

For latex correct output is 0.5
But how to print 0.5 instead of 0.5 with many trailing zeros?


In what cases is Float('some number', prec='') is useful?


Vinzent Steinberg

unread,
Jul 13, 2015, 12:08:27 PM7/13/15
to sy...@googlegroups.com
On Sunday, July 12, 2015 at 6:18:26 PM UTC+2, Paul Royik wrote:
Is it possible to set for float precision of 50, but to cut trailing zeros for finite decimals?
a=Float('0.5',prec=50)
print a/7
Output: 0.071428571428571428571428571428571428571428571428571


a=Float('0.5',prec=50)
print a
Output: 0.50000000000000000000000000000000000000000000000000 (but I want a)

For latex correct output is 0.5
But how to print 0.5 instead of 0.5 with many trailing zeros?

In floating point representation, there is no difference between finite and non-finite decimals, so they are printed the same.
If you want to remove trailing zeros, convert it to a string and strip the zeros:

    a_without_zeros = str(a).rstrip('0')

Vinzent

Paul Royik

unread,
Jul 14, 2015, 3:53:35 PM7/14/15
to sy...@googlegroups.com
str(a).rstrip('0') trick won't won't work with complex numbers, like 0.3000000000000+0.50000000000i

Aaron Meurer

unread,
Jul 14, 2015, 4:10:38 PM7/14/15
to sy...@googlegroups.com
The printer has an option to do this. Use sstr() and set the full_prec flag.

Aaron Meurer

--
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 sympy+un...@googlegroups.com.
To post to this group, send email to sy...@googlegroups.com.
Visit this group at http://groups.google.com/group/sympy.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/cbd44009-2081-4bfb-9c20-330afbcb15ad%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Paul Royik

unread,
Jul 15, 2015, 8:48:26 AM7/15/15
to sy...@googlegroups.com
Thank you.
Reply all
Reply to author
Forward
0 new messages