%% options copyright owner = Dirk Krause copyright year = 2014 license = bsd %% header /** @file dk3mai.h Mathematical operations on int. */ #include #include #if DK3_HAVE_SYS_TYPES_H #include #endif #if DK3_HAVE_STDINT #include #endif #if DK3_HAVE_INTTYPES_H #include #endif #if DK3_HAVE_LIMITS_H #include #endif #if DK3_HAVE_MATH_H #include #endif #if DK3_HAVE_FLOAT_H #include #endif #ifdef __cplusplus extern "C" { #endif /** Absolute value. @param a Original value. @param ec Pointer to error code variable, may be NULL. @return Non-negative value on success, DK3_I_MAX and ec = DK3_ERROR_MATH_OVERFLOW on error. */ int dk3ma_i_abs_ok(int a, int *ec); /** Addition. @param a Left operand. @param b Right operand. @param ec Pointer to error code variable, may be NULL. The variable may be set to DK3_ERROR_MATH_OVERFLOW when returning. @return Summary of a and b. */ int dk3ma_i_add_ok(int a, int b, int *ec); /** Substraction. @param a Left operand. @param b Right operand. @param ec Pointer to error code variable, may be NULL. The variable may be set to DK3_ERROR_MATH_OVERFLOW when returning. @return Difference of a and b. */ int dk3ma_i_sub_ok(int a, int b, int *ec); /** Multiplication. @param a Left operand. @param b Right operand. @param ec Pointer to error code variable, may be NULL. The variable may be set to DK3_ERROR_MATH_OVERFLOW when returning. @return Product of a and b. */ int dk3ma_i_mul_ok(int a, int b, int *ec); /** Division. @param a Left operand (nominator). @param b Right operand (denominator). @param ec Pointer to error code variable, may be NULL. The variable may be set to DK3_ERROR_MATH_OVERFLOW or DK3_ERROR_MATH_DIVZERO when returning. @return Fraction of a and b. */ int dk3ma_i_div_ok(int a, int b, int *ec); /** Greatest common divisor. @param a Left operand. @param b Right operand. @param ec Pointer to error code variable, may be NULL. The variable may be set to DK3_ERROR_MATH_OVERFLOW when returning. @return Greatest common divisor of a and b. */ int dk3ma_i_gcd_ok(int a, int b, int *ec); #ifdef __cplusplus } #endif %% module #include "dk3ma.h" $!trace-include int dk3ma_i_abs_ok(int a, int *ec) { if (NULL != ec) { if (DK3_I_MIN == a) { *ec = DK3_ERROR_MATH_OVERFLOW; } } return ((0 <= a) ? a : (0 - a)); } int dk3ma_i_add_ok(int a, int b, int *ec) { if (NULL != ec) { if ((0 < a) && (0 < b)) { if((DK3_I_MAX - a) < b) { *ec = DK3_ERROR_MATH_OVERFLOW; } } else { if ((0 > a) && (0 > b)) { if (DK3_I_MIN == a) { *ec = DK3_ERROR_MATH_OVERFLOW; } else { if ((DK3_I_MIN - a) > b) { *ec = DK3_ERROR_MATH_OVERFLOW; } } } } } return (a + b); } int dk3ma_i_sub_ok(int a, int b, int *ec) { if (NULL != ec) { if ((0 < a) && (0 > b)) { if((DK3_I_MAX + b) < a) { *ec = DK3_ERROR_MATH_OVERFLOW; } } else { if ((0 > a) && (0 < b)) { if((DK3_I_MIN + b) > a) { *ec = DK3_ERROR_MATH_OVERFLOW; } } } } return (a - b); } int dk3ma_i_mul_ok(int a, int b, int *ec) { if (NULL != ec) { if ((0 != a) && (0 != b)) { if ((DK3_I_MIN == a) && (1 != b)) { *ec = DK3_ERROR_MATH_OVERFLOW; goto checksfinished; } if ((DK3_I_MIN == b) && (1 != a)) { *ec = DK3_ERROR_MATH_OVERFLOW; goto checksfinished; } if ((DK3_I_MAX / dk3ma_i_abs_ok(a, ec)) < dk3ma_i_abs_ok(b, ec)) { *ec = DK3_ERROR_MATH_OVERFLOW; /* goto checksfinished; -- when adding further tests -- */ } } } checksfinished: return (a * b); } int dk3ma_i_div_ok(int a, int b, int *ec) { int back = 0; if (0 != b) { if ((DK3_I_MIN == a) && (-1 == b)) { back = DK3_I_MAX; *ec = DK3_ERROR_MATH_OVERFLOW; } else { back = a / b; } } else { if (NULL != ec) { *ec = DK3_ERROR_MATH_DIVZERO; } if (0 <= a) { back = DK3_I_MAX; } else { back = DK3_I_MIN; } } return back; } int dk3ma_i_gcd_ok(int a, int b, int *ec) { int h; if (0 > a) { if (DK3_I_MIN == a) { a = 0; if (NULL != ec) { *ec = DK3_ERROR_MATH_OVERFLOW; } goto finished; } else { a = 0 - a; } } if (0 > b) { if (DK3_I_MIN == b) { a = 0; if (NULL != ec) { *ec = DK3_ERROR_MATH_OVERFLOW; } goto finished; } else { b = 0 - b; } } while (0 < b) { h = a % b; a = b; b = h; } if (0 == a) { a = 1; } finished: return a; }