How do I use a decimal step value for range()?
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.
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])
If you really want to use a floating-point step value, use 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])
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])
How do I use a decimal step value for range()?
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.
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])
If you really want to use a floating-point step value, use 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])
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])
for loop in python with decimal number as step
use numpy
's arange instead of range:
import numpy as np
for x in np.arange(0, 1, 0.1):
print(x)
How do I use a decimal step value for range()?
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.
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])
If you really want to use a floating-point step value, use 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])
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])
range() for floats
I don't know a built-in function, but writing one like [this](https://stackoverflow.com/a/477610/623735) shouldn't be too complicated.
def frange(x, y, jump):
while x < y:
yield x
x += jump
---
As the comments mention, this could produce unpredictable results like:
>>> list(frange(0, 100, 0.1))[-1]
99.9999999999986
To get the expected result, you can use one of the other answers in this question, or as @Tadhg mentioned, you can use decimal.Decimal
as the jump
argument. Make sure to initialize it with a string rather than a float.
>>> import decimal
>>> list(frange(0, 100, decimal.Decimal('0.1')))[-1]
Decimal('99.9')
Or even:
import decimal
def drange(x, y, jump):
while x < y:
yield float(x)
x += decimal.Decimal(jump)
And then:
>>> list(drange(0, 100, '0.1'))[-1]
99.9
[editor's not: if you only use positive jump
and integer start and stop (x
and y
) , this works fine. For a more general solution see here.]
Python decimal range() step by step value
What you’re running into here is essentially the problem behind floating point alrithmetic. See this question for further information about what’s going on and why it happens.
To sum it up, just look at the results from your drange(0.1, 0.9, 0.1)
:
>>> list(drange(0.1, 0.9, 0.1))
[0.1, 0.2, 0.30000000000000004, 0.4, 0.5, 0.6, 0.7, 0.7999999999999999, 0.8999999999999999]
As you can see, you don’t get exact results there. So when you sum them up, you won’t get an exact 1
.
Instead, when comparing floats with rounded numbers, you should always allow for some kind of precision loss. One way to do that is to take the difference and see if it’s below some threshold (in this case, I chose 0.00001
):
if abs((a + b + c) - 1) < 0.00001:
print('The sum is likely 1')
So in your case, your code could look like this:
for a in drange(0.1, 0.9, 0.1):
for b in drange(0.1, 0.9, 0.1):
for c in drange(0.1, 0.9, 0.1):
if abs((a + b + c) - 1) < 0.00001 and a > b > c:
print a
print b
print c
And that will safely produce the expected output.
Range with step of type float
You could use numpy.arange
.
EDIT: The docs prefer numpy.linspace
. Thanks @Droogans for noticing =)
How do I make a for with decimals?
if you need to use range for float.
one way is to import arange from Numpy
from numpy import arange
def f(x):
result = x**2+(x/2)
return(result)
for x in arange(0.1, 0.10, 0.001):
print(x)
NB : in your case both start and end are same value (0.1) hence no output would be produced try changing the values to produce output.
Related Topics
How to Melt a Pandas Dataframe
How to Add a New Column to an Existing Dataframe
Using @Property Versus Getters and Setters
Use a List of Values to Select Rows from a Pandas Dataframe
Python Function Global Variables
Python Rounding Error With Float Numbers
How to Identify on Which Os Python Is Running On
How to Retrieve a Module'S Path
Understanding Repr( ) Function in Python
Tkinter - Executing Functions Over Time
Select Dataframe Rows Between Two Dates
Saving Utf-8 Texts With Json.Dumps as Utf8, Not as \U Escape Sequence
Unicodedecodeerror When Reading CSV File in Pandas With Python
How to Pass a String into Subprocess.Popen (Using the Stdin Argument)
Removing from a List While Iterating Over It
Is There a Portable Way to Get the Current Username in Python