I have typed out the following block of code in LaTex/SageTex.

    q1 = 10 - 10*p1
    q2 = 5 - 5*p2

B is $B=\sage{B}$.

The output is

B is B = 25.0000000000000 (p1 − 1.00000000000000)(p2 − 1)−14.0000000000000p1 − 10p2 + 24.0000000000000.

I would like for it to print out

B is B = 25 (p1 - 1)(p2 - 1)- 14p1 - 10p2 + 24.

I have looked through the Sagemath documentation without much luck. Apparently there is this method n() that takes a digit parameter and returns a truncated version of the expression. However, this returns a truncated version of the entire expression (which has no effect) not the floats that are in it.

  • $\begingroup$ An answer that won't work in general, but will work in your case, is to not use 1.4 and 0.5 but 14/10 and 1/2 instead. Maybe all your examples are really rational so it would be fine to do that? $\endgroup$
    – kcrisman
    Mar 13, 2015 at 13:00

1 Answer 1


I think that this question on ask.sagemath.org might be helpful, though you are right about applying it to the entire expression not being entirely straightforward. You could walk the expression tree and apply this but it's not so easy to do that either.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.