How do I find the largest integer less than x?

pheon

If x is 2.3, then math.floor(x) returns 2.0, the largest integer smaller than or equal to x (as a float.)

How would I get i the largest integer strictly smaller than x (as a integer)?

The best I came up with is:

i = int(math.ceil(x)-1)

Is there a better way?

Note, that if x is 2.0 then math.floor(x) returns 2.0 but I need the largest integer smaller than 2.0, which is 1.

Lærne

math.ceil(x)-1 is correct and here is the proof.

if x is in Z (the set of integers), then math.ceil(x) = x. Therefore math.ceil(x)-1=x-1, the largest integer smaller than x.

Else we have x in R \ Z and math.ceil(x) is the smallest integer y such that xy. But then y-1 is an integer smaller than the smallest integer such that xy, therefore x > y-1 and by construction y-1 is the largest such integer smaller than x.

It's simple enough that I wouldn't bother with those if-else. But to avoid computation errors with floats I would do the -1 outside the int conversion.

int(math.ceil(x))-1

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I find the largest number in an array that is less than or equal to a random integer?

How do I scp files of size less than x?

How to find the largest power of 2 less than the given number

How can I find the coordinates of the largest all-true rectangle in a 2d array of booleans in less than O(n^3)?

Find largest permutation less than a certain value

while loop to find largest number less than

How do I select pairs from table with distance less than x?

How do I test if 2 canvas images are less than x number of pixels away on JS canvas?

How do I sort taking greater than and less than into consideration?

How to find a number which is less than and closest to number X in an array?

Find the largest element in a vector less than values in another vector in R

How to find less than by decode?

if integer is greater than x but less than y (Swift)

How do i find the largest value of an item in a nested dict in python?

How do I find largest valid sequence of parentheses and brackets in a string?

Excel VBA - How do I find the smallest and largest keys in a dictionary

How do I find the largest cluster in this simple dataset?

How Do I Find the Largest Number in a 3D Array?

How do i find the smallest or largest value in a list in Python

Pandas. How do I find the 10 largest values in a column?

How do I find the largest value of a cell in another column?

How do I find the index of an integer array?

How to find the second largest integer in an ArrayList

How do I change the height of GIMP to less than 1266 pixels?

How do I center an image with a width less than 100%?

How do I recursively count up to less than n

How do I use expect to test for less than

How do I scan less chars than defined into an array?

How do I less a filename rather than an inode number?