Bug 479025 - rint() returns incorrect values on x86_64
rint() returns incorrect values on x86_64
Status: CLOSED WORKSFORME
Product: Red Hat Enterprise Linux 5
Classification: Red Hat
Component: glibc (Show other bugs)
5.2
All Linux
low Severity medium
: rc
: ---
Assigned To: Carlos O'Donell
BaseOS QE
:
Depends On:
Blocks:
  Show dependency treegraph
 
Reported: 2009-01-06 11:00 EST by Tru Huynh
Modified: 2016-11-24 07:36 EST (History)
3 users (show)

See Also:
Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of:
Environment:
Last Closed: 2009-03-16 17:55:36 EDT
Type: ---
Regression: ---
Mount Type: ---
Documentation: ---
CRM:
Verified Versions:
Category: ---
oVirt Team: ---
RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: ---


Attachments (Terms of Use)
test case (575 bytes, text/x-c)
2009-01-06 11:00 EST, Tru Huynh
no flags Details


External Trackers
Tracker ID Priority Status Summary Last Updated
CentOS 3326 None None None Never

  None (edit)
Description Tru Huynh 2009-01-06 11:00:09 EST
Created attachment 328290 [details]
test case

Description of problem:
rint() returns incorrect values on x86_64

Version-Release number of selected component (if applicable):
RHEL 3 4 and 5 with gcc

How reproducible:
always

Steps to Reproduce:
1. compile the attached program
2. run it
3.
  
Actual results:
wrong results for the 64 bits version, ok with -m32

Expected results:
correct results for 32 and 64 bits versions

Additional info:
Comment 1 Tru Huynh 2009-01-06 11:03:24 EST
could this be cloned to version 3 and 4 too? or should I fill it again.
Comment 2 Ulrich Drepper 2009-03-16 17:55:36 EDT
I don't see any problems with RHEL5.4.

289440.590000000026 289441.000000000000
289440.750000000000 289441.000000000000
289441.590000000026 289442.000000000000
289441.750000000000 289442.000000000000
289442.590000000026 289443.000000000000
289442.750000000000 289443.000000000000
Comment 3 Spenser Gilliland 2014-06-27 14:09:22 EDT
I have a similar bug on x86_64 

$ gcc -m32 -std=gnu99 tests.c -o tests -lm
$ ./tests 
FE_TONEAREST
rint(-524096.750000000000) = -524097.000000000000
$ gcc -std=gnu99 tests.c -o tests -lm
$ ./tests 
FE_TONEAREST
rint(-524096.750000000000) = -524096.000000000000

This occurs on the following hosts

$ lsb_release -a
LSB Version:    :core-4.0-amd64:core-4.0-ia32:core-4.0-noarch:graphics-4.0-amd64:graphics-4.0-ia32:graphics-4.0-noarch:printing-4.0-amd64:printing-4.0-ia32:printing-4.0-noarch
Distributor ID: RedHatEnterpriseServer
Description:    Red Hat Enterprise Linux Server release 5.6 (Tikanga)
Release:        5.6
Codename:       Tikanga

$ lsb_release -a
LSB Version:    :core-4.0-amd64:core-4.0-ia32:core-4.0-noarch:graphics-4.0-amd64:graphics-4.0-ia32:graphics-4.0-noarch:printing-4.0-amd64:printing-4.0-ia32:printing-4.0-noarch
Distributor ID: RedHatEnterpriseClient
Description:    Red Hat Enterprise Linux Client release 5.9 (Tikanga)
Release:        5.9
Codename:       Tikanga

$ cat tests.c
#include <math.h>
#include <fenv.h>
#include <stdio.h>

int main() {
        switch(fegetround()) {
        case FE_DOWNWARD:
                printf("FE_DOWNWARD\n");
                break;
        case FE_TONEAREST:
                printf("FE_TONEAREST\n");
                break;
        case FE_TOWARDZERO:
                printf("FE_TOWARDZERO\n");
                break;
        case FE_UPWARD:
                printf("FE_UPWARD\n");
                break;
        default:
                printf("unknown rounding mode\n");
        }
        union {
                unsigned int i;
                float f;
        } val = {0xc8ffe818};
        printf("rint(%.12f) = %.12f\n", val.f, rint(val.f));

}
Comment 4 Spenser Gilliland 2014-06-27 14:11:37 EDT
Additionaly, I can confirm this bug is not present in RHEL 6.1 and above.
Comment 5 Spenser Gilliland 2014-06-27 15:25:43 EDT
NV(In reply to Spenser Gilliland from comment #4)
> Additionaly, I can confirm this bug is not present in RHEL 6.1 and above.

I retract this previous statement.  There is a bug in the RHEL 6.1 rintf function. (I was attempting to use rintf as a workaround and found this issue)

$ gcc tests.c -lm -o tests
$ ./tests 
FE_TONEAREST
rintf(2899846.750000000000) = 2899846.000000000000
$ gcc -m32 tests.c -lm -o tests
$ ./tests 
FE_TONEAREST
rintf(2899846.750000000000) = 2899847.000000000000

$ lsb_release -a
LSB Version:    :core-4.0-amd64:core-4.0-ia32:core-4.0-noarch:graphics-4.0-amd64:graphics-4.0-ia32:graphics-4.0-noarch:printing-4.0-amd64:printing-4.0-ia32:printing-4.0-noarch
Distributor ID: RedHatEnterpriseWorkstation
Description:    Red Hat Enterprise Linux Workstation release 6.1 (Santiago)
Release:        6.1
Codename:       Santiago

$ cat tests.c
#include <math.h>
#include <fenv.h>
#include <stdio.h>

int main() {
        switch(fegetround()) {
        case FE_DOWNWARD:
                printf("FE_DOWNWARD\n");
                break;
        case FE_TONEAREST:
                printf("FE_TONEAREST\n");
                break;
        case FE_TOWARDZERO:
                printf("FE_TOWARDZERO\n");
                break;
        case FE_UPWARD:
                printf("FE_UPWARD\n");
                break;
        default:
                printf("unknown rounding mode\n");
        }
        union {
                unsigned int i;
                float f;
        } val = {0x4a30fe1b};
        printf("rintf(%.12f) = %.12f\n", val.f, rintf(val.f));

}
Comment 6 Spenser Gilliland 2014-06-27 17:28:46 EDT
My current work around for RHEL5/6 compatibility is to use rintl. According to exhaustive test runs rintl provides correct results for all single precision floating point numbers.
Comment 7 Spenser Gilliland 2014-07-11 19:13:53 EDT
Ahh yet another issue, when using rintl you cannot enable -O1-3 as the compiler will try to optimize it to rint (which fails.)
Comment 8 Spenser Gilliland 2014-07-22 14:01:02 EDT
Alright, finally figured out a workaround to enable optimization. The following wrapper will run properly for -O3.

double rint_wrap(double x) {
	long double xx = x;
	long double val = rintl(xx);

	return val;
}

Note You need to log in before you can comment on or make changes to this bug.