GARANTIR DESCONTO

Fórum Alguem explica isso? Seria um BUG no delphi? #274665

30/03/2005

0

Olá pessoal:

Tenho um exemplo:

Coloque um botão no form e coloque esse código.
Veja o resultado. Alguém pode explicar o que acontece???
Pode-se trocar os valores....


procedure TForm1.Button1Click(Sender: TObject);
var
fvalor, fvalor1: real;
begin

fvalor1 := 20.15;

fvalor := 0;
fvalor := fvalor + 0.60;
fvalor := fvalor + 19;
fvalor := fvalor + 0.01;
fvalor := fvalor + 0.54;

if fvalor = fvalor1 then
showmessage(´é igual´);

if fvalor > fvalor1 then
showmessage(´fvalor é maior´);

if fvalor < fvalor1 then
showmessage(´fvalor é menor´);

end;


Abraços
Alexandre


_alex_

_alex_

Responder

Posts

30/03/2005

Nildo

Abre o Evaluation e coloca a expressão depois de ter atribuido os valores:

fvalor1 - fvalor

Você vai ver que tem diferença.
Pode ser que sejam as casas decimais que não são apresentadas visualmente


Responder

Gostei + 0

30/03/2005

Faelcavalcanti

:shock: :shock: :shock:
Cara que loukura é essa. Estive tentando e não consegui, fiz um gambiarra e coloquei na força e deu certo! :lol: :lol: :lol:

const valordefault : Real = 20.15;
var
fvalor, fvalor1: Extended;

begin

fvalor1 := 20.15;

fvalor := 0;
fvalor := fvalor + 0.60;
fvalor := fvalor + 19;
fvalor := fvalor + 0.01;
fvalor := fvalor + 0.54;


if (StrToFloat(FloatToStr(fvalor)) = StrToFloat(FloatToStr(valordefault))) then
showmessage(´é igual´);

if fvalor > fvalor1 then
showmessage(´fvalor é maior´);

if fvalor < fvalor1 then
showmessage(´fvalor é menor´);


Mas é lógico que não funciona, é como se fossem dois objetos distintos, de forma que não adianta verificar a condições dos dois e sim o valor literário, como referência na memória entre si.


Responder

Gostei + 0

30/03/2005

Andremuller

Muito interessante.

Fiz alguns testes:

Assim funcionou:
aumentando o valor para 20.16 e somando 0.02 ao invés de 0.01
ou colocando a expressão toda em uma linha
ou passando as variáveis de real para currency


Responder

Gostei + 0

30/03/2005

Macario

Ola Programadores.

A diferença ocorre usando tipo: Real, Double ou Extended.

Usando Currency, não ocorre a diferença.

Como normalmente uso tipos Double ou Extended, comparo da seguinte forma

var
fvalor, fvalor1: Extended;
begin

  fvalor1 := 20.15;

  fvalor := 0;
  fvalor := fvalor + 0.60;
  fvalor := fvalor + 19;
  fvalor := fvalor + 0.01;
  fvalor := fvalor + 0.54;
  fvalor := Arredonda(fvalor,4);
  fvalor1 := Arredonda(fvalor1,4);

  if fvalor = fvalor1 then
  showmessage(´é igual´);

  if fvalor > fvalor1 then
  showmessage(´fvalor é maior´);

  if fvalor < fvalor1 then
  showmessage(´fvalor é menor´);


function Arredonda(Value: Extended; Decimals: integer): Extended;
var
  Factor, Fraction: Extended;
begin
  Factor := IntPower(10, Decimals);
  (* A conversão para string e depois para float evita
    erros de arredondamentos indesejáveis.*)
  Value := StrToFloat(FloatToStr(Value * Factor));
  Result := Int(Value);
  Fraction := Frac(Value);
  if Fraction >= 0.5 then

    Result := Result + 1
  else if Fraction <= -0.5 then
    Result := Result - 1;
  Result := Result / Factor;
end;


Agora se alguem puder dizer as diferenças de como se comporta cada tipo de variavel, seria legal.


Responder

Gostei + 0

30/03/2005

Fred

cara, doidera mesmo!!! com o tipo double acontece a mesma coisa, mas com Currency funciona!!!


Responder

Gostei + 0

30/03/2005

Faelcavalcanti

[quote:6781411545=´Macario o Aspone´]Ola Programadores.

A diferença ocorre usando tipo: Real, Double ou Extended.

Usando Currency, não ocorre a diferença.

[/quote:6781411545]

Cara no help têm :
The following remarks apply to fundamental real types. * Real48 is maintained for backward compatibility. Since its storage format is not native to the Intel CPU family, it results in slower performance than other floating-point types. * Extended offers greater precision than other real types but is less portable. Be careful using Extended if you are creating data files to share across platforms. * The Comp (computational) type is native to the Intel CPU and represents a 64-bit integer. It is classified as a real, however, because it does not behave like an ordinal type. (For example, you cannot increment or decrement a Comp value.) Comp is maintained for backward compatibility only. Use the Int64 type for better performance. * Currency is a fixed-point data type that minimizes rounding errors in monetary calculations. It is stored as a scaled 64-bit integer with the four least-significant digits implicitly representing decimal places. When mixed with other real types in assignments and expressions, Currency values are automatically divided or multiplied by 10000.


Ou seja ele está atribuído a um tipo literal de 8 bytes possuindo de 19-20 dígitos. Essa só o Mr. M sabe !!! :lol:


Responder

Gostei + 0

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar