Transformation#identity? not working

When working with transformations I noticed that the built in Transformation#identity? doesn’t function (probably due to floating point precision). I need to check if a transformation is the identity transformation and only move instances that actually need to be moved. However when checking (t*(t.inverse)).identity? the result is in most cases false even though it by definition should be true.

t is given by selecting an instance and run t=Sketchup.active_model.selection.first.transformation. (t*(t.inverse)).to_a typically looks like [1.0, 0.0, -2.77555756156289e-017, 0.0, 0.0, 1.0, 0.0, 0.0, -2.77555756156289e-017, -5.55111512312578e-017, 1.0, 0.0, 0.0, 0.0, 1.4210854715202e-014, 1.0]

I wrote this snippet that relies on the Length#== method which returns true if the values are close enough. It seems to work but I’m not a mathematician or a computer scientist (or even a trained programmer) and don’t know if the same precision can/should be used in practical applications when checking lengths and transformation matrices, so for now I consider this being a hack.

def identity_matrix?(t)
  
  a = t.to_a
  a_ref = Geom::Transformation.new.to_a
  a.each_with_index do |n, i|
    return false unless n.to_l == a_ref[i].to_l
  end
  
  true
  
end

If anyone knows a better way to do this please tell me.

edit: more tests shows that selecting an instance lined up with the model coordinates and running Sketchup.active_model.selection.first.transformation.identity? returns false even tough the same transformation translates to the array [1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0]. it’s not a precision error in the built in method. The method doesn’t seem to check the matrix at all :S .

I moved 4 posts to an existing topic: Ruby API: Transformation Class’s “identity?” issue