# How to use a decimal range() step value?

How to use a decimal range() step value?

Asked on October 13, 2018 in

Rather than employing a decimal step directly, it’s a lot of safer to precise this in terms of what number points you wish. Otherwise, floating-point rounding is probably going to present you a wrong result.

You can use the linspace operate from the NumPy library (which is not a part of the quality library however is comparatively simple to obtain). linspace takes variety of points to come back, and additionally allows you to specify whether or not or to not embrace the proper 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 actually wish to use a floating-point step price, 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])```

Floating-point rounding can cause issues, though. Here’s an easy case wherever rounding causes arange turn out|to supply|to provide} a length-4 array once it ought to solely produce three numbers:

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

Python’s range() will solely do integers, not floating purpose. In your specific case, you’ll use a listing comprehension instead:

```[x * 0.1 for x in range(0, 10)]
```

(Replace the decision to vary thereupon expression.)
For the additional general case, you’ll wish to jot down a custom perform or generator.

scipy features a inbuilt perform arange that generalizes Python’s range() builder to satisfy your demand of float handling.

```from scipy import arrange

```

Sample code is

```  for d100 in xrange(0, 100, 1):
d = d100 / 100.0
fl = float("0.00"[:4 - len(str(d100))] + str(d100))
print d, "=", fl , d == fl
```

The given code for the above question is

```def frange(start, end=None, inc=None):
"A range function, that does accept float increments..."
if end == None:
end = start + 0.0
start = 0.0
if inc == None:
inc = 1.0
L = []
while 1:
next = start + len(L) * inc
if inc > 0 and next >= end:
break
elif inc < 0 and next <= end:
break
L.append(next)
return L
```
``````USING ITERTOOLSimport itertools

def seq(start, end, step):
if step == 0:
raise ValueError("step must not be 0")
sample_count = int(abs(end - start) / step)
return itertools.islice(itertools.count(start, step), sample_count)``````

Usage Example:

``````for i in seq(0, 1, 0.1):
print(i)``````

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.

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])
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])
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])

``````#include <stdio.h>
#include <math.h>
long long convertDecimalToBinary(int n);

int main()
{
int n;
printf("Enter a decimal number: ");
scanf("%d", &n);
printf("%d in decimal = %lld in binary", n, convertDecimalToBinary(n));
return 0;
}

long long convertDecimalToBinary(int n)
{
long long binaryNumber = 0;
int remainder, i = 1, step = 1;

while (n!=0)
{
remainder = n%2;
printf("Step %d: %d/2, Remainder = %d, Quotient = %d\n", step++, n, remainder, n/2);
n /= 2;
binaryNumber += remainder*i;
i *= 10;
}
return binaryNumber;
}``````