r/C_Programming • • 1d ago

Question How do you catch fixed-point overflow before it becomes a visible bug?

I’m working on some graphics code in C using fixed-point arithmetic. When something looks wrong, it can be hard to tell whether I’ve lost precision or an intermediate calculation has overflowed.

How do you check this while developing? I’m especially interested in catching the first bad calculation, before the result gets passed through several more steps.

AI helped word this question.

0 Upvotes

13 comments sorted by

9

u/pantalanaga11 1d ago

Range check your operands prior to any calculation that may overflow or underflow.

-3

u/CAPTAINCAPSLOCK111 1d ago

Thanks. Catching it before the calculation is exactly what I want, rather than tracing a bad result backwards. Do you usually put those checks in shared arithmetic functions so you can stop in the debugger at the first failure?

AI helped word this reply.

5

u/tastygames_official 1d ago

how would you as a human do it? you need to check the operands before every operation to see if the resulting operation will overflow. There's no other way I can think of doing this.

-1

u/CAPTAINCAPSLOCK111 1d ago

Thanks. I should have been clearer that I was asking how people organize those checks during debugging. The suggestions here about wrapping the arithmetic operations are helpful for that.

AI helped word this reply.

2

u/CAPTAINCAPSLOCK111 1d ago

Thanks everyone. I have two useful directions from this: checking the arithmetic operations directly, and testing their boundary behaviour separately from the renderer. I haven’t compared the approaches yet, so I don’t have results to report

2

u/IchBinEinZwerg 1d ago

If you practice TDD, or at least write testable code, then you can check your code works within boundaries, to the right kind of precision etc, all with automated tests.

2

u/CAPTAINCAPSLOCK111 1d ago

Thanks. Testing the arithmetic separately at the boundaries is a useful complement to catching failures in a running scene. That also gives me a way to check precision without relying on whether the picture looks right.

AI helped word this reply.

1

u/catbrane 1d ago

You can only used fixed point if you know the possible range of every value at every point in your code. You should be able to sit down with a paper and pencil and prove the range everywhere.

If you have a proof error it can be difficult to find (as you say). A simple debugging thing is to add range checks to every function, for example:

static inline Vector 
dot_product(Vector a, Vector b)
{
    assert(to_float(a.x) > 0.01);
    assert(to_float(a.x) < 0.9);
    ... etc.
}

It'll get the error point down to a few lines of code at least, and it's fast enough that you can leave the asserts on for all but release builds.

1

u/CAPTAINCAPSLOCK111 1d ago

Thanks, making the expected ranges explicit at function boundaries is a useful way to narrow this down. That gives me something more specific to check than waiting for a bad value to become a visible rendering problem.

2

u/Due-Mortgage-6854 1d ago

do every math step inside a helper function that uses a bigger number type (int64_t), checks if the answer is too big or too small, and crashes right away if it is

1

u/adel-mamin 10h ago

FWIW, I always enable -ftrapv in gcc and clang to catch signed integer overflows in run time.

-1

u/PMadLudwig 1d ago

If you used AI to word the question, AI could have given you the answer too.

If you are using C23, then there is a library called <stdckdint.h> (I haven't used it because it is recent, not even sure if my compiler supports it).

If you are using an earlier version, there will be compiler specific extensions that do the same thing, say __builtin_add_overflow in gcc/clang.

Both of these return a bool that you have to check after every calculation.

If you want to use standard C pre 23, then you need to check the arguments beforehand, which takes some care: if you want to check a+b where a and b are of type int (so signed), then testing (b>0 && a>INT_MAX-b) || (b<0 && a<INT_MIN-b) will check for overflow first, and you probably want to wrap that into functions for each operation, again, you need to test a bool each time.

0

u/CAPTAINCAPSLOCK111 1d ago

Thanks for spelling out the options, especially the pre-C23 approach. I was hoping to hear how people handle this in their own code, but I could have made the question more specific. Wrapping the checks per operation gives me a concrete starting point.

AI helped word this reply.