Andz Posted November 25, 2011 Share Posted November 25, 2011 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? Quote Link to comment Share on other sites More sharing options...
malexander Posted November 25, 2011 Share Posted November 25, 2011 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? Quote Link to comment Share on other sites More sharing options...
Andz Posted November 25, 2011 Author Share Posted November 25, 2011 (edited) 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 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 And that would only work for one decimal place. Edited November 25, 2011 by Andz Quote Link to comment Share on other sites More sharing options...
rdg Posted November 25, 2011 Share Posted November 25, 2011 Like this? http://www.gamedev.net/topic/379975-implementing-floor/ Quote Link to comment Share on other sites More sharing options...
symek Posted November 26, 2011 Share Posted November 26, 2011 Quite probably, floor() and ceil() functions rely on hardware specific implementations (perhaps based on modulo), not pure algebraic. I'm speculating... Quote Link to comment Share on other sites More sharing options...
zarti Posted November 26, 2011 Share Posted November 26, 2011 (edited) floor( val ) = val - modulo( val / 1 ) --test val = 2.74; floor( val ) = 2.00 Edited November 26, 2011 by zarti Quote Link to comment Share on other sites More sharing options...
edward Posted November 26, 2011 Share Posted November 26, 2011 The fastest way is to make direct use of the IEEE representation of the floating point number. eg. http://stereopsis.com/FPU.html#convert Quote Link to comment Share on other sites More sharing options...
eetu Posted November 26, 2011 Share Posted November 26, 2011 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, Quote Link to comment Share on other sites More sharing options...
Andz Posted November 27, 2011 Author Share Posted November 27, 2011 (edited) 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: 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 Edited November 27, 2011 by Andz Quote Link to comment Share on other sites More sharing options...
edward Posted November 27, 2011 Share Posted November 27, 2011 just get the closest integer and you're set... That is NOT what floor() is supposed to do. I think you're looking for round() instead? Quote Link to comment Share on other sites More sharing options...
Andz Posted November 27, 2011 Author Share Posted November 27, 2011 (edited) 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 I'm sorry, I should have said the highest integer below or equal x. Edited November 27, 2011 by Andz Quote Link to comment Share on other sites More sharing options...
gaurav Posted November 27, 2011 Share Posted November 27, 2011 (edited) 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 November 27, 2011 by vectorblur Quote Link to comment Share on other sites More sharing options...
Andz Posted November 27, 2011 Author Share Posted November 27, 2011 (edited) 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 November 28, 2011 by Andz Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.