I didn't find an answer to this question on the site, but as I was writing up my question, I figured out how to do it, so I thought I should post it and answer my own question so that others can benefit from it.

This is a similar question to the one found here, but I am looking for a more general solution.

Suppose I have a complicated structure, which can include functions (some defined, some not), lists (sometimes nested), replacement rules, Quantity expressions, and so on. I need to modify the precision of any number that is not an integer or a ratio of integers, but want to leave the integers and ratios unchanged. How can I do that? Examples:

rxn[a + 1/2 b, 2 c + d, Quantity[1.03*10^-2, "Molar"]]

{x[1] -> Quantity[2.4*10^3, "Molar"], x[2] -> 0, 
          x[3] -> Quantity[16.2, "Moles"], x[4] -> 3.2}

Here I want to change the precision of the three values within the Quantity statements and the 3.2, but nothing else.

Answer below.

Replace[exp1, a:(_Real|_Complex) :> SetPrecision[a, 10], All]
| improve this answer | |

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.