Error with Math.round()

This is where you talk about the NXJ software itself, installation issues, and programming talk.

Moderators: 99jonathan, roger, imaqine

Error with Math.round()

Postby foliot » Fri Mar 16, 2012 10:30 pm

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.
foliot
New User
 
Posts: 17
Joined: Fri Mar 16, 2012 10:21 pm

Re: Error with Math.round()

Postby skoehler » Sat Mar 17, 2012 2:29 am

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.

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,
   };
skoehler
leJOS Team Member
 
Posts: 1350
Joined: Thu Oct 30, 2008 4:54 pm

Re: Error with Math.round()

Postby foliot » Sat Mar 17, 2012 4:54 am

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.
foliot
New User
 
Posts: 17
Joined: Fri Mar 16, 2012 10:21 pm

Re: Error with Math.round()

Postby skoehler » Sat Mar 17, 2012 10:36 am

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.
skoehler
leJOS Team Member
 
Posts: 1350
Joined: Thu Oct 30, 2008 4:54 pm


Return to NXJ Software

Who is online

Users browsing this forum: Baidu [Spider] and 1 guest

more stuff