Page 1 of 1

Newlib rounding bug with function lroundf()

PostPosted: Fri Jun 16, 2017 4:36 pm
by xenic
The Newlib lroundf() function rounds all numbers between 1.0 and -1.0 to 0. Only numbers with an absolute value less than 0.5 should be rounded to 0. clib2 rounds numbers between 1.0 and -1.0 correctly.

Here is a small program that demonstrates the error when compiled and executed:

Code: Select all
#include <proto/exec.h>
#include <proto/dos.h>
#include <math.h>
#include <stdio.h>

int main()
{
   float x = 0.9;
   float y = -0.9;
   int a = 0;
   int b = 0;

   a = lroundf(x);
   b = lroundf(y);

   printf("x = %f  &  a = %d\n", x, a);
   printf("y = %f  &  b = %d\n", y, b);

   return 0;
}

The output when the above program (named 'test') is compiled with newlib & executed:
6.Ram_Disk:> test
x = 0.900000 & a = 0
y = -0.900000 & b =0

The output when the above program (named 'test') is compiled with clib2 & executed:
6.Ram_Disk:> test
x = 0.900000 & a = 1
y = -0.900000 & b = -1

Can someone compile & test the above program, confirm the erronious result and file a bug report to get this fixed.

Re: Newlib rounding bug with function lroundf()

PostPosted: Fri Jun 16, 2017 5:06 pm
by broadblues
Curiously it only applies to numbers less than 1.0 so nothing as simple as the "rounding direction" being different. (though the spec does say these functionjs ignore the rounding direction anyway).

Re: Newlib rounding bug with function lroundf()

PostPosted: Fri Jun 16, 2017 8:57 pm
by xenic
broadblues wrote:Curiously it only applies to numbers less than 1.0 so nothing as simple as the "rounding direction" being different. (though the spec does say these functionjs ignore the rounding direction anyway).

It does ignore the rounding direction; which is why I should have said it rounds numbers between 0 & 1.0 and between 0 & -1.0 incorrectly. I've edited my original post to reflect that fact. Thanks for pointing it out. I just hope someone can file a bug report now.

Re: Newlib rounding bug with function lroundf()

PostPosted: Sat Jun 17, 2017 4:45 pm
by broadblues
Should be fixed in newlib 53.43

Re: Newlib rounding bug with function lroundf()

PostPosted: Sat Jun 17, 2017 5:25 pm
by xenic
broadblues wrote:Should be fixed in newlib 53.43

Thanks.