Displaying a smaller number of 0 for a Real call

asked 2023-02-23 16:39:42 +0100

Cyrille gravatar image

Suppose I define the function

var('A y')
f(A,x,y)= A*x^.25*y^.25

If I call f(A,x,y), Sagemath returns A*x^.250000000000000*y^.250000000000000. I suppose that is the approximation of reals by defaults. But I would like to know if I can shorten the number of 0.

edit retag flag offensive close merge delete

Comments

For example:

var('A y')
f(A,x,y)= A*x^n(.25,10)*y^n(.25,10)
f(A,x,y)
achrzesz gravatar imageachrzesz ( 2023-02-23 19:09:44 +0100 )edit

The question is ; does this approximation has any consequences in subsequent operations ?

Cyrille gravatar imageCyrille ( 2023-02-23 22:24:44 +0100 )edit

Check:

0.25==n(0.25,10)
True

but such cosmetics can be safe only for presentation, not in calculations (where more bits of exponents can be needed). See also https://ask.sagemath.org/question/493...

n(0.25) is 0.25
False

0.25.parent(),   n(0.25,10).parent()
(Real Field with 53 bits of precision, Real Field with 10 bits of precision)

Something like numpy.set_printoptions would be nice but we still have

u = 0.25
print("A*x^%.2f*y^%.2f" % (u,u))
A*x^0.25*y^0.25
achrzesz gravatar imageachrzesz ( 2023-02-24 00:35:13 +0100 )edit

Independent of @achrzesz's comment (which should be an answer, for the benefit of future ask.sagemath.org (per-)users) : do you have imperative reasons to use 0.25 (inexact numerical approximation) rather than 1/4 (exact rational) ?

The former will coerce any future result of your function to RR, thus depriving you of possible simplifications using the exact value 1/4...

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2023-02-24 10:28:57 +0100 )edit