substracting position from position not resulting in 0

Hi i am trying to move a cube in only horizontal way , Horizontal axis in my case is Z ,

when player clicks on character that moves in horizontal axis , i save his position and when player does 2nd click on the screen i save that variable too .

Now i am doing

if (this.gameObject.transform.position.z - targetPosition.z == 0) {
  print("say hi ");
}

but its not working
when i try to print values of both z positions i get same number :

12.50797

		print (this.gameObject.transform.position.z);
		print (targetPosition.z);

but if i do

		print (this.gameObject.transform.position.z - targetPosition.z);

i get wierd number :

-9.536743E-07

You’re dealing with floating point numbers. The way they work introduce tiny errors.

The value -9.536743E-07 is actually: -0.0000009536743

So the error is very very small.

See those links for more details:

Why Are Floating Point Numbers Inaccurate?

What Every Computer Scientist Should Know About Floating-Point Arithmetic

You have to check a certain range. For example:

 if (Mathf.Abs(transform.position.z - targetPosition.z) < 0.0001f) {
   print("say hi ");
 }

Or if you want to compare two vectors as a whole:

 if (Vector3.Distance(transform.position, targetPosition) < 0.0001f) {
   print("say hi ");
 }