Files
  • main.py
  • bins.txt
  • main-numpy.py
  • numbers.txt
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import ctypes
from math import inf

libc = ctypes.CDLL(None)
nextafter = libc.nextafter
nextafter.restype = ctypes.c_double
nextafter.argtypes = [ctypes.c_double, ctypes.c_double]
    

# assume sorted and all numbers are within bins range
with open('numbers.txt') as numbers_file, \
    open('bins.txt') as bins_file:
    bins = map(float, bins_file)
    b = next(bins)
    for x in map(float, numbers_file):
        while x > b:
            b = next(bins)
        if x == b:
            x = nextafter(x, -inf) # previous
        print(x)