Nas*_*ser 7 wolfram-mathematica
Mathematica 8.0.1
任何人都可以解释这个结果背后的逻辑
In[24]:= Round[10.75, .1]
Out[24]= 10.8
In[29]:= Round[2.75, .1]
Out[29]= 2.8000000000000003
Run Code Online (Sandbox Code Playgroud)
我预计上面的第二个结果是2.8?
编辑1:
我正在尝试上面的格式化目的只是为了使数字适合空间.我最终做了以下以获得我想要的结果:
In[41]:= NumberForm[2.75,2]
Out[41] 2.8
Run Code Online (Sandbox Code Playgroud)
我希望Mathematica有printf()之类的格式化功能.我在Mathematica中找到了确切字段宽度的格式编号,与使用printf()格式规则相比,形成了一点尴尬.
编辑2:我尝试$ MaxExtraPrecision = 1000我试图格式/回合的一些数字,但它没有用,这就是我发布这个问题的原因.这里是
In[42]:= $MaxExtraPrecision=1000;
Round[2035.7520395261859,.1]
Out[43]= 2035.8000000000002
In[46]:= $MaxExtraPrecision=50;
Round[2.75,.1]
Out[47]= 2.8000000000000003
Run Code Online (Sandbox Code Playgroud)
我找到了这种方法,只将数字格式化为一个小数点.使用Numberform,但首先需要通过计算小数点左边的位数来找到要使用的n位精度,然后加1.
In[56]:= x=2035.7520395261859;
NumberForm[x,IntegerLength[Round@x]+1]
Out[57]//NumberForm= 2035.8
Run Code Online (Sandbox Code Playgroud)
以上(编辑3)不适用于诸如此类的数字
a=2.67301785 10^7
Run Code Online (Sandbox Code Playgroud)
经过一些试验,我找到了会计表来做我想做的事.AccountingForm摆脱了NumberForm没有的10 ^ n形式:
In[76]:= x=2035.7520395261859;
AccountingForm[x,IntegerLength[Round@x]+1]
Out[77]//AccountingForm= 2035.8
In[78]:= x=2.67301785 10^7;
AccountingForm[x,IntegerLength[Round@x]+1]
Out[79]//AccountingForm= 26730178.5
Run Code Online (Sandbox Code Playgroud)
为了格式化数值,我找到的最好的语言是Fortran,遵循COBOL以及那些使用或支持printf()标准格式的语言.使用Mathematica,我可以肯定会做这样的格式化,但对我来说这看起来确实太复杂了.我从未理解为什么数学没有Printf [].
Sza*_*lcs 11
并非所有具有有限位数的十进制(基数10)数字都可以用二进制(基数2)表示,并且数字有限.例如,0.1不能用二进制表示,就像1/3~ = 0.33333 ...不能用十进制表示.Mathematica(和其他软件)在显示隐藏此效果的数字时仅使用有限数量的十进制数字.但是,偶尔会出现足够的十进制数字,表明不匹配变得可见.
http://en.wikipedia.org/wiki/Floating_point#Representable_numbers.2C_conversion_and_rounding
编辑
此命令将显示当您使用20个二进制数字找到0.1的结束二进制表示形式时会发生什么,然后将其转换回十进制:
RealDigits[FromDigits[RealDigits[1/10, 2, 20], 2], 10]
Run Code Online (Sandbox Code Playgroud)
Number/AccountingForm可以在第二个参数中取一个列表,其中第二项是要显示小数点后的位数:
In[61]:= x=2035.7520395261859;
In[62]:= AccountingForm[x,{Infinity,3}]
Out[62]//AccountingForm= 2035.752
Run Code Online (Sandbox Code Playgroud)
也许这很有用。
| 归档时间: |
|
| 查看次数: |
1573 次 |
| 最近记录: |