posted 13 years ago

Hi All,

In K&B book, page 371 following definition is given for Math.round():

Going by the above definition:

round(8.2) = Nearest integer to (8.2 + 0.5 = 8.7) = 8

round(8.7) = Nearest integer to (8.7 + 0.5 = 9.2) = 9

round(-8.2) = Nearest integer to (-8.2 + 0.5 = -7.7) = -7 //?? (Ans: -8)

round(-8.7) = Nearest integer to (-8.7 + 0.5 = -8.2) = -8 //?? (Ans: -9)

But, for negative numbers, if I subtract 0.5 instead of adding, we get the right answers. So, I think it should be add 0.5 to +ve numbers and subtract 0.5 from negative numbers. Also, I don't think 'nearest integer' are the right words to use. I think it should be add 0.5 to +ve numbers or subtract 0.5 from negative numbers and 'discard decimal portion'. Does anyone have a different opinion on this?

Ramesh

In K&B book, page 371 following definition is given for Math.round():

The round() method returns the integer closest to the argument. The algorithm is to add 0.5 to the argument and truncate to the nearest integer equivalent.

Going by the above definition:

round(8.2) = Nearest integer to (8.2 + 0.5 = 8.7) = 8

round(8.7) = Nearest integer to (8.7 + 0.5 = 9.2) = 9

round(-8.2) = Nearest integer to (-8.2 + 0.5 = -7.7) = -7 //?? (Ans: -8)

round(-8.7) = Nearest integer to (-8.7 + 0.5 = -8.2) = -8 //?? (Ans: -9)

But, for negative numbers, if I subtract 0.5 instead of adding, we get the right answers. So, I think it should be add 0.5 to +ve numbers and subtract 0.5 from negative numbers. Also, I don't think 'nearest integer' are the right words to use. I think it should be add 0.5 to +ve numbers or subtract 0.5 from negative numbers and 'discard decimal portion'. Does anyone have a different opinion on this?

Ramesh

posted 13 years ago

I think that we have to add 0.5 to the given number and take its floor value, regardless of whether the number is positive or negative.

e.g. Math.round(-8.2) = Math.floor(-8.2+0.5)=Math.floor(-7.7)=-8

e.g. Math.round(-8.2) = Math.floor(-8.2+0.5)=Math.floor(-7.7)=-8

posted 13 years ago

yes, that's right. But just be careful as floor retuns a double but round returns an int or long.

posted 13 years ago

mathmetically integer closest to -7.7 in -8 not -7.so the algorithm is quite right

~naren

~naren

~naren<br /> scjp1.4<br /> scwcd1.4

narendra darlanka

Ranch Hand

Posts: 66

posted 13 years ago

according to Khalid mughal its equivalent to adding .5 and taking floor of the result

~naren

~naren

~naren<br /> scjp1.4<br /> scwcd1.4

Ramesh Mangam

Greenhorn

Posts: 13

posted 13 years ago

Then how come the following is NOT working out:

round(8.2) = Nearest integer to (8.2 + 0.5 = 8.7) = 8

Nearest integer to 8.7 is 9. But, the correct answer is 8.

mathmetically integer closest to -7.7 in -8 not -7.so the algorithm is quite right

Then how come the following is NOT working out:

round(8.2) = Nearest integer to (8.2 + 0.5 = 8.7) = 8

Nearest integer to 8.7 is 9. But, the correct answer is 8.

posted 13 years ago

I don't know about what the book says, but the correct method is to add 0.5, then take the floor - i.e. find the first integer smaller than whatever value you have.

so:

round(8.2) = floor of (8.2 + 0.5) = floor of (8.7) = 8

round(8.7) = floor of (8.7 + 0.5) = floor of (9.2) = 9

round(-8.2) = floor of (-8.2 + 0.5) = floor of (-7.7) = -8 (-7 is bigger)

round(-8.7) = floor of (-8.7 + 0.5) = floor of (-8.2) = -9 (-8 is bigger)

[corrected cut'n'paste goof]

[ July 01, 2005: Message edited by: fred rosenberger ]

so:

round(8.2) = floor of (8.2 + 0.5) = floor of (8.7) = 8

round(8.7) = floor of (8.7 + 0.5) = floor of (9.2) = 9

round(-8.2) = floor of (-8.2 + 0.5) = floor of (-7.7) = -8 (-7 is bigger)

round(-8.7) = floor of (-8.7 + 0.5) = floor of (-8.2) = -9 (-8 is bigger)

[corrected cut'n'paste goof]

[ July 01, 2005: Message edited by: fred rosenberger ]

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

posted 13 years ago

Fred, I think what you meant was more like

round(8.2) = Nearest integer to (8.2) = floor (8.2 + 0.5) = floor(8.7) = 8

We're not looking for the nearest integer to 8.7 - were looking for the

Similar changes can be made to the other 3 examples.

[ July 01, 2005: Message edited by: Jim Yingst ]

round(8.2) = Nearest integer to (8.2) = floor (8.2 + 0.5) = floor(8.7) = 8

We're not looking for the nearest integer to 8.7 - were looking for the

*floor*of 8.7.Similar changes can be made to the other 3 examples.

[ July 01, 2005: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, *Twister*

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

ahh... crud. that's what i get for a quick cut'n'paste. i'll correct it.

posted 13 years ago

The K&B book also says that when if a number's fraction is less than 0.5 then round will work like Math.floot(); When the number's fraction is equal to or greater than 0.5 then round will work like Math.ceil().

When dealing with negative numbers think of them like this:

smaller number.........................................greater number

... -9.0, -8.9, -8.8, -8.7, -8.6, -8.5, -8.4, -8.3, -8.2, -8.1, -8.0,....-7.0,...

So if round(-8.2), since it is greater than -8.5 round is like the ceil() method and goes for the greater number which in this case is -8.0

Hope this helps,

Fes

When dealing with negative numbers think of them like this:

smaller number.........................................greater number

... -9.0, -8.9, -8.8, -8.7, -8.6, -8.5, -8.4, -8.3, -8.2, -8.1, -8.0,....-7.0,...

So if round(-8.2), since it is greater than -8.5 round is like the ceil() method and goes for the greater number which in this case is -8.0

Hope this helps,

Fes

Consider Paul's rocket mass heater. |