Jump to content

Mathematical floor function


Andz

Recommended Posts

It sounds so simple that I'm almost ashamed of asking this. Some folks here from the programming area are doubting some results of a floor function in C#, so they asked what was the math behind the floor and ceiling functions.

Does anyone know a mathematical way to convert an float to integer, floor or ceiling?

Link to comment
Share on other sites

It sounds so simple that I'm almost ashamed of asking this. Some folks here from the programming area are doubting some results of a floor function in C#, so they asked what was the math behind the floor and ceiling functions.

Does anyone know a mathematical way to convert an float to integer, floor or ceiling?

floor() - return the integer that is less than or equal N

trunc() - strip off the decimal of N, rounding towards zero

ceil() - return the integer greater than or equal to N

If the floating point value is already an integer, all of the above return the same value.

If the floating point value is fractional and positive, floor(N) and trunc(N) return the same value. ceil(N) returns floor(N)+1.

If the floating point value is fractional and negative, ceil(N) and trunc(N) return the same value, and floor(N) returns ceil(N)-1.

ie:

floor(1.2) = 1
trunc(1.2) = 1
ceil (1.2) = 2

floor(-1.2) = -2
trunc(-1.2) = -1
ceil (-1.2) = -1

Is this what you're asking?

Link to comment
Share on other sites

Hi malexander, I would call that the programmatic way of doing it (if that even term even exists), what I was looking for would be a mathematical way of doing that. I know there are math functions in programming for that just like you mentioned.

I'd like to no how to mathematically convert 2.3 into 2

9a7be5d0fe8cba7090d2127aea9990f9.png

9acb0ccdd857452d6dc078357d230d18.png

In other words, not using programming therms, convert a real number into an integer.

The closest I could find but haven't verified it is:

x - (((x *10):10)/10)

where :10 is the integer remaining of the number divided by 10, but to me that sounds almost like type casting :rolleyes: And that would only work for one decimal place.

Edited by Andz
Link to comment
Share on other sites

Hi malexander, I would call that the programmatic way of doing it (if that even term even exists), what I was looking for would be a mathematical way of doing that.

Looking at wikipedia, it looks like floor is also a proper mathematical operation, but it is indeed interesting to think of how to achieve it with other operations.

Wikipedia does give a series solution, 349a0a6b840af1b8b5972a1a14b31232.png

Link to comment
Share on other sites

Hi guys, thank you for the help.

RDG, I'm not a pro-programmer, but what I think they are doing there is exactly what I was trying to avoid, is the type casting, just telling the computer to ignore what ever is in the decimal places of the number. But of course verifying if the number is positive or negative so that their floor function work well. The coders found out that what was going on was exactly an error that can occur with type casting, internally they were getting the right value from the floor function but the output window was doing some black magic with their result and printing out absurd results like floor(2) = 1, when I decided to calculate what they were doing by hand, 1 was the right answer, the error was that internally the program was calculating the floor(1,9999... someting) and not 2 like it was printing.

SYmek, I have also heard that when I started to wonder around about this, if I understood what edward meant, he is pointing to that as well.

eetu At first I thought that this should be a simple question, since the first thing that came to my head were the step functions:

floor_graph.gif

Only now I realized that I have always used a human interpretation for them, that is... just get the closest integer and you're set... just don't forget to check if it is positive or negative. The only problem that I see in the series, is that (if im not mistaken) it will never stabilize and I won't end up with a integer :blink:

Edited by Andz
Link to comment
Share on other sites

That is NOT what floor() is supposed to do. I think you're looking for round() instead?

I was trying to generalize round, ceiling and floor in just one grasp :P

I'm sorry, I should have said the highest integer below or equal x. 9a7be5d0fe8cba7090d2127aea9990f9.png

Edited by Andz
Link to comment
Share on other sites

Hi Andz, It might be little off the track but thought of sharing.

Intrigued by your deceptively simple question, I could not resist asking it to perhaps the most well know face of Discrete Mathematics."Professor Arthur Benjamin", at least for general public like me.

http://www.math.hmc.edu/~benjamin/benjamin.info.html

He just generously replied to my mail:

""

Its a Good question. Not sure how to do it without using a function like

floor or trunc.

Professor Benjamin

""

Cheers !!

Edited by vectorblur
Link to comment
Share on other sites

Thanks for checking that out vectorblur, I was about to ask one of my former professors about that.

I ran some test on the little expression I wrote above few posts and it works on all cases I have tried:

x - (((x *10) mod 10)/10) where 10 is the amount of decimal places multiple of 10 I need to take care off.

for x = 2.6,

floor(x) = 2.6 - ((( 2.6 * 10) mod 10)/10)
         = 2.6 - ((26 mod 10) / 10)
         = 2.6 - (6 / 10)
         = 2.6 - 0.6
         = 2

But for 2.65 I would have to try:

floor(x) = 2.65 - ((( 2.65 * 100) mod 100)/100)

The good thing is that 100 or 100000000... would work just as well for just one decimal place, so I don't need to keep changing it:

floor(x) = 2.6 - ((( 2.6 * 1000) mod 1000)/1000)
= 2.6 - ((2600 mod 1000)/1000)
= 2.6 - (600/1000)
= 2.6 - 0.6
= 2

Of course these are just pin point tests, I'd need to run a formal proof of this. I would first need to figure out how to deal with the modulo and "the highest multiple of 10"

Edited by Andz
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...