Ask Your Question
0

can i display only two decimals of precision

asked 2019-01-29 01:15:51 +0100

mike newman gravatar image

I have some lists I want to display. I don't want to limit the precision for the arithmetic that created them, I only want to limit the number of decimal places (not significant figures) that are displayed.

def f(n): return sorted([ 2*cos(2*3.14159*j/n) for j in range(n) ]) ;
f(5)

Gives me the five required values displayed with what seems to be 14 digits after the decimal. Is it possible to change 14 to some other value?

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
1

answered 2019-01-29 09:11:48 +0100

vdelecroix gravatar image

Given most object in Sage that have a numerical counterpart you can use the function numerical_approx.

sage: pi.numerical_approx(digits=2)
3.1
sage: (cos(pi/7) + sin(pi/13)).numerical_approx(digits=10)
1.140284532

Alternatively, you obtain the same result with

sage: numerical_approx(pi, digits=2)
3.1
sage: numerical_approx(cos(pi/7) + sin(pi/13), digits=10)
1.140284532
edit flag offensive delete link more

Comments

Note that a predefined abbreviation of numerical_approx is n:

sage: n(pi, digits=2)
3.1
eric_g gravatar imageeric_g ( 2019-01-29 11:07:56 +0100 )edit

This gives 2 significant digits. Is it possible to get two digits after the decimal? So I'd like pi to display as 3.14 and I'd like 10*pi to display as 31.42.

mike newman gravatar imagemike newman ( 2019-01-29 23:02:42 +0100 )edit

Then ask for 4 digits. What do you want to do with 0.0032? You want to display 0.00? You can figure out how many digits you actually want to see by computing the logarithm in base 10 of the absolute value of your number (= number of digits of the integer part).

vdelecroix gravatar imagevdelecroix ( 2019-01-30 10:24:51 +0100 )edit

Actually, yes, I want 0.0032 to display as 0.00 (not always of course, but in this case).

I have a bunch of values, and I want to see them all rounded off to the same decimal digit. They have different magnitudes, so asking for a number of significant digits is not the same thing. I am only talking about displaying them, not about actually rounding off the values for further calculation.

Yes I can compute the number of significant digits required for every number I wish to display, and then display it with that number of significant digits, but I was hoping for osme way of saying "Hey, Sagemath, can you please just display everything to two decimals for the moment!" :-)

mike newman gravatar imagemike newman ( 2019-02-13 10:57:45 +0100 )edit
0

answered 2019-01-30 15:09:37 +0100

dan_fulea gravatar image

Just use the usual pythonic way to format numbers. For instance:

myList = (pi, exp(16), sqrt(2019))
for x in myList:
    print "%.2f" % x

The above gives:

3.14
8886110.52
44.93

(Using format there are some problems, but the above works and it is enough for our purposes.) So in our case, in a dialog with the sage interpreter:

sage: myList = sorted([ 2*cos(2*pi*j/5) for j in range(5) ])
sage: myList
[-1/2*sqrt(5) - 1/2,
 -1/2*sqrt(5) - 1/2,
 1/2*sqrt(5) - 1/2,
 1/2*sqrt(5) - 1/2,
 2]
sage: print [ '%.2f' % x for x in myList ]
['-1.62', '-1.62', '0.62', '0.62', '2.00']
sage: print ', '.join( [ '%.2f' % x for x in myList ] )
-1.62, -1.62, 0.62, 0.62, 2.00
edit flag offensive delete link more

Comments

This accomplishes what I want; is there a way to apply this to displaying the entries of a matrix?

Ideally I would like to be able to set the "default" method for displaying floating point. At the moment the default (at least on my machine) seems to be to display every floating point number to 15 significant digits.

mike newman gravatar imagemike newman ( 2019-02-13 11:06:04 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2019-01-29 01:15:51 +0100

Seen: 6,399 times

Last updated: Jan 30 '19