# GNU Scientific Library - Bugs: bug #47348, Use of incorrect ideom floor(x+0.5)

You are not allowed to post comments on this tracker with your current authentication level.

*bug #47348*: Use of incorrect ideom floor(x+0.5)

Submitter: | M Welinder <terra> | ||

Submitted: | Sat 05 Mar 2016 10:58:10 PM UTC | ||

Category: | None | Severity: | 3 - Normal |

Operating System: | Linux | Status: | None |

Assigned to: | None | Open/Closed: | Open |

Release: | 2.1 |

No files currently attached

Depends on the following items: None found

Items that depend on this one: None found

Carbon-Copy List

No changes have been made to this item

A quick grep through the source code shows that the ideom

floor(x+0.5) is used quite a few places in the code with the

intention of rounding to nearest integer.

That ideom does not work for all x.

Specifically, there are two classes of numbers for which it

does not work:

1. The largest positive number strictly less than 0.5. This

will round to 1, but it should round to 0. This happens

because the addition rounds to 1 which floor then doesn't

touch.

2. A long sequence of even integers starting at 2^52.

For these numbers (and their negative cousins) adding 0.5

will actually add 1.

So, for example:

./specfunc/fermi_dirac.c: const int j_integer = ( fabs(j - floor(j+0.5)) < 100.0*GSL_DBL_EPSILON );

./specfunc/hyperg_1F1.c: const int bma_integer = ( bma - floor(bma+0.5) < _1F1_INT_THRESHOLD );

2^52 isn't recognized as an integer here. I have no idea

what consequences that has.

./specfunc/ellint.c: double nc = floor(phi/M_PI + 0.5);

./specfunc/ellint.c: double nc = floor(phi/M_PI + 0.5);

./specfunc/ellint.c: double nc = floor(phi/M_PI + 0.5);

./specfunc/ellint.c: double nc = floor(phi/M_PI + 0.5);

./specfunc/hyperg_2F1.c: const int intd = floor(d+0.5);

./specfunc/hyperg_2F1.c: const double rinta = floor(a + 0.5);

./specfunc/hyperg_2F1.c: const double rintb = floor(b + 0.5);

./specfunc/hyperg_2F1.c: const double rintc = floor(c + 0.5);

./specfunc/hyperg_2F1.c: const double rintc = floor(c + 0.5);

./specfunc/hyperg_2F1.c: const double rinta = floor(a + 0.5);

./specfunc/hyperg_2F1.c: const double rintb = floor(b + 0.5);

./specfunc/hyperg_2F1.c: const double rintc = floor(c + 0.5);

./specfunc/hyperg_2F1.c: const double rintc = floor(c + 0.5);

./specfunc/hyperg_2F1.c: const double rinta = floor(aR + 0.5);

Let's hope getting the right rounded values isn't important.