Changeset 11474


Ignore:
Timestamp:
Dec 5, 2010, 6:50:18 PM (11 years ago)
Author:
charles
Message:

(2.0x libtransmission) backport tr_truncd() fix for #3521 "rounding issue in tr_truncd()"

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2.0x/libtransmission/utils.c

    r11050 r11474  
    2525#include <ctype.h> /* isalpha, tolower */
    2626#include <errno.h>
     27#include <float.h> /* DBL_EPSILON */
     28#include <locale.h> /* localeconv() */
    2729#include <math.h> /* pow */
    2830#include <stdarg.h>
     
    14021404
    14031405double
    1404 tr_truncd( double x, int decimal_places )
    1405 {
    1406     const int i = (int) pow( 10, decimal_places ); 
    1407     const double x2 = (int64_t)(x * i);
    1408     return x2 / i;
     1406tr_truncd( double x, int precision )
     1407{
     1408    char * pt;
     1409    char buf[128];
     1410    const int max_precision = (int) log10( 1.0 / DBL_EPSILON ) - 1;
     1411    tr_snprintf( buf, sizeof( buf ), "%.*f", max_precision, x );
     1412    if(( pt = strstr( buf, localeconv()->decimal_point )))
     1413        pt[precision ? precision+1 : 0] = '\0';
     1414    return atof(buf);
    14091415}
    14101416
Note: See TracChangeset for help on using the changeset viewer.