Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: [C++] How to avoid arithmetic failure? (ignore, solved)  (Read 748 times)


  • Bay Watcher
    • View Profile
[C++] How to avoid arithmetic failure? (ignore, solved)
« on: April 10, 2009, 04:11:52 am »

So, I'm writing a 2d physics library to use in a future side-scroller game. Now, I've run into a problem while implementing rotations: the program fails at basic arithmetic. Here's a concrete example:

I'm trying to rotate point A (0, 0) by 90 degrees around point B (1, 1). What I get is rotated point A(2, 3.71403e-007) instead of A(2, 0). Going trough the debugger I've found that it fails at this line:

y = center->get_y() + sin(points_angle) * points_distance;

where the values are:

      center->get_y()   1.0000000   float
      sin(points_angle)   -0.70710653   float
      points_distance   1.4142135   float
      sin(points_angle) * points_distance   -0.99999962859667590   double
      center->get_y() + sin(points_angle) * points_distance   3.7140332409535404e-007   double

As you can see, it works OK until it tries to add 1 and -0.99999962859667590. So it's clearly failing at basic arithmetic. I've tried typecasting all the numbers as floats (I can't really cast them as ints as it'd break rotation for other angles), but it didn't help at all.

So, how do I solve this problem so it never comes up again?

*edit* Apparently, I didn't understand the e-n notation. Everything was in fact working just fine.
« Last Edit: April 10, 2009, 05:28:31 am by DJ »
Urist, President has immigrated to your fortress!
Urist, President mandates the Dwarven Bill of Rights.

Cue magma.
Ah, the Magma Carta...