How do I specify the printing precision for Sympy.pretty_print? - sympy

I've been trying to figure this out from the Sympy docs, but I can't find any options or parameters that control precision for printed output. I don't want to change the precision of the stored numbers, I just don't need 17 digits of precision in my output. Any good way to do this?
import sympy as sp
sp.init_printing() # presumably options would go here?
a = sp.pi
sp.pprint(sp.N(a)) # I'd like to see only a few digits here, not full precision

Related

Strange number formating in ssas tabular?

I have a ssas tabular cube. I have a question regarding formatting here:
I have number 1,000,000,000.5
By using format: #,##0,.0
it gets displayed as: 1,000,000.5
I have 2 questions:
What's the logic? how is #,##0,.0 instructing to remove 000?
Also, i would like to get rid of the decimal, and show it like 1,000,000 How can I do it?
Commas before the decimal point that are not followed by # or 0 divide the result by 1000.
Whatever digits it shows will be rounded to that precision. If you want to drop the decimal rather than rounding, you can use TRUNC or INT in the measure definition.
If you use the following format #,### will give you the following.

XSLT - Round up to two decimal places

There is a requirement to round the value up(always) to two decimal places. meaning, the number 8.3333333 should become 8.34. Round and format-number functions do not seem to achieve this. Does anyone have an idea on how to get the desired output using xslt transformation please?
To round up a number with precision of two decimal places:
ceiling(100*$value) div 100
If you need trailing zeros (i.e. a string, not a number) then wrap this in format number().

Double precision in fortran accurate up to 16 digits

I am using FORTRAN 95 using Silverfrost Plato where I am trying to make real double precision variables. Currently I use 'double precision' which gives accuracy up to 12 decimal digits. But I wanted to know if we could have digits accurate up to 16 digits. Interestingly, when I tried using 'real(kind=3)' there were some residual values showing up in the last few digits that ended up making the results go wrong.
I am unable to find a proper solution to this. Any help or links to relevant documentation would be greatly appreciated.

How to correctly add floating numbers in Python?

I am trying to add 0.2 value to constant x where x = 8 in a loop that runs to 100. Following is the code
x = 8
>>> for i in range(100):
... x += 0.2
...
>>> x
but everytime I get different answer and calculation always incorrect. I read about Floating Point Arithmetic Issue and Limitations but there should be some way around this. Can I use doubles (if they exists) ? I am using Python 2.7
UPDATE:
import time
x=1386919679
while(1):
x+=0.02
print "xx %0.9f"%x
b= round (x,2)
print "bb %0.9f"%b
time.sleep(1)
output
xx 1386933518.586801529
bb 1386933518.589999914
xx 1386933518.606801510
bb 1386933518.609999895
xx 1386933518.626801491
bb 1386933518.630000114
Desired output
I want correct output, I know If just write print x it will be accurate. But my application require that I should print results with 9 precision. I am newbie so please be kind.
You can use double-precision floating point, sure. You're already using it by default.
As for a way around it:
x += 0.2 * 100
I know that sounds facile, but the solution to floating point imprecision is not setting FLOATING_POINT_IMPRECISION = False. This is a fundamental limitation of the representation, and has no general solution, only specific ones (and patterns which apply to groups of specific situations).
There's also a rational number type which can exactly store 0.2, but it's not worth considering for most real-world use cases.

Where does this precision loss happen and how to prevent it?

I'm writing a simple tool in Qt which reads data from two GPX (XML) files and combines them in a certain way. I tested my tool with track logs that contain waypoints having 6 decimal digits precision. When I read them from the GPX file, the precision gets reduced to 4 decimal digits (rounded properly). So for example this original tag:
<trkpt lat="61.510656" lon="23.777735">
turns into this when my tool writes it again:
<trkpt lat="61.5107" lon="23.7777">
Debug output shows the precision loss happens on this line:
double lat = in.attributes().value("", "lat").toString().toDouble();
but I can't see why. in is a QXmlStreamReader reading from a text file handle.
It is probably when you are writing the value back to the XML. Please post that code in your question.
If I had a guess before seeing the code, you are using QString::number to convert from the double back to a string. The default precision in the conversion is 6, which corresponds to what you are seeing. You can increase the precision to get all the decimals.