Fractional Part of the number question?

Mod the number with one to get the decimal part, if its >0.5, round up, else round down.

Up vote 2 down vote favorite 1 share g+ share fb share tw.

Edit: Looking for a mathematical number trick to figure out the part needed to round the number to the nearest integer. The more primitive the math operations the better. Please avoid using other's procedures.

0.5 can be taken eitherway, whatever suits your method. This is NOT my homework question, nor am I going to use this anywhere. Math rounding floor ceiling link|improve this question edited May 17 '09 at 19:39 asked May 17 '09 at 19:02kunj2aan2,559848139 74% accept rate.

– DeadHead May 17 '09 at 19:05 Sorry no castings. – kunj2aan May 17 '09 at 19:07 does that also exclude the nearest integer function nint()? – TStamper May 17 '09 at 19:08 1 How would you deal with 0.5, which is equidistant from 0 and 1.

Arithmetic rounding always rounds upward, but Banker's rounding takes the nearest even number. – Eugene Yokota May 17 '09 at 19:27.

Mod the number with one to get the decimal part, if its >0.5, round up, else round down OR Divide the number by 0.5, if its odd, round up, else round down.

1 Note that some languages only define mod/% on integers. – Stephan202 May 17 '09 at 19:13 The algorithm is clever if x %1 >= 0.5 print ((x- x%1) + 1) -x else print x%1 end Can you please think of anothersolution without the %? – kunj2aan May 17 '09 at 19:17 Any other constraints?

Will dividing do the trick? – Simonw May 17 '09 at 19:29 I don't think dividing by 0.5(multiplying by 2) will work. – kunj2aan May 17 '09 at 19:30 Why?

Without any other info its impossible to answer this question – Simonw May 17 '09 at 19:34.

Once you have the fractional part of the number, the problem is pretty much solved. One way to get the fractional part is to repeatedly subtract powers-of-2 from you number (assuming it has been made positive, if it were negative to begin with). The function below, getWholeMaker, returns what you want (the "thing" that must be added to round the number).

It's running time is O(log(n)), and uses only elementary operations. /* Returns the factional part of x */ double getFrac(double x) { if(x = 0 */ double t = 2; while(t+t = 1) { if(t = 0? +1 : -1; return sign * (frac -frac : 1-frac); }.

Sweet. But this looks like pkaeding's algorithm optimized. Am I correct?

– kunj2aan May 17 '09 at 20:53 Yes, It's like pkaeding's, but works in logarithmic time (pkaeding's is linear time). – Ashutosh Mehra May 17 '09 at 21:07.

If you can't use mod (because it might only be defined for integers in your language, you might be able to do something like this (in C-ish pseudocode): // make the input positive: boolean inputPositive = true; if (input 1) { input = input - 1; integerPart++; } int ret; if (input >= 0.5) { // round up ret = integerPart + 1; } else { ret = integerPart; } if (inputPositive) { return ret; } else { return 0 - ret; } This solution doesn't use mod or any outside functions. Of course, I can't imagine why you would want this in real life. It is interesting to think about, though.

1 Very good answer according to me it follows all the requirements! :-) – yves Baumes May 17 '09 at 19:40 +1 of course, I forgot to mention .. ;-) – yves Baumes May 17 '09 at 19:43 1 +1. You could improve on this by subtracting ever smaller powers of two, so that this procedure will also terminate within reasonable time for large inputs.

– Stephan202 May 17 '09 at 19:45 1 This is very interesting. I like the part where you check if the number is positive. I hadnt thought about that.

I had hoped somebody would come with math tricks and even though subtracting one is quite tedious, I think this solution works. Thanks. – kunj2aan May 17 '09 at 19:48.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions