久久久久久久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 的意思不同)
主站蜘蛛池模板: 午夜天堂精品久久久久 | 日本一区二区视频 | 久久久综合精品 | 国产精品国产三级国产aⅴ无密码 | 欧美一区二区另类 | 五月婷婷中文 | 国产乱码精品一区二区三区忘忧草 | 亚洲免费人成在线视频观看 | 色伊人 | 亚洲高清成人在线 | 久久999| 在线亚洲人成电影网站色www | 日韩精品一区二区三区视频播放 | 久久久久国产精品 | 粉色午夜视频 | 国产成人99久久亚洲综合精品 | 国产在线1 | 青青草久久| 国内精品免费久久久久软件老师 | 日韩免费一二三区 | 精品久久香蕉国产线看观看亚洲 | 91成人在线视频 | 成人影院av | 黄色免费网 | 欧美日韩一本 | 国产精品久久久乱弄 | 一区二区三区免费 | 欧美日一区 | 草草视频在线观看 | 久草网址| 日韩高清成人 | 成年人的视频免费观看 | 在线观看午夜视频 | 天天操天天干天天爽 | 日本久久视频 | 中文字幕一区二区三区日韩精品 | www.午夜| 丝袜一区二区三区 | 欧美日韩大片 | 国产一区二区成人 | 天天操天天干天天曰 |