wolfram-mathematicasignificant-digits

Calculation with approximate digits in Wolfram Mathematica


I have a problem calculating with significant figures in Wolfram Mathematica. Let me explain better.

I have

f[a_, b_] = a b Sin[25]

and

f[92.0 , 9.81] =381.421

However, I would first like to approximate the result of the product between a and b to three significant digits and then multiply it by Sin [25]. In short, I would like a function like this

f1[a_, b_] = NumberForm[a b, {3, 0}] Sin[25]

But if I evaluate

f1[92,0 , 9.81] 

I get

f1[92,0 , 9.81]= 903.Sin[25]

instead of 381.62.

How should I modify f1[a_, b_] to get f1[92,0 , 9.81]=381.62 ?


Solution

  • You can use Round to round to 3 significant digits in your specific case. Then the result is an integer, so Sin[25] does not convert to a real number (a floating point number). However this can be forced with N.

    Also Sin assumes radian input unless the input is specified as degree.

    Note use of SetDelayed (:=) for the function definition.

    f[a_, b_] := N[Round[a b] Sin[25 Degree]]
    
    f[92.0, 9.81]
    

    381.624

    For 3 significant digits on a b in general you can use

    f[a_, b_] := N[Round[a b, 10^(-3 + Floor[Log10[Abs[a b]]] + 1)] Sin[25 Degree]]
    

    E.g. rounding a b

    a = 1.2345;
    b = 5.4321;
    N[Round[a b, 10^(-3 + Floor[Log10[Abs[a b]]] + 1)]]
    

    6.71