# Round for negative nr

posted 12 years ago

- 0

Round algorithm is to ad 0.5 and truncate to the nearest integer(equivalent).

This algorithm works strange with negative nr.

By ex:

x=-2.4 => -2.4+0.5 = -1.9 => trunc(-1.9)=-1.But this wrong Math.round(-2.4)=-2;

but

x=-2.5=>-2.5 + 0.5=-2 => trunc(-2)=-2 this is ok!

That means(for negative values) if the number after decimal point is greater or equal with 5 the algorthm is

value+.5 and truncate else

-(value +.5 ) and truncate

I try this with code & it seams to work-> that means at least from the practically part I am right.Can be tat the round has (for negative values) two algorithms?

This algorithm works strange with negative nr.

By ex:

x=-2.4 => -2.4+0.5 = -1.9 => trunc(-1.9)=-1.But this wrong Math.round(-2.4)=-2;

but

x=-2.5=>-2.5 + 0.5=-2 => trunc(-2)=-2 this is ok!

That means(for negative values) if the number after decimal point is greater or equal with 5 the algorthm is

value+.5 and truncate else

-(value +.5 ) and truncate

I try this with code & it seams to work-> that means at least from the practically part I am right.Can be tat the round has (for negative values) two algorithms?

SCJP, SCJD, SCWCD, OCPJBCD

posted 12 years ago

- 0

My problem is with the statement "truncate to the nearest integer".

the word "truncate" means to "cut off", i.e. drop the stuff to the right of the decimal. if i truncate 1.999999, i get 1.

"to the nearest integer" mean "look at the decimal part, and use that to decide where to go". 1.99999 to the nearest integer is 2.

Your alorithm doesn't make sense to me. I'm not saying it's wrong, just that I don't understand it.

the word "truncate" means to "cut off", i.e. drop the stuff to the right of the decimal. if i truncate 1.999999, i get 1.

"to the nearest integer" mean "look at the decimal part, and use that to decide where to go". 1.99999 to the nearest integer is 2.

Your alorithm doesn't make sense to me. I'm not saying it's wrong, just that I don't understand it.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors

posted 12 years ago
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors

- 0

Now that i've a) had some coffee, and b) done a little research, here's what i've found..

the algorithm should be "floor(x + 0.5"). "floor" is the operation that says "take the largest integer LESS THAN or equal to the value in question. there is a similar function called "ceiling", which is the smallest number greated than or equal to the number in question. there are good examples here:

Floor/Ceiling examples

so..

x=-2.4 => -2.4+0.5 = -1.9 => floor(-1.9)=-2. (-1 is not less than -1.9, -2 is!!!).

x=-2.5=>-2.5 + 0.5=-2 => floor(-2)=-2. this again works.

the algorithm should be "floor(x + 0.5"). "floor" is the operation that says "take the largest integer LESS THAN or equal to the value in question. there is a similar function called "ceiling", which is the smallest number greated than or equal to the number in question. there are good examples here:

Floor/Ceiling examples

so..

x=-2.4 => -2.4+0.5 = -1.9 => floor(-1.9)=-2. (-1 is not less than -1.9, -2 is!!!).

x=-2.5=>-2.5 + 0.5=-2 => floor(-2)=-2. this again works.

Ray Stojonic

Ranch Hand

Posts: 326

posted 12 years ago

- 0

Math.round() adds .5, then takes the Math.floor() of the result, which will return the integer that is closest to positive infinty and not greater than the number.

ie:

-2.4 should round to -2

-2.4 + .5 = -1.9

integer that is closest to +inf and not greater than -1.9 = -2

-2.5 should round to -2

-2.5 + .5 = -2.0

-2.0 is already == -2 and falls under the special case where the argument is == to an integer, the number is returned as an int: -2

-2.6 should round to -3

-2.6 + .5 = -2.1

integer that is closest to +inf and not greater than -2.1 = -3

Except for the special cases (there are others detailed in the javadoc for Math.round() and Math.floor()) there is only one procedure for any argument.

Hope that helps

ie:

-2.4 should round to -2

-2.4 + .5 = -1.9

integer that is closest to +inf and not greater than -1.9 = -2

-2.5 should round to -2

-2.5 + .5 = -2.0

-2.0 is already == -2 and falls under the special case where the argument is == to an integer, the number is returned as an int: -2

-2.6 should round to -3

-2.6 + .5 = -2.1

integer that is closest to +inf and not greater than -2.1 = -3

Except for the special cases (there are others detailed in the javadoc for Math.round() and Math.floor()) there is only one procedure for any argument.

Hope that helps

With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |