4

I have code (Python) that must perform some operations regarding distances between reflected segments of a curve.

In order to make the thinking and the code clearer, I apply two rotations (using matrix multiplication) before performing the actual calculation. I suppose it would be possible to perform the calculation without any rotation at all, but the code and the thinking would be much more awkward.

What I have to ask is: are the three rotations too much of a penalty in terms of lost precision because of rounding-off floating point errors? Is there a way to estimate the magnitude of this error?

Thanks for reading

heltonbiker
  • 26,657
  • 28
  • 137
  • 252
  • Are the results of these transformations going to be cumulative? i.e., are you using the outputs as inputs iteratively? Also, what is your order of operations? Are you merging the three rotation matricies first, or are you rotating your points successively? – nicholas Aug 13 '12 at 21:25
  • I first rotate, then slice, then rotate again, then reflect, then calculate distances between some points. (well, that actualy makes for only TWO rotations). – heltonbiker Aug 13 '12 at 21:55
  • can you explain what you mean by slice? also, what is the general application? (physics engine, ray tracing, game, etc.) – nicholas Aug 13 '12 at 22:12

2 Answers2

4

As a rule of thumb in numerical calculations -- only take the first 12 digits seriously :)

Now, assuming 3D rotations, and that outcomes of trig functions are infinitely precise, a matrix multiplication will involve 3 multiplications and 2 additions per element in the rotated vector. Since you do two rotations, this amounts to 6 multiplications and 4 additions per element.

If you read this (which you should read front to back one day), or this, or this, you'll find that individual arithmetic operations of IEEE 754 are guaranteed to be accurate to within half a ULP (=last decimal place).

Applied to your problem, that means that the 10 operations per element in the result vector will be accurate to within 5 ULPs.

In other words -- suppose you're rotating a unit vector. The elements of the rotated vector will be accurate to 0.000000000000005 -- I'd say that's nothing to worry about.

Including the errors in the trig functions, well, that's a bit more complicated...that really depends on your programming language and/or version of your compiler etc. But I guarantee it'll be comparable to the 5 ULPs.

If you do think this accuracy is not going to be enough, then I'd suggest you perform the two rotations in one go. Work out the matrix multiplication analytically, and implement the rotation as a single matrix multiplication. Alternatively: have a look at quaternions (although I suspect that's a bit overkill for your situation).

Rody Oldenhuis
  • 37,726
  • 7
  • 50
  • 96
  • Nice facts. I'll choose this one because of the possible ways to improve acuracy. By the way, I am already sharpening my programming knife to go into quaternions and homogeneous coordinates, that's the way to go sooner or later... – heltonbiker Aug 14 '12 at 13:20
1

What you need to do is compute the condition number of your operations and determine whether it may incur loss of significance. That should allow you to estimate the error that could be introduced.

Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
  • From wikipedia on 'condition number': "For example, the condition number associated with the linear equation `Ax = b` gives a bound on how inaccurate the solution `x` will be after approximate solution. Note that this is before the effects of round-off error are taken into account; conditioning is a property of the matrix, not the algorithm or floating point accuracy of the computer used to solve the corresponding system." So although this is a relevant answer, it does *not* give an estimate on the rounding error - that still needs to be added to the conditioning error. – Rody Oldenhuis Aug 14 '12 at 06:43
  • This is a very interesting answer. And, specifically, knowing that condition number depends only on the matrix itself is a very relevant fact. – heltonbiker Aug 14 '12 at 13:21