Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2007-03

CompareValue

6 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Vincent Snijders

Hi,

In the math unit are the CompareValue functions only for signed integers. Why not for DWORD and QWORD?

I tested with the program below.

For DWord is works correctly, presumely because the int64 overload is used. For QWord without typecast the code doesn't compile, because the compile cannot choose the right overload. With typecasts it returns the wrong values.

The math unit also has these lines:
{ this causes more trouble than it solves
function Min(a, b: Cardinal): Cardinal;
function Max(a, b: Cardinal): Cardinal;
}
It doesn't tell what problems and why.

Vincent

===========================================
program Project1;
{$mode objfpc}{$H+}

uses
Classes, SysUtils, Math

   { add your units here };
var
   d1, d2: dword;
   q1, q2: qword;
begin
   d1 := $F0000000;
   d2 := 10;
   writeln('CompareValue (d1,d2): ', comparevalue(d1,d2));
   writeln('CompareValue (d2,d1): ', comparevalue(d2,d1));
   q1 := $F000000000000000;
   q2 := 10;
   writeln('CompareValue (q1,q2): ', comparevalue(int64(q1),int64(q2)));
   writeln('CompareValue (q2,q1): ', comparevalue(int64(q2),int64(q1)));
   readln;
end.

Reply by email

Пётр Косаревский
to Vincent Snijders
In the math unit are the CompareValue functions only for signed integers. Why not for DWORD and QWORD?

I guess, that it's Delphi mimicing implementation (because it is Delphi-like).

And it can be tricky to distinguish signed and unsigned expression values.

Like,

a: qword;
a-1 --- ??? (anyone may know for sure that a-1>0 and want to subtract one)

I guess it's something like that.

Reply by email

C Western
to Пётр Косаревский
Пётр Косаревский wrote 13 lines
> In the math unit are the CompareValue functions only for signed integers. Why not > for DWORD and QWORD? I guess, that it's Delphi mimicing implementation (because it is Delphi-like). And it can be tricky to distinguish signed and unsigned expression values. Like, a: qword; a-1 --- ??? (anyone may know for sure that a-1>0 and want to subtract one) I guess it's something like that.

If you check <http://www.freepascal.org/mantis/view.php?id=8448> you can see this bug in action - the fpcadds and math unit provide signed and unsigned versions of CompareValue, but the correct one is not always chosen.

Colin

Reply by email

Vincent Snijders
to C Western
C Western wrote 22 lines
Пётр Косаревский wrote: >> In the math unit are the CompareValue functions only for signed >> integers. Why not for DWORD and QWORD? > > I guess, that it's Delphi mimicing implementation (because it is > Delphi-like). > > And it can be tricky to distinguish signed and unsigned expression > values. > > Like, > a: qword; > a-1 --- ??? (anyone may know for sure that a-1>0 and want to subtract > one) > > I guess it's something like that. If you check <http://www.freepascal.org/mantis/view.php?id=8448> you can see this bug in action - the fpcadds and math unit provide signed and unsigned versions of CompareValue, but the correct one is not always chosen.

What a lousy compiler we use! ;-)

Do you have a testprogram, so I can make a fpc bug report?

Vincent

Reply by email

Unknown author
to Vincent Snijders

Quoting Vincent Snijders :

Show quoted text 27 lines
C Western schreef: > ð£ÔÒ ëÏÓÁÒÅ×ÓËÉÊ wrote: >>> In the math unit are the CompareValue functions only for signed >>> integers. Why not for DWORD and QWORD? >> >> I guess, that it's Delphi mimicing implementation (because it is >> Delphi-like). >> >> And it can be tricky to distinguish signed and unsigned expression >> values. >> >> Like, >> a: qword; >> a-1 --- ??? (anyone may know for sure that a-1>0 and want to subtract >> one) >> >> I guess it's something like that. > > If you check <http://www.freepascal.org/mantis/view.php?id=8448> you can > see this bug in action - the fpcadds and math unit provide signed and > unsigned versions of CompareValue, but the correct one is not always > chosen. > What a lousy compiler we use! ;-) Do you have a testprogram, so I can make a fpc bug report?

I think this is inherent in the language design, as it is too easy to convert between signed and unsigned integers. For example should the call:

CompareValue(0,0)

use the signed or unsigned version?

Colin

Reply by email

Vincent Snijders
to Unknown author
Show quoted text 33 lines
Quoting Vincent Snijders : > C Western schreef: >> ð£ÔÒ ëÏÓÁÒÅ×ÓËÉÊ wrote: >>>> In the math unit are the CompareValue functions only for signed >>>> integers. Why not for DWORD and QWORD? >>> I guess, that it's Delphi mimicing implementation (because it is >>> Delphi-like). >>> >>> And it can be tricky to distinguish signed and unsigned expression >>> values. >>> >>> Like, >>> a: qword; >>> a-1 --- ??? (anyone may know for sure that a-1>0 and want to subtract >>> one) >>> >>> I guess it's something like that. >> If you check <http://www.freepascal.org/mantis/view.php?id=8448> you can >> see this bug in action - the fpcadds and math unit provide signed and >> unsigned versions of CompareValue, but the correct one is not always >> chosen. >> > What a lousy compiler we use! ;-) > > Do you have a testprogram, so I can make a fpc bug report? I think this is inherent in the language design, as it is too easy to convert between signed and unsigned integers. For example should the call: CompareValue(0,0) use the signed or unsigned version?
But in this particular case, I did not use constants, but variables of
the same type.

Vincent

Reply by email