久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何使用小數范圍()步長值?

How to use a decimal range() step value?(如何使用小數范圍()步長值?)
本文介紹了如何使用小數范圍()步長值?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

有沒有辦法在 0 和 1 之間步進 0.1?

Is there a way to step between 0 and 1 by 0.1?

我以為我可以這樣做,但它失敗了:

I thought I could do it like the following, but it failed:

for i in range(0, 1, 0.1):
    print i

相反,它說 step 參數不能為零,這是我沒想到的.

Instead, it says that the step argument cannot be zero, which I did not expect.

推薦答案

比起直接使用小數步長,用你想要的點數來表達要安全得多.否則,浮點舍入錯誤很可能會給你一個錯誤的結果.

Rather than using a decimal step directly, it's much safer to express this in terms of how many points you want. Otherwise, floating-point rounding error is likely to give you a wrong result.

您可以使用 linspace 函數NumPy 庫(它不是標準庫的一部分,但相對容易獲得).linspace 需要返回多個點,還可以讓您指定是否包含正確的端點:

You can use the linspace function from the NumPy library (which isn't part of the standard library but is relatively easy to obtain). linspace takes a number of points to return, and also lets you specify whether or not to include the right endpoint:

>>> np.linspace(0,1,11)
array([ 0. ,  0.1,  0.2,  0.3,  0.4,  0.5,  0.6,  0.7,  0.8,  0.9,  1. ])
>>> np.linspace(0,1,10,endpoint=False)
array([ 0. ,  0.1,  0.2,  0.3,  0.4,  0.5,  0.6,  0.7,  0.8,  0.9])

如果你真的想使用浮點步進值,你可以使用 numpy.arange.

If you really want to use a floating-point step value, you can, with numpy.arange.

>>> import numpy as np
>>> np.arange(0.0, 1.0, 0.1)
array([ 0. ,  0.1,  0.2,  0.3,  0.4,  0.5,  0.6,  0.7,  0.8,  0.9])

浮點舍入誤差導致問題.這是一個簡單的例子,其中舍入錯誤導致 arange 在它應該只產生 3 個數字時產生一個長度為 4 的數組:

Floating-point rounding error will cause problems, though. Here's a simple case where rounding error causes arange to produce a length-4 array when it should only produce 3 numbers:

>>> numpy.arange(1, 1.3, 0.1)
array([1. , 1.1, 1.2, 1.3])

這篇關于如何使用小數范圍()步長值?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Python 3 Float Decimal Points/Precision(Python 3 浮點小數點/精度)
Converting Float to Dollars and Cents(將浮點數轉換為美元和美分)
What are some possible calculations with numpy or scipy that can return a NaN?(numpy 或 scipy 有哪些可能的計算可以返回 NaN?)
Python float to ratio(Python浮動比率)
How to manage division of huge numbers in Python?(如何在 Python 中管理大量數字的除法?)
mean from pandas and numpy differ(pandas 和 numpy 的意思不同)
主站蜘蛛池模板: 黑人精品欧美一区二区蜜桃 | 国产高清精品一区 | 91精品国产综合久久婷婷香蕉 | 综合第一页 | 亚洲日韩视频 | 欧美一区二区三区在线 | 午夜视频在线播放 | 欧美日韩电影一区二区 | 日韩福利在线观看 | 午夜欧美| 国产在线观看不卡一区二区三区 | 九九热这里 | av黄色在线 | 完全免费在线视频 | 精品无码久久久久国产 | 欧美性网| 最新免费av网站 | 日韩一区二区福利视频 | 欧美aaaaaaaa | 精品久久99 | 二区中文 | 亚洲激情在线观看 | 我想看国产一级毛片 | 久久狼人天堂 | 免费看的av | 97伦理影院 | 欧美白人做受xxxx视频 | 久久精片| 欧美在线视频一区 | 精品欧美乱码久久久久久1区2区 | 国产精品久久久久久久久久久新郎 | 中文字幕乱码亚洲精品一区 | 99在线精品视频 | 九九亚洲| 国产精品久久久久久影视 | 全免费a级毛片免费看视频免费下 | 天天射影院 | 国产精品亚洲二区 | 超碰成人免费观看 | 成人不卡| 国产人成精品一区二区三 |