Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I am trying to make a function that would make rounding real numbers in an expression possibly consisting of real numbers, integer numbers and symbols. An example of such an expression is below:

expr = 1.2345 + 3.2471*x + 4.3946*x^2 + 5.56789*x^3 + 3.4812*x^4 + 
   1.34682*x*Exp[-2.3467*x^3] + 13.25684*x^3*Exp[23.9476*x^7] + 
   Sin[34.876459*x^2];

What I want to achieve is the following. Let us say, I would like to have all real numbers to have not more that two figures after comma. The expression then should then look as follows:

    1.23 + 3.25 x + 1.35 E^(-2.35 x^3) x + 4.39 x^2 + 5.57 x^3 + 
 13.26 E^(23.95 x^7) x^3 + 3.48 x^4 + Sin[34.88 x^2]

Such a function is useful, to show dynamically and in a compact form a result obtained in the course of dynamic calculation. For example, I often fit data to a function, the both being dependent upon parameter(s). To show the fitting function dynamically in the same Panel or Manipulate window I need to cut number of figures after comma of real numbers.

At present I am here:

    rnd[expr_, m_Integer] := 
  Map[If[NumberQ[#], 
     If[Element[#, Reals], (Round[#*10^m]/10^m // N), 
      IntegerPart[#]], #] &, expr, {1, Depth[expr]}];

Here expr is any expression to subject the rounding, m is the integer equal to the number of figures after comma to be left.

This function almost works. Its application to the expression given above yields the following:

rnd[expr, 2]

    (* 
1.23 + 3.25 x + 1.35 E^(-2.35 x^3.) x + 4.39 x^2. + 5.57 x^3. + 
     13.26 E^(23.95 x^7.) x^3. + 3.48 x^4. + Sin[34.88 x^2.] 
*)

You may see that the difference between what I get and what I want is that the powers are 2., 3. and so on instead of 2, 3 and so on. I cannot understand, why these decimal points show up, and how to get rid of them.

share|improve this question
2  
It seems this suits Your needs: expr /. x_Real :> Round[x, .01] –  Kuba Jun 13 '13 at 9:25
2  
...alternatively, expr /. x_?InexactNumberQ :> Round[x, 0.01]. –  J. M. Jun 13 '13 at 9:26
    
@Kuba Thank you, it indeed suits my need. –  Alexei Boulbitch Jun 13 '13 at 11:50
    
0x4A4D Thank you. –  Alexei Boulbitch Jun 13 '13 at 11:51
add comment

1 Answer 1

up vote 1 down vote accepted

As regards your final comment: "I cannot understand why these decimal points show up", and interested by your use of Element, it appears the integers are getting converted because they are found to be in the domain of real numbers, which I wouldn't have expected.

Element[1, Reals]

True

So your function can be made to work thusly:

rnd[expr_, m_Integer] := Map[If[NumberQ[#],
    If[Element[#, Reals] && Not[Element[#, Integers]],
     (Round[#*10^m]/10^m // N), #], #] &, expr, {1, Depth[expr]}]
share|improve this answer
2  
Succinctly put, 1 is real, but not Real, while 1. is both real and Real. The "Mathematica sense" of Real (inexact number with zero imaginary part) versus the "mathematics sense" of real (any admissible element of $\mathbb R$), which Element[] tests for, are seen to be different. –  J. M. Jun 13 '13 at 16:10
    
@Chris Degnen Thank you. –  Alexei Boulbitch Jun 14 '13 at 8:48
    
@Chris, actually it is a useful comment –  Alexei Boulbitch Jun 18 '13 at 11:11
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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