好的,
我遇到了一个舍入错误问题。我有一个 java 程序,它逐行读取一些数字列表。该程序要求此列表为浮点数(什么类型的浮点数?我怀疑是单精度)数字,其总和 S 在 0.999 和 1.001 之间(即 0.999 <= S <= 1.001)。这是一个例子:
from numpy import array, linalg
def Normalize(X):
NumpyX = array(X)
Norm = linalg.norm(NumpyX)
Normalize = NumpyX/Norm
return Normalize
def FileMaker(FilePointer,Vector)
for i in Vector:
FilePointer.write('%f\n'%i)
return
#sum(SubstitutionPoints) does not add to 1
SubstitutionPoints =[0.00606508512067950,0.00675296642376962,0.00688999694872917,0.00580692396866418,0.00680583604896024,0.00609061670962565,0.00585881991631447,0.00577148570812953,0.00600882981888663,0.00618499536435559,0.00650767341787896,0.00670521809234427,0.00699374780209504,0.00841141135948587,0.00830145870238677,0.00879477131238090,0.00918627324146331,0.00958946761973615,0.01032404247887830,0.01093417870737930,0.01188202458790520,0.01261860720648550,0.01355451051017660,0.01473818756656830,0.01623978223562570,0.01811682034513980,0.01990010225231130,0.02154250858435480,0.02418185925226890,0.02583490296173980,0.02844212438633430,0.03194935989118780,0.03534228607419560,0.03971366519834600,0.04548573525944540,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660]
NormedSP = Normalize(SubstitutionPoints)
SubFile = open('Sub.txt','w')
FileMaker(SubFile,NormedSP)
SubFile.close()
由于python不使用单精度浮点数,我担心创建的文件会导致我的java程序出现问题,因为我的java程序(metasim)返回错误:
Simulating files:
[BorreliaBurgdorferiB31_CP32-3.fasta]
java.lang.Exception: Substitution rates don't sum to 1.0.
!!! FAILED !!!
不幸的是,我无法调试 java 程序,因为它是“java 可执行文件”。我唯一的选择是提供正确的输入。
任何建议/帮助表示赞赏。
编辑
从尝试 float32() 的一些建议中,我修改了我的规范化方法:
def Normalize(self,X):
Total = sum(X)
NumpyX = array([float32(i) for i in X])
Norm = linalg.norm(NumpyX,ord=1)
Normalize = NumpyX/Norm
return Normalize
我开始怀疑这是单精度浮点问题。
我的输出是否被截断并导致错误?