The following C code works in a certain sense --- it gives you the following most negative integer, which is represented as a floating point number:
double flooor(double x) { return floor(nextafter(x, -1.0/0.0)); }
The following Python code is direct transliteration, but it relies on NumPy:
def flooor(x): return math.floor(numpy.nextafter(x, -numpy.inf))
The nextafter function moves from its first argument one double closer to the second argument. He has a special case; if z < 0 , and you ask for nextafter(0.0, z) , it will return the smallest negative negative number.
It is not clear from your specification what to do with positive infinity and the most negative finite number. This code sends positive infinity to the most positive finite number, negative infinity to itself and to the most negative finite number to negative infinity.
Martijn Pieters gave the int(math.ceil(x)) - 1 spell in his answer since it was deleted. This correctly finds the largest int less than float x . This round x up, converts it to an integer and subtracts 1, giving the largest Python int , which is numerically smaller than x .
tmyklebu
source share