Board index » delphi » "double" types with "<", ">" etc won't work

"double" types with "<", ">" etc won't work

I'd be grateful if anyone could tell me how I can compare two "double" numbers
using the "<" and ">" relational operators.

if var1 and var2 are doubles, the following fails to compile:-

if var1 > var2 then

It fails with "type mismatch" and the cursor is positioned by the '>'. I
assume this is because doubles can't be compared in this way. How can I do
it?!!! I've checked the manual, help and the Delphi Knowledge Base!

Thanks in advance,

Mark

 

Re:"double" types with "<", ">" etc won't work


Quote
MARK CHRISTOPHER HUDSON wrote:

> I'd be grateful if anyone could tell me how I can compare two "double" numbers
> using the "<" and ">" relational operators.

> if var1 and var2 are doubles, the following fails to compile:-

> if var1 > var2 then

> It fails with "type mismatch" and the cursor is positioned by the '>'. I
> assume this is because doubles can't be compared in this way. How can I do
> it?!!! I've checked the manual, help and the Delphi Knowledge Base!

Mark,

Your fragment seems okay to me.  I even tried it myself and it compiled successfully.

However beware of operator precedence!  The following will NOT compile:

var
   var1, var2, var3, var4 : double;
begin
   ...
   if var1 > var2 and var3 > var4 then ...
end;

This is due to the logical operator 'and' having higher precedence than the relational operator '>'.
Thus necessitating parentheses:

   if (var1 > var2) and (var3 > var4) then ...

Andy Coshan

Other Threads