Python Forum
Inclusive (closed) range with float numbers
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Inclusive (closed) range with float numbers
#1
Hi

I need to add range of numbers to some arrays used, but I see that range in Python is not for float numbers and is not inclusive, namely range is not including the last number.

Seems that we need to use extra functions to complement the default function in Python.

I was able to get ranges using floats using this function:
def float_range(start, stop, skip = 1.0, decimals = 2):
	for i in range(int(start / skip), int(stop / skip)):
		yield float(('%0.' + str(decimals) + 'f') % (i * skip))

my_float_range = float_range(0.45, 0.6, 0.01)

all_combo = list(my_float_range)
print(f'size={len(all_combo)}')
pprint(all_combo)
Output:
size=15 [0.45, 0.46, 0.47, 0.48, 0.49, 0.5, 0.51, 0.52, 0.53, 0.54, 0.55, 0.56, 0.57, 0.58, 0.59] BUT I need to get in the array 0.60 too!!
I have this function to get inclusive ranges, but is not valid for float numbers
def closed_range(slices):
	slice_parts = slices.split(',')
	[start, stop, step] = map(int, slice_parts)
	num = start
	if start <= stop and step > 0:
		while num <= stop:
			yield num
			num += step
	# if negative step
	elif step < 0:
		while num >= stop:
			yield num
			num += step

all_combo = list(closed_range('1, 6, 1'))
print(f'size={len(all_combo)}')
pprint(all_combo)
Output:
size=6 [1, 2, 3, 4, 5, 6]
How can I get inclusive ranges also valid for float numbers?

Thank you very much for your reply

Mapg
Reply
#2
You could call the function with the end as the next step my_float_range = float_range(0.45, 0.61, 0.01)
or alter the function to have an inclusive option.
def float_range(start, stop, skip = 1.0, decimals = 2, inclusive=True):
    if inclusive:
        stop += skip
    for i in range(int(start / skip), int(stop / skip)):
        yield float(('%0.' + str(decimals) + 'f') % (i * skip))
Reply
#3
Use
>>> import numpy as np
>>> np.linspace(0.45, 0.6, 16)
array([ 0.45,  0.46,  0.47,  0.48,  0.49,  0.5 ,  0.51,  0.52,  0.53,
        0.54,  0.55,  0.56,  0.57,  0.58,  0.59,  0.6 ])
Notice this in the documentation of numpy.arange()
numpy documentation Wrote:When using a non-integer step, such as 0.1, the results will often not
be consistent. It is better to use linspace for these cases.
Reply
#4
Obviously, it's very practical what you mentioned. For now, it's the simplest solution.

Thanks Yoriz

(Apr-27-2019, 08:47 PM)Gribouillis Wrote: Notice this in the documentation of numpy.arange()
[quote=numpy documentation] When using a non-integer step, such as 0.1, the results will often not
be consistent. It is better to use linspace for these cases.

Yes, I am getting an extremely long float at 0.47

I am formatting all this as follows ...
import numpy as np
from numpy import linspace

myRange = list(np.linspace(0.45, 0.6, 16))
myFormattedRange = ['%.2f'% elem for elem in myRange]
Output:
print(myRange) [0.45, 0.46, 0.47000000000000003, 0.48, 0.49, 0.5, 0.51, 0.52, 0.53, 0.54, 0.55, 0.56, 0.57, 0.58, 0.59, 0.6] print(myFormattedRange) ['0.45', '0.46', '0.47', '0.48', '0.49', '0.50', '0.51', '0.52', '0.53', '0.54', '0.55', '0.56', '0.57', '0.58', '0.59', '0.60']
Reply
#5
Quote:Yes, I am getting an extremely long float at 0.47
Read this page in the python documentation. It is not a limitation of python, it is a limitation of the way computers handle floating numbers (namely the IEEE-754 format)
>>> 0.45 + 0.01 + 0.01
0.47000000000000003
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  This result object does not return rows. It has been closed automatically dawid294 5 676 Jan-10-2024, 10:55 PM
Last Post: deanhystad
  matplotlib x axis range goes over the set range Pedroski55 5 3,102 Nov-21-2021, 08:40 AM
Last Post: paul18fr
  A question about 'Event loop is closed' fc5igm 2 2,164 Oct-05-2021, 02:00 AM
Last Post: fc5igm
  ValueError: I/O operation on closed file problem aliwien 0 2,074 Apr-23-2021, 05:50 PM
Last Post: aliwien
  Run an app in a standalone terminal and wait until it's closed glestwid 2 2,446 Aug-30-2020, 08:14 AM
Last Post: glestwid
  Return prime numbers from range krzyfigh 2 1,885 Apr-20-2020, 08:08 PM
Last Post: krzyfigh
  Define a range, return all numbers of range that are NOT in csv data KiNeMs 18 6,873 Jan-24-2020, 06:19 AM
Last Post: KiNeMs
  Comaparing Float Values of Dictionary Against A Float Value & Pick Matching Key firebird 2 3,324 Jul-25-2019, 11:32 PM
Last Post: scidam
  Print Numbers starting at 1 vertically with separator for output numbers Pleiades 3 3,662 May-09-2019, 12:19 PM
Last Post: Pleiades
  VMware View - Session opened/closed? hhh 0 2,294 May-31-2018, 10:10 AM
Last Post: hhh

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020