jart/jtckdint: C23 Checked Arithmetic
github.com·1d
💧Liquid Types
Preview
Report Post

jtckdint.h is a portable single-file header-only library that defines three type generic functions:

Which allow integer arithmetic errors to be detected. There are many kinds of integer errors, e.g. overflow, truncation, etc. These funcs catch them all. Here’s an example of how it works:

Experienced C / C++ users should find this example counter-intuitive because the expression 0x7fffffff + 2 not only overflows it’s also undefined behavior. However here we see it’s specified, and does not result in an error. That’s because C23 checked arithmetic is not the arithmetic you’re used to.

C23 checked arithmetic is defined as performing the arithmetic using infinite precision and then checking if the resulting value will f…

Similar Posts

Loading similar posts...