# What is the proper way to deal with rounding error?

I am currently working on a Physics Engine in C++. The Physics Engine is for a sandbox video game. I want to build a powerful physics engine, so therefore, it needs to be very precise.

I have written the following function:

``````Vector3D* rotationX(double theta);
``````

This function will rotate a vector around the x-axis at theta degrees. My test attempts to rotate the vector <0,1,0> about the x-axis, which should be the vector <0,0,1>. The output is the following vector, which is very close to what it should be: <0,0.000796327,1>.

I am aware of the floor function, but I think this isn't precise enough since my 3-d vector class should be able to handle real numbers; that is, not just integers.

• You are not handling real numbers. You are not even handling rational numbers. You are handling floating point numbers. If you need to handle rational numbers, you'll need a library for handling arbitrary precision "decimal" numbers. Commented Aug 24, 2019 at 21:40
• The result you have is not very close at all. That's not due to rounding error, there is some serious bug in your code. Commented Aug 24, 2019 at 22:01
• Its best to publish the code. Commented Aug 25, 2019 at 0:39
• Have you quantified how precise it needs to be? Commented Aug 26, 2019 at 16:50
• @TKK problems fixed. Commented Aug 26, 2019 at 16:54

You have basically the following options:

1.) Do nothing. Maybe the result is precise enough for your use case. Note when using floating point operations, it is quite normal you have to deal with some rounding errors. I would recommend to have a look at What Every Computer Scientist Should Know About Floating-Point Arithmetic.

2.) If your use case involves comparing two vectors for equality, make sure you compare against some "epsilon", where epsilon is about 10^(-3).

3.) Find out why the result is that much imprecise. A rotation involving standard trigonometric functions which uses 64 bit double precision should usually bring results up to a preciseness better than 10^(-10) to my experience.

In between, it became clear (see comments) that in this case the root cause was using 3.14 as an approximation for Pi. It is usually a good idea to use predefined constants for such standard values whenever they are available in the used environment. In C++, for example, one can use the constant `M_PI` from `<math.h>` (see this SO post).

• I am using trigonometric functions for rotation, and in particular the following: en.wikipedia.org/wiki/Rotation_matrix#In_three_dimensions Commented Aug 24, 2019 at 21:52
• Also, the reason why it's not precise is because the value 0.000796327 should be 0. Commented Aug 24, 2019 at 22:00
• I am using a double data type. Commented Aug 24, 2019 at 22:02
• Let me make an educated guess: You are using a five or six digit approximation for pi somewhere. Commented Aug 24, 2019 at 22:04
• Seems like it's time for @GaryDrocella to do some more reading: en.wikipedia.org/wiki/Significant_figures Commented Aug 25, 2019 at 18:00