From aab4c3fa293f351b1a8bfe5e61a22f41153377c8 Mon Sep 17 00:00:00 2001 From: Antonio Linares Date: Thu, 16 Oct 2003 11:16:15 +0000 Subject: [PATCH] hb_numRound() completely redesigned --- harbour/source/rtl/round.c | 47 ++++++++++++-------------------------- 1 file changed, 15 insertions(+), 32 deletions(-) diff --git a/harbour/source/rtl/round.c b/harbour/source/rtl/round.c index 5984737348..6aeeeba19a 100644 --- a/harbour/source/rtl/round.c +++ b/harbour/source/rtl/round.c @@ -57,6 +57,9 @@ * Copyright 1999 Matthew Hamilton * INT() * + * Copyright 2003 Vicente Aranzana + * hb_numRound() + * * See doc/license.txt for licensing terms. * */ @@ -84,44 +87,24 @@ HB_FUNC( INT ) hb_errRT_BASE_SubstR( EG_ARG, 1090, NULL, "INT", 1, hb_paramError( 1 ) ); } -double hb_numRound( double dResult, int iDec ) +double hb_numRound( double doValue, int nPrecision ) { - HB_TRACE(HB_TR_DEBUG, ("hb_numRound(%lf, %d)", dResult, iDec)); + static const double doBase = 10.0f; + double doComplete5, doComplete5i; - if( dResult != 0.0 ) - { - if( iDec == 0 ) - { - if( dResult < 0.0 ) - dResult = ceil( dResult - 0.5 ); - else - dResult = floor( dResult + 0.5 ); - } - else if( iDec < 0 ) - { - double dAdjust = pow( 10, -iDec ); + HB_TRACE(HB_TR_DEBUG, ("hb_numRound(%lf, %d)", doValue, iPrecision)); - if( dResult < 0.0 ) - dResult = ceil( ( dResult / dAdjust ) - 0.5 ); - else - dResult = floor( ( dResult / dAdjust ) + 0.5 ); + doComplete5 = doValue * pow(doBase, (double) (nPrecision + 1)); - dResult *= dAdjust; - } - else - { - double dAdjust = pow( 10, iDec ); + if(doValue < 0.0f) + doComplete5 -= 5.0f; + else + doComplete5 += 5.0f; - if( dResult > 0.0 ) - dResult = ceil( ( dResult * dAdjust ) + 0.5 ); - else - dResult = floor( ( dResult * dAdjust ) - 0.5 ); + doComplete5 /= doBase; + modf(doComplete5, &doComplete5i); - dResult /= dAdjust; - } - } - - return dResult; + return doComplete5i / pow(doBase, (double) nPrecision); } HB_FUNC( ROUND )