Página 1 de 1

Erro Round() no xHarbour

Enviado: 19 Jun 2014 08:33
por joaoalpande
Bom dia ,

Não sei se devo postar neste fórum ou noutro , mas fiz uns testes com a função Round() e deu problema no xHarbour, eu trabalho com xHarbour e nunca pensei que estivesse mal , exemplo:

Teste no xHarbour :
nImp := 0.00
qt := 2.0500
preco := 14.7000

nImp := ( ROUND( qt, 2 ) ) * ( ROUND( preco, 2 ) )
? ROUND( nImp, 2 )
// Deveria retornar 30.14 e nao esta... Retorna 30.13


Teste no Harbour :
nImp := 0.00
qt := 2.0500
preco := 14.7000
nImp := ( ROUND( qt, 2 ) ) * ( ROUND( preco, 2 ) )
? ROUND( nImp, 2 )
// retornar 30.14 que está certo


Se algúem puder explicar e ajudar, preciso corrigir essa situação alterando a função Round()

Cumprimentos

João Alpande

Erro Round() no xHarbour

Enviado: 19 Jun 2014 10:24
por Jairo Maia
Olá João Alpande,
joaoalpande escreveu:preciso corrigir essa situação alterando a função Round()
Pode ser, mas atenção: Isso ocorre com o xHarbour porque você está usando xHarbour com BCC, e com certeza seu Harbour é MinGW. Se você compilar seu exemplo com Harbour + BCC, também retornará: 30.13.

O motivo não sei, mas com BCC parece que o critério do Round() que é arredondar para baixo de 0 a 4 e para cima 5 a 9 não funciona.

Erro Round() no xHarbour

Enviado: 19 Jun 2014 10:26
por joaoalpande
Obrigado pela rápida resposta , e como vc faz no seu sistema não usa o Round() ou usa outra função?


Um abraço
João Alpande

Erro Round() no xHarbour

Enviado: 19 Jun 2014 10:50
por Jairo Maia
Olá João,

Continuo usando o Round(), mas percebi isso depois de muita dor de cabeça principalmente em cálculos de parcelas.

No início, eu usava o Harbour com BCC, mas percebi que quando compilava com MinGW o Resultado era correto, não dava diferença de centavo, então abandonei em definitivo o BCC e passei a usar Harbour + MinGW.

No meu caso cheguei a pensar em abandonar o Round() e usar apenas SET DECIMALS TO 2 na hora do cálculo, mas deu dor cabeça também.

Erro Round() no xHarbour

Enviado: 19 Jun 2014 10:54
por joaoalpande
Estou a pensar mudar de FWh+ xharbour para Fwh+ Harbour , será muito complicado? terei de usar o BCC??

Erro Round() no xHarbour

Enviado: 19 Jun 2014 11:26
por Poka
Fiz um teste com round, retorna 30.14
Utilizando str( 12,2 ) (sem round) retorna 30.14 também, não era para retornar 30.13?

Poka

Erro Round() no xHarbour

Enviado: 19 Jun 2014 11:54
por joaoalpande
Tem de retornar 30.14 , se converter para string funciona , estou falando com o Round() sem converter.

Um abraço
João Alpande

Erro Round() no xHarbour

Enviado: 19 Jun 2014 11:59
por Jairo Maia
Olá João,
joaoalpande escreveu:Estou a pensar mudar de FWh+ xharbour para Fwh+ Harbour , será muito complicado? terei de usar o BCC??
Não sei dizer. Nunca usei FiveWin, então não sei dizer.

Porém, me ocorreu o seguinte: Quando deixei de usar o BCC, eu usava a versão 5.5. Talvez se você atualizar a versão do BCC pode ser que resolva. Tente isso primeiro.

Olá Poka,
Poka escreveu:Utilizando str( 12,2 ) (sem round) retorna 30.14 também, não era para retornar 30.13?
Não. Está correto. a função Str() ao diminuir as casas decimais, bem como se você usar SET DECIMALS TO <nDecimais>, será arredondado com o mesmo critério do Round().

Erro Round() no xHarbour

Enviado: 19 Jun 2014 12:32
por joaoalpande
EU uso a versão bcc585 é igual, dá o mesmo problema.

Erro Round() no xHarbour

Enviado: 19 Jun 2014 23:07
por Paredes01
Hola a todos.

En mi caso no uso round(), lo hago de esta forma y funciona muy bien.
Tu decides cuantos decimales necesitas expresar.

nIMP = VAL( STR( (nPRECIO * nCANT) ,15,2) )
nIMP = VAL( STR( (nPRECIO * nCANT) ,15,3) )
nIMP = VAL( STR( (nPRECIO * nCANT) ,15,4) )

Saludos !!

Erro Round() no xHarbour

Enviado: 20 Jun 2014 07:02
por joaoalpande
Obrigado , mas teria de alterar todos prgs do software por favor demonstrem como fazem , se usam ou tem alguma função para substituir que funcione bem?


Um abraço

João Alpande

Erro Round() no xHarbour

Enviado: 20 Jun 2014 09:11
por Toledo
João, você pode fazer o seguinte:

Código: Selecionar todos

#xtranslate round(<nVal>,<nDec>) => val(str(<nVal>,20,<nDec>))

Func Main()
 nImp := 0.00
 qt := 2.0500
 preco := 14.7000
 nImp := ( ROUND( qt, 2 ) ) * ( ROUND( preco, 2 ) )
 ? ROUND( nImp, 2 )
Retu Nil
Se desejar, pode criar um arquivo CH (por exemplo: t_round.ch) com apenas:

Código: Selecionar todos

#xtranslate round(<nVal>,<nDec>) => val(str(<nVal>,20,<nDec>))
E depois no início de cada PRG do seu programa incluir a linha:

Código: Selecionar todos

#include "t_round.ch"
Abraços,

Erro Round() no xHarbour

Enviado: 20 Jun 2014 09:23
por joaoalpande
obrigado pel ajuda, irei testar.

Um abraço

João Alpande

Erro Round() no xHarbour

Enviado: 23 Jun 2014 11:52
por Itamar M. Lins Jr.
Isso é gambiarra.
Tem que corrigir o erro no xHB. (se for erro!)
Se ele mudar no futuro p/ o Harbour vai ficar com esses códigos dentro do sistema sem necessidade nenhuma.
Se não for um erro do xHB, for do BCC é só mudar para Mingw.
O código atual dela é essa logo abaixo, verifica se tem diferença com o do Xhb.

Código: Selecionar todos

/*
 * Round(), Int() functions
 *
 * Copyright 1999 David G. Holm <dholm@jsd-llc.com>
 * Copyright 1999 Matthew Hamilton <mhamilton@bunge.com.au> (Int())
 * Copyright 2003 Vicente Aranzana <varanzana@gruposp.com> (hb_numRound())
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this software; see the file COPYING.txt.  If not, write to
 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307 USA (or visit the web site https://www.gnu.org/).
 *
 * As a special exception, the Harbour Project gives permission for
 * additional uses of the text contained in its release of Harbour.
 *
 * The exception is that, if you link the Harbour libraries with other
 * files to produce an executable, this does not by itself cause the
 * resulting executable to be covered by the GNU General Public License.
 * Your use of that executable is in no way restricted on account of
 * linking the Harbour library code into it.
 *
 * This exception does not however invalidate any other reasons why
 * the executable file might be covered by the GNU General Public License.
 *
 * This exception applies only to the code released by the Harbour
 * Project under the name Harbour.  If you copy code from other
 * Harbour Project or Free Software Foundation releases into a copy of
 * Harbour, as the General Public License permits, the exception does
 * not apply to the code that you add in this way.  To avoid misleading
 * anyone as to the status of such modified files, you must delete
 * this exception notice from them.
 *
 * If you write modifications of your own for Harbour, it is your choice
 * whether to permit this exception to apply to your modifications.
 * If you do not wish that, delete this exception notice.
 *
 */

#include "hbapi.h"
#include "hbapiitm.h"
#include "hbapierr.h"

HB_FUNC( INT )
{
   PHB_ITEM pNumber = hb_param( 1, HB_IT_NUMERIC );

   if( pNumber )
   {
      if( HB_IS_NUMINT( pNumber ) )
         hb_itemReturn( pNumber );
      else
      {
         int iWidth;

         hb_itemGetNLen( pNumber, &iWidth, NULL );
         hb_retnlen( hb_numInt( hb_itemGetND( pNumber ) ), iWidth, 0 );
      }
   }
   else
      hb_errRT_BASE_SubstR( EG_ARG, 1090, NULL, HB_ERR_FUNCNAME, HB_ERR_ARGS_BASEPARAMS );
}

HB_FUNC( ROUND )
{
   PHB_ITEM pNumber = hb_param( 1, HB_IT_NUMERIC );

   if( pNumber && HB_ISNUM( 2 ) )
   {
      int iDec = hb_parni( 2 );

#ifdef HB_CLP_STRICT
      /* In CA-Cl*pper Round() always returns double item, what in some
       * applications may be important due to different formatting rules
       * when SET FIXED is ON [druzus]
       */
      hb_retndlen( hb_numRound( hb_itemGetND( pNumber ), iDec ), 0, HB_MAX( iDec, 0 ) );
#else
      if( iDec == 0 && HB_IS_NUMINT( pNumber ) )
         hb_retnint( hb_itemGetNInt( pNumber ) );
      else
         hb_retnlen( hb_numRound( hb_itemGetND( pNumber ), iDec ), 0, HB_MAX( iDec, 0 ) );
#endif
   }
   else
      hb_errRT_BASE_SubstR( EG_ARG, 1094, NULL, HB_ERR_FUNCNAME, HB_ERR_ARGS_BASEPARAMS );
}
Saudações,
Itamar M. Lins Jr.