## Error with Math.round()

**Moderators:** imaqine, 99jonathan, roger

### Error with Math.round()

I am using the newest version of lejos, 0.9.1. Using the Math.round() method on a value between 0 and .5 exclusive yields -1. I made my own method using Math.ceil() and Math.floor() and discovered that Math.floor() yields this same result. This is interesting, since simply casting to int works perfectly in place of floor. Unless something is going wrong with just the Math.floor() method in my setup, there is surely an error in lejos, so be wary.

### Re: Error with Math.round()

Fixed in SVN:

http://lejos.svn.sourceforge.net/viewvc ... 28&r2=6478

I tested many input values (see below) and floor and ceil now comply with what the JDK's floor/ceil return, including negative zero.

Some day, I will write unit tests for our java.lang.Math. Please be patient.

http://lejos.svn.sourceforge.net/viewvc ... 28&r2=6478

I tested many input values (see below) and floor and ceil now comply with what the JDK's floor/ceil return, including negative zero.

Some day, I will write unit tests for our java.lang.Math. Please be patient.

Code: Select all

` static double[] testvals = {`

2.0, 1.7, 1.3, 1.0, 0.7, 0.3, 0.0,

-0.0, -0.3, -0.7, -1.0, -1.3, -1.7, -2.0,

};

### Re: Error with Math.round()

Works like a champ. I suppose casting is only like floor for the positive side of the plane and ceil for the other half. In any case, I've revised my code, too. All is well with my robot now.

### Re: Error with Math.round()

foliot wrote:Works like a champ. I suppose casting is only like floor for the positive side of the plane and ceil for the other half. In any case, I've revised my code, too. All is well with my robot now.

Casting always rounds toward zero (decimal places are stripped). Floor rounds toward negative infinity and ceil towards positive infinity.

Thanks for confirming that the revised methods work.

### Who is online

Users browsing this forum: No registered users and 5 guests