1 /* LibTomFloat, multiple-precision floating-point library
3 * LibTomFloat is a library that provides multiple-precision
4 * floating-point artihmetic as well as trigonometric functionality.
6 * This library requires the public domain LibTomMath to be installed.
8 * This library is free for all purposes without any express
11 * Tom St Denis, tomstdenis@iahu.ca, http://float.libtomcrypt.org
15 int mpf_cmp(mp_float
*a
, mp_float
*b
)
19 /* if one is zero than we early out */
20 za
= mp_iszero(&(a
->mantissa
));
21 sa
= a
->mantissa
.sign
;
22 zb
= mp_iszero(&(b
->mantissa
));
23 sb
= b
->mantissa
.sign
;
25 if (za
== MP_YES
&& zb
== MP_NO
) {
26 /* result depends on b */
32 } else if (za
== MP_NO
&& zb
== MP_YES
) {
33 /* result depends on a */
41 /* compare the signs */
42 if (sa
== MP_NEG
&& sb
== MP_ZPOS
) {
44 } else if (sa
== MP_ZPOS
&& sb
== MP_NEG
) {
48 /* they're both non-zero, the same sign and normalized, compare the exponents */
49 if (a
->exp
> b
->exp
) {
50 return (sa
== MP_NEG
) ? MP_LT
: MP_GT
;
51 } else if (a
->exp
< b
->exp
) {
52 return (sa
== MP_NEG
) ? MP_GT
: MP_LT
;
55 /* same exponent and sign, compare mantissa */
56 return mp_cmp(&(a
->mantissa
), &(b
->mantissa
));