[英]Calculation with approximate digits in Wolfram Mathematica
I have a problem calculating with significant figures in Wolfram Mathematica.我在计算 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]
.但是,我首先想将 a 和 b 之间的乘积结果近似为三位有效数字,然后将其乘以Sin [25]
。 In short, I would like a function like this简而言之,我想要一个像这样的 function
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
.而不是381.62
。
How should I modify f1[a_, b_]
to get f1[92,0, 9.81]=381.62
?我应该如何修改f1[a_, b_]
以获得f1[92,0, 9.81]=381.62
?
You can use Round
to round to 3 significant digits in your specific case.在您的特定情况下,您可以使用Round
舍入到 3 个有效数字。 Then the result is an integer, so Sin[25]
does not convert to a real number (a floating point number).那么结果是 integer,所以Sin[25]
不会转换为实数(浮点数)。 However this can be forced with N
.但是,这可以通过N
强制执行。
Also Sin
assumes radian input unless the input is specified as degree. Sin
也假定弧度输入,除非输入指定为度数。
Note use of SetDelayed
( :=
) for the function definition.注意对 function 定义使用SetDelayed
( :=
)。
f[a_, b_] := N[Round[a b] Sin[25 Degree]]
f[92.0, 9.81]
381.624 381.624
For 3 significant digits on ab
in general you can use通常,对于ab
上的 3 个有效数字,您可以使用
f[a_, b_] := N[Round[a b, 10^(-3 + Floor[Log10[Abs[a b]]] + 1)] Sin[25 Degree]]
Eg rounding ab
例如四舍五入ab
a = 1.2345;
b = 5.4321;
N[Round[a b, 10^(-3 + Floor[Log10[Abs[a b]]] + 1)]]
6.71 6.71
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.