%% options copyright owner = Dirk Krause copyright year = 2014 license = bsd %% header /** @file dk3mas.h Mathematical operations on short. */ #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_S_MAX and ec = DK3_ERROR_MATH_OVERFLOW on error. */ short dk3ma_s_abs_ok(short 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. */ short dk3ma_s_add_ok(short a, short 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. */ short dk3ma_s_sub_ok(short a, short 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. */ short dk3ma_s_mul_ok(short a, short 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. */ short dk3ma_s_div_ok(short a, short 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. */ short dk3ma_s_gcd_ok(short a, short b, int *ec); #ifdef __cplusplus } #endif %% module #include "dk3ma.h" $!trace-include short dk3ma_s_add_ok(short a, short b, int *ec) { if (NULL != ec) { if ((0 < a) && (0 < b)) { if((DK3_S_MAX - a) < b) { *ec = DK3_ERROR_MATH_OVERFLOW; } } else { if ((0 > a) && (0 > b)) { if (DK3_S_MIN == a) { *ec = DK3_ERROR_MATH_OVERFLOW; } else { if ((DK3_S_MIN - a) > b) { *ec = DK3_ERROR_MATH_OVERFLOW; } } } } } return (a + b); } short dk3ma_s_sub_ok(short a, short b, int *ec) { if (NULL != ec) { if ((0 < a) && (0 > b)) { if((DK3_S_MAX + b) < a) { *ec = DK3_ERROR_MATH_OVERFLOW; } } else { if ((0 > a) && (0 < b)) { if((DK3_S_MIN + b) > a) { *ec = DK3_ERROR_MATH_OVERFLOW; } } } } return (a - b); } short dk3ma_s_abs_ok(short a, int *ec) { if (NULL != ec) { if (DK3_S_MIN == a) { *ec = DK3_ERROR_MATH_OVERFLOW; } } return((0 <= a) ? a : ((short)(0 - a))); } short dk3ma_s_mul_ok(short a, short b, int *ec) { if (NULL != ec) { if ((0 != a) && (0 != b)) { if ((DK3_S_MIN == a) && (1 != b)) { *ec = DK3_ERROR_MATH_OVERFLOW; } else { if ((DK3_S_MIN == b) && (1 != a)) { *ec = DK3_ERROR_MATH_OVERFLOW; } else { if ((DK3_S_MAX / dk3ma_s_abs_ok(a, ec)) < dk3ma_s_abs_ok(b, ec)) { *ec = DK3_ERROR_MATH_OVERFLOW; } } } } } return (a * b); } short dk3ma_s_div_ok(short a, short b, int *ec) { short back = (short)0; if (0 != b) { if ((DK3_S_MIN == a) && (-1 == b)) { back = DK3_S_MAX; *ec = DK3_ERROR_MATH_OVERFLOW; } else { back = a / b; } } else { if (NULL != ec) { *ec = DK3_ERROR_MATH_DIVZERO; } if (0 <= a) { back = DK3_S_MAX; } else { back = DK3_S_MIN; } } return back; } short dk3ma_s_gcd_ok(short a, short b, int *ec) { short h; /* Check a */ if (0 > a) { if (DK3_S_MIN == a) { a = (short)0; if (NULL != ec) { *ec = DK3_ERROR_MATH_OVERFLOW; } goto finished; } else { a = (short)(0 - a); } } /* Check b */ if (0 > b) { if (DK3_S_MIN == b) { a = (short)0; if (NULL != ec) { *ec = DK3_ERROR_MATH_OVERFLOW; } goto finished; } else { b = (short)(0 - b); } } /* Calculate gcd. */ while (0 < b) { h = a % b; a = b; b = h; } if(0 == a) { a = (short)1; } /* Return result. */ finished: return a; }