# Strange vector behaviour

When i do:

Vector[2,4,6] == Vector[1,2,3]*2
#=> true

It returns true as expected.

However,

Vector[1.1,2.2,3.3] == Vector[1,2,3]*1.1
#=> false

Returns false which shouldn’t be the case.

Any ideas whats going on?

this is using 1.8.7 and the vector class from ‘matrix’.

regards,
tom.

On Sun, Sep 18, 2011 at 3:26 AM, thomas Mulvaney
[email protected] wrote:

Vector[1.1,2.2,3.3] == Vector[1,2,3]*1.1
#=> false

Returns false which shouldn’t be the case.

Any ideas whats going on?

Floating point numbers.

=> Vector[1,2,3]*1.1

Vector[1.1, 2.2, 3.3000000000000003]

Basically, whenever you’re comparing equality of floats, you should
stop and think what you’re doing.

On Sep 17, 2011, at 10:26 PM, thomas Mulvaney wrote:

Vector[2,4,6] == Vector[1,2,3]*2
#=> true

It returns true as expected.

[…]

Vector[1.1,2.2,3.3] == Vector[1,2,3]*1.1
#=> false

Any ideas whats going on?

Integer literals have exact representations while floats do not.
Decimal floating point numbers in general can not be represented exactly
in binary so there is a conversion from the string “1.1”, for example,
to the binary floating point number that is “closest” to 1.1:

“%0.60f” % 1.1 #=>
“1.100000000000000088817841970012523233890533447265625000000000”

Of course this means that multiple of 1.1 aren’t exactly what you might
expect:

“%0.60f” % (1.1 * 3) #=>
“3.300000000000000266453525910037569701671600341796875000000000”

Here is the obligatory reference for this topic: