草庐IT

Round-towards-Infinity

全部标签

python - 如何修复此 python 错误?溢出错误 : cannot convert float infinity to integer

它给我这个错误:Traceback(mostrecentcalllast):File"C:\Users\Public\SoundLog\Code\CódigoPython\SoundLog\Plugins\NoisePlugin.py",line113,inonPaintdc.DrawLine(valueWI,valueHI,valueWF,valueHF)File"C:\Python26\lib\site-packages\wx-2.8-msw-unicode\wx\_gdi.py",line3177,inDrawLinereturn_gdi_.DC_DrawLine(*args,*

python - Round 在 Python 中向下 float 以仅保留一位非零小数

我有一个只用float填充的Python列表:list_num=[0.41,0.093,0.002,1.59,0.0079,0.080,0.375]我需要将此列表四舍五入以获得:list_num_rounded=[0.4,0.09,0.002,1.5,0.007,0.08,0.3]问题:将1.59四舍五入到1.5很容易做到。但是,我的问题是float小于1。问题:基本上,我需要将所有float向下舍入,以便:如果float尝试:这是我尝试过的:list_num_rounded=[]foreleminlist_num:ifelem>0.01andelem0.001andelem0.1:l

python - python中两个列表混合的Round Robin方法

如果输入是round_robin(range(5),"hello")我需要输出为[0,'h',1,'e',2,'l',3,'l',4,'o']我试过了defround_robin(*seqs):list1=[]length=len(seqs)list1=cycle(iter(items).__name__foritemsinseqs)whilelength:try:forxinlist1:yieldxexceptStopIteration:length-=1pass但它给出错误为AttributeError:'listiterator'objecthasnoattribute'__na

小数选项大于 2 的 python np.round()

Python有默认的round()函数,但我用cython编程,想用numpy函数替换pythonic代码。但是,在终端中进行实验时,我得到了以下结果。>>>np.around(1.23456789)1.0>>>np.around(1.23456789,decimals=0)1.0>>>np.around(1.23456789,decimals=1)1.2>>>np.around(1.23456789,decimals=2)1.23>>>np.around(1.23456789,decimals=3)1.2350000000000001>>>np.around(1.23456789,d

python - Sci-Kit 学习 SGD 算法时出错 - "Array contains NaN or infinity"

我收到一条错误消息,指出“数组包含NaN或无穷大”。我已经检查了我的数据,包括训练/测试缺失值,没有遗漏任何东西。我可能对“数组包含NaN或无穷大”的含义有错误的解释。importnumpyasnpfromsklearnimportlinear_modelfromnumpyimportgenfromtxt,savetxtdefmain():#createthetraining&testsets,skippingtheheaderrowwith[1:]dataset=genfromtxt(open('C:\\Users\\Owner\\training.csv','r'),delimit

python - 为什么 round(x) 和 round(np.float64(x)) 有区别?

据我了解,2.675和numpy.float64(2.675)都是相同的数字。然而,round(2.675,2)给出2.67,而round(np.float64(2.675),2)给出2.68。为什么会这样?importnumpyasnpfromdecimalimportDecimalx=2.675np_x=np.float64(x)type(x)#floatDecimal(x)#Decimal('2.67499999999999982236431605997495353221893310546875')Decimal(np_x)#Decimal('2.6749999999999998

python - 错误 "TypeError: type numpy.ndarray doesn' t 定义 __round__ 方法”

importnumpy......#Predictionpredictions=model.predict(X_test)#roundpredictionsrounded=[round(x)forxinpredictions]print(rounded)"predictions"isalistofdecimalsbetween[0,1]withsigmoidoutput.为什么总是报这个错:File"/home/abigail/workspace/ml/src/network.py",line41,inrounded=[round(x)forxinpredictions]TypeErr

python 2.7 : round a float up to next even number

我想将float四舍五入到下一个偶数。步骤:1)检查一个数是奇数还是偶数2)如果是奇数,四舍五入到下一个偶数我已经准备好第1步,一个检查给定数字是否为偶数的函数:defis_even(num):ifint(float(num)*10)%2==0:return"True"else:return"False"但我正在为第2步而苦苦挣扎......有什么建议吗?注意:所有float都是正值。 最佳答案 不需要步骤1。只需将值除以2,四舍五入到最接近的整数,然后再次乘以2:importmathdefround_up_to_even(f):r

SQL Server Round将5解释为较低

例如,我想在SQLServer2012中汇总小数:SelectROUND(1.056,2)--returns1.06SelectROUND(1.055,2)--returns1.06SelectROUND(1.054,2)--returns1.05如果5降低,我该如何使第二个查询返回1.05将第三个小数四舍五入?看答案您可以使用此。它将对您有用。DECLARE@testdecimal(10,3)=1.055SELECTCASEWHENround(@test,3,1)-round(@test,2,1)=0.005THENround(@test,2,1)ELSEround(@test,2)END

PostgreSQL round函数使用总结

在使用sql函数计算的时候有时候要保留小数位,有时候不需要,下面就总结一下PostgreSQL中round函数的使用注意事项以及一些踩过的坑1.整数相除得到不是理想的数:3/9=0.0000SELECTround(3/9,4);这里是因为保留四位小数,3和9都是整型,本来结果是0.33333无限循坏,小数位的3直接舍掉然后补充4位小数0。同理10/9结果就会是1.0000SELECTround(10/9,4);  如果想要获取到正确结果那么就得运用到PostgreSQL的numeric(可选精度的准确数字)还得提到PostgreSQL的另一个cast函数,很多时候需要转换数据类型。Postgr