Unix/Linux Go Back    


Programming Post questions about C, C++, Java, SQL, and other programming languages here.

Problem with rounding using lrint

Programming


Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 11-04-2016
migurus migurus is offline
Registered User
 
Join Date: Sep 2008
Last Activity: 19 January 2017, 7:23 PM EST
Location: US
Posts: 280
Thanks: 54
Thanked 31 Times in 29 Posts
Problem with rounding using lrint

I run into a situation when integer result of 81 * 0.5 is 40 (expected 41) when using lrint() function.

When I use a simple +0.5 approach results are right.

The code is:

Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
 int main(int argc, char *argv[])
{
        const int       X       = 81;
        double          prc     = (argc == 2) ? atof(argv[1]) : 0.5;
        double          dval    = X * prc;
        long            ival    = lrint(dval);
        printf("%i * %.16f = %.16f  IVAL = %li  ", X, prc, dval, ival);
        ival = (X * prc) + 0.5;
        printf("USING +0.5 IVAL = %li\n", ival);
        return(0);
}

results are:

Code:
~$ c 0.5
81 * 0.5000000000000000 = 40.5000000000000000  IVAL = 40  USING +0.5 IVAL = 41

Using gcc ver 5.4.0 on Linux, running on 2 years old Xeon processor, if that matters. I checked floating point rounding defaults in compiler and it is a regular "round to the nearest integer" rule.

My question is - should I abandon rint-family functions and use +0.5 approach?
Sponsored Links
    #2  
Old Unix and Linux 11-04-2016
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is offline Forum Staff  
Administrator
 
Join Date: Jul 2012
Last Activity: 22 January 2017, 3:24 AM EST
Location: San Jose, CA, USA
Posts: 9,862
Thanks: 476
Thanked 3,385 Times in 2,891 Posts
You need to determine what rounding rules you want to use for tie-breaking cases (see Wikipedia's discussion on rounding)
and you need to realize that even in double precision floating point, the result of a floating point calculation is not always exact (even when computing a value using decimal calculations would be exact). For example, the awk program (which uses double precision floating point for its calculations):

Code:
printf '.1 .5\n.3 .5\n' | awk '{printf("%.40f\n", $1 * $2)}'

produces the output:

Code:
0.0500000000000000027755575615628913510591
0.1499999999999999944488848768742172978818

not the output you might expect:

Code:
0.0500000000000000000000000000000000000000
0.1500000000000000000000000000000000000000

Which shows two examples where decimal arithmetic produces exact results, but binary arithmetic produced one result that was a little bit high and one result that was a little bit low.
Sponsored Links
    #3  
Old Unix and Linux 11-04-2016
migurus migurus is offline
Registered User
 
Join Date: Sep 2008
Last Activity: 19 January 2017, 7:23 PM EST
Location: US
Posts: 280
Thanks: 54
Thanked 31 Times in 29 Posts
Thanks.
I'd love to hear what others use in C/C++, that is rint() family functions? home grown solutions?
    #4  
Old Unix and Linux 11-05-2016
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 22 January 2017, 7:43 AM EST
Location: NM
Posts: 10,930
Thanks: 474
Thanked 1,005 Times in 934 Posts
Normally you call fegetround() to determine which rounding mode of the four possible ones is active.

fesetround() sets the mode. Only the fpu supported modes are available on a particular box.


For complete details try:
The GNU C Library: Rounding
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Rounding off a decimal SanjayKumar28 UNIX for Dummies Questions & Answers 11 01-16-2015 11:08 AM
Rounding Script Help kingrj46 Linux 0 11-26-2009 03:33 PM
Rounding off to the next whole number damansingh Shell Programming and Scripting 2 07-17-2008 07:18 AM
Rounding problem shash UNIX for Dummies Questions & Answers 2 01-18-2007 05:33 AM
Rounding off using BC. noodlesoup Shell Programming and Scripting 3 09-11-2006 01:38 AM



All times are GMT -4. The time now is 02:13 PM.