1

I'm using Eigen library, v.3.2.1. I'm calculating some normals of some planes. Then I want to normalize them. My problem is that for some normals the calculated norm is not precisely 1.00000, that is:

normalA=(0.0000,0.0000,1165.0521)----->normalA.normalize()=(0.0000,0.0000,1.0000)

normalB=(0.0000,0.0000,1165.0524)----->normalB.normalize()=(0.0000,0.0000,1.0000)

normalC=(0.0000,0.0000,312.17474)----->normalC.normalize()=(0.0000,0.0000,1.0000)

normalD=(0.0000,0.0000,2017.9299)----->normalD.normalize()=(0.0000,0.0000,0.99999994)

My problem is that when I compare normalA with normalD c++ return false and my algorithm fails, i.e if(normalA==normalD).

How could I solve this problem? Are there some function to avoid this simple problem? I'm sorry, but I'm a beginner: teach me!

SPS
  • 465
  • 9
  • 23
  • 7
    This kind of error is expected with floats. You should use a tolerance when comparing floating point numbers. [What Every Programmer Should Know About Floating-Point Arithmetic](http://floating-point-gui.de/) – japreiss Mar 31 '14 at 17:04
  • 1
    Also, you should take a look at a [nice previous question here on SO](http://stackoverflow.com/questions/17333/most-effective-way-for-float-and-double-comparison?lq=1). – Dexter Apr 01 '14 at 07:45

1 Answers1

2

As already pointed out, the error is caused by floating point arithmetic. This post

[1]: https://stackoverflow.com/questions/15051367/how-to-compare-vectors-approximately-in-eigen

Provides a nice way to cope with your problem.

Best regards

Community
  • 1
  • 1