IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
To/From Code
   ► KBTo/From GuidesReferenceOperators  Print This     

Comparison Operators (Cross Ref > Operators)

Comparison Operators

General Info: Round Floating Point Numbers

When comparing floating point numbers, make sure you round to an acceptable level of rounding for the type of application you are using.

Languages Focus

A comparison operator compares two values either literals as in "Hello" and 3 or variables as in X and Counter. Most languages use the same operators for comparing both numbers and strings. Perl, for example, uses separate sets of comparison operators for numbers and strings.

Access VBA:   =, <>

Save as VB Classic. Common comparison operators:

= equal
<> not equal
< less than
> greater than
<= less than or equal
>= greater than or equal

Syntax Example:
//Does Access evaluate the math correctly? No!
If 0.1 + 0.1 + 0.1 = 0.3 Then
MsgBox "correct"
Else
MsgBox "not correct"
End If

More Info

Tip:  Round Floating Point Numbers

ASP Classic:   =, <>

Save as VB Classic. Common comparison operators:

= equal
<> not equal
< less than
> greater than
<= less than or equal
>= greater than or equal
Syntax Example:
//Does ASP evaluate the math correctly? No!
If .1 + .1 + .1 = .3 Then
Response.Write "correct"
Else
Response.Write "not correct"
End If

The Fix

There are several techniques for handling computer rounding errors. For ASP Classic, consider using the Round function. For example:

If Round(.1+.1+.1, 4) = .3 Then
  Response.Write "yes" & "&lt;br&gt;" 'Yes is displayed!
Else
  Response.Write "no" & "&lt;br&gt;"
End If

Reserve Floating Point Values

Because computers have trouble representing floating point values, you may want to reserve the use of floating point literals for imprecise measurements such length, height, weight, etc. The very nature of measurements is imprecise.

More Info

Tip:  Round Floating Point Numbers

C#:   ==, !=

Common comparison operators:

== equal
!= not equal
< less than
> greater than
<= less than or equal
>= greater than or equal

Syntax Example:
//Does C# evaluate the math correctly? No!
if (.1 + .1 + .1 == .3)
MessageBox.Show("correct");
else
MessageBox.Show("not correct");


More Info

Tip:  Round Floating Point Numbers

C++:   ==, !=

Common comparison operators:

== equal
!= not equal
< less than
> greater than
<= less than or equal
>= greater than or equal

Syntax Example:
//C++Builder example (ShowMessage is a VCL method).
//Does C++Builder evaluate the math correctly? No!
If (.1 + .1 + .1 == .3)
ShowMessage("correct");
else
ShowMessage("not correct");


More Info

Tip:  Round Floating Point Numbers

C++/CLI:   ==, !=

Same as standard C++. Common comparison operators:

== equal
!= not equal
< less than
> greater than
<= less than or equal
>= greater than or equal
Syntax Example:
//Does C++/CLI evaluate the math correctly? No!
if (0.1 + 0.1 + 0.1 == 0.3)
MessageBox::Show("correct");
else
MessageBox::Show("not correct");


More Info

Tip:  Round Floating Point Numbers

Corel Paradox:   =, <>

Common comparison operators:

= equal
<> not equal
< less than
> greater than
<= less than or equal
>= greater than or equal

Syntax Example:
'Does ObjectPAL evaluate the math correctly? No!
If .1 + .1 + .1 = .3 Then
msgInfo("", "correct")
Else
msgInfo("", "not correct")
endIf

Overcoming Floating Point Errors

Because computers have trouble with certain floating point values (such as .1), you need to decide what level of accuracy is "good enough" for your application. Perhaps establish a standard for each application. For example, you could establish 6 decimal points as a standard and require all developers to use ObjectPAL's round() method when comparing floating point number. For example, you could rewrite the above routine as follows and get the result you expect.

If round(.1 + .1 + .1, 6) = round(.3, 6) Then
msgInfo("", "correct")
Else
msgInfo("", "not correct")
EndIf

 

More Info

Code:  ObjectPAL Comparison Operators (=, <>)
Tip:  Round Floating Point Numbers

Delphi:   =, <>

Common comparison operators:

= equal
<> not equal
< less than
> greater than
<= less than or equal
>= greater than or equal

Syntax Example:
//Does Delphi evaluate the math correctly? Yes!
//Refer to math.pas MaxSingle for more info.
if (0.1 + 0.1 + 0.1 = 0.3) then
ShowMessage('correct')
else
ShowMessage('not correct')

Delphi 2009 Working Example

Here's a simple working example from a button click event:

procedure TForm1.Button1Click(Sender: TObject);

var

x: Boolean;

begin

x := True;


if x = True then

begin

ShowMessage('X was true');

end

else

begin

ShowMessage('X was false');

end;

end;

More Info

Code:  Delphi Comparison Operators (=, <>)
Tip:  Round Floating Point Numbers

Delphi Prism:   =, <>

Same as Delphi. Common comparison operators:

= equal
<> not equal
< less than
> greater than
<= less than or equal
>= greater than or equal
Syntax Example:
//Does Prism evaluate the math correctly? No!
//This is different than later versions of 
//Delphi that muse MaxSingle in math.pas.
If .1 + .1 + .1 = .3 Then
MessageBox.Show("correct")
Else
MessageBox.Show("not correct");

More Info

Tip:  Round Floating Point Numbers

Java:   ==, !=

The Java comparison operators are:

== equal
!= not equal
< less than
> greater than
<= less than or equal
>= greater than or equal

More Info / Comment

More Info

Tip:  Round Floating Point Numbers

JavaScript:   ==, !=

Common comparison operators:

== equal
!= not equal
< less than
> greater than
<= less than or equal
>= greater than or equal

Syntax Example:
//Does JavaScript evaluate the math correctly? No!
if (.1 + .1 + .1 == .3) {
document.write("correct");
}
else {
document.write("not correct");
}

More Info

Tip:  Round Floating Point Numbers

Perl:   ==, !=

Common comparison operators:

== equal
!= not equal
< less than
> greater than
<= less than or equal
>= greater than or equal

Perl also offers additional string comparison operators:

eq string equals
ne string not equal
lt string less than
gt string greater than
le string less than or equal
ge string greater than or equal

Syntax Example:
#Does Perl evaluate the math correctly? No!
if ((.1 + .1 + .1) == .3) {
print("Correct<br>");
} else {
print("Not correct<br>");
}

More Info

Tip:  Round Floating Point Numbers

PHP:   ==, != or <>

Common comparison operators:

== equal
!= or <> not equal
< less than
> greater than
<= less than or equal
>= greater than or equal

PHP 4 and above also offers === for indentical (equal plus same type) and !== for not identical (not equal or not same type).

Syntax Example:
//Does PHP evaluate the math correctly? No!
if (.1 + .1 + .1 == .3) {
echo "correct";
}
else {
echo "not correct";
}

More Info

Tip:  Round Floating Point Numbers

VB Classic:   =, <>

Common comparison operators:

= equal
<> not equal
< less than
> greater than
<= less than or equal
>= greater than or equal
Syntax Example:
//Does VB evaluate the math correctly? No!
If 0.1 + 0.1 + 0.1 = 0.3 Then
MsgBox "correct"
Else
MsgBox "not correct"
End If

More Info

Tip:  Round Floating Point Numbers

VB.Net:   =, <>

Save as VB Classic. Common comparison operators:

= equal
<> not equal
< less than
> greater than
<= less than or equal
>= greater than or equal

Syntax Example:
//Does VB.Net evaluate the math correctly? No!
If 0.1 + 0.1 + 0.1 = 0.3 Then
MessageBox.Show("correct")
Else
MessageBox.Show("not correct")
End If




Linked Certification Question(s)

The following are practice certification questions with answers highlighted. These questions were prepared by Mike Prestwood and are intended to stress an important aspect of this KB post. All our practice questions are intended to prepare you generally for passing any certification test as well as prepare you for professional work.

Beginner

1 Beginner Level Question

Question #1: Multiple Choice

Which code statement is the correct use of the comparison operator?

Answer:
1. 
If x == True Then
  MessageBox.Show("x is true")
End If
2. 
If (x = True)
  MessageBox.Show("x is true")
End If
3. 
If (x == True)
  MessageBox.Show("x is true")
End If
4. 
If (x == True) Then
  MessageBox.Show("x is true")
End If
5. 
If x = True Then
  MessageBox.Show("x is true")
End If

More Info

Tip:  Round Floating Point Numbers
 




Go ahead!   Use Us! Call: 916-726-5675  Or visit our new sales site: 
www.prestwood.com


©1995-2024 Prestwood IT Solutions.   [Security & Privacy]