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.
    – 94239
    Commented Aug 24, 2019 at 21:40
  • 8
    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.
    – gnasher729
    Commented Aug 24, 2019 at 22:01
  • Its best to publish the code.
    – NoChance
    Commented Aug 25, 2019 at 0:39
  • 1
    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

1 Answer 1


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).


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.