35 #include "qofmath128.c" 45 if ((0 < in.num) || (-4 > in.num))
63 if (b.denom == a.denom)
66 if ((b.denom < a.denom) && ((a.denom % b.denom) == 0))
68 if ((a.denom < b.denom) && ((b.denom % a.denom) == 0))
70 lcm =
lcm128 (a.denom, b.denom);
120 if ((a.num == 0) && (a.denom != 0))
138 if ((a.num < 0) && (a.denom != 0))
156 if ((a.num > 0) && (a.denom != 0))
177 if (a.denom == b.denom)
186 if ((a.denom > 0) && (b.denom > 0))
202 aa = a.num * a.denom;
203 bb = b.num * b.denom;
220 return ((a.num == b.num) && (a.denom == b.denom));
232 if ((a.denom == b.denom) && (a.denom > 0))
233 return (a.num == b.num);
234 if ((a.denom > 0) && (b.denom > 0))
241 #if ALT_WAY_OF_CHECKING_EQUALITY 244 if (ra.denom != rb.denom)
246 if (ra.num != rb.num)
251 if ((a.denom < 0) && (b.denom < 0))
265 return ((a.num * -a.denom * b.denom) == b.num);
267 return (a.num == (b.num * a.denom * -b.denom));
269 return ((a.num * b.denom) == (a.denom * b.num));
305 if (a.denom == b.denom)
334 if (a.denom == b.denom)
336 sum.num = a.num + b.num;
350 lcd = qof_numeric_lcd (a, b);
353 ca =
mult128 (a.num, lcd / a.denom);
356 cb =
mult128 (b.num, lcd / b.denom);
371 denom = qof_numeric_lcd (a, b);
411 if (a.denom == b.denom)
424 denom = qof_numeric_lcd (a, b);
440 bignume =
mult128 (a.num, b.num);
441 bigdeno =
mult128 (a.denom, b.denom);
442 product.num = a.num * b.num;
443 product.denom = a.denom * b.denom;
457 bignume =
mult128 (a.num, b.num);
458 bigdeno =
mult128 (a.denom, b.denom);
459 product.num = a.num * b.num;
460 product.denom = a.denom * b.denom;
473 product = reduce128 (bignume, product.denom);
484 product.num = bignume.lo;
486 product.num = -product.num;
488 product.denom = bigdeno.lo;
489 if (0 == product.denom)
495 if (product.denom < 0)
497 product.num = -product.num;
498 product.denom = -product.denom;
522 if (a.denom == b.denom)
524 else if (a.denom == 0)
542 if (a.denom == b.denom)
544 quotient.num = a.num;
545 quotient.denom = b.num;
560 nume =
mult128 (a.num, b.denom);
561 deno =
mult128 (b.num, a.denom);
568 gint64 gcf_nume =
gcf64 (ra.num, rb.num);
569 gint64 gcf_deno =
gcf64 (rb.denom, ra.denom);
571 nume =
mult128 (ra.num / gcf_nume, rb.denom / gcf_deno);
572 deno =
mult128 (rb.num / gcf_nume, ra.denom / gcf_deno);
577 quotient.num = sgn * nume.lo;
578 quotient.denom = deno.lo;
581 else if (0 == deno.
isbig)
583 quotient = reduce128 (nume, deno.lo);
601 quotient.num = sgn * nume.lo;
602 quotient.denom = deno.lo;
603 if (0 == quotient.denom)
609 if (quotient.denom < 0)
611 quotient.num = -quotient.num;
612 quotient.denom = -quotient.denom;
619 denom = qof_numeric_lcd (a, b);
666 gdouble ratio, logratio;
678 switch (how & QOF_NUMERIC_DENOM_MASK)
692 if (in.denom != denom)
704 logratio = log10 (ratio);
705 logratio = ((logratio > 0.0) ?
706 (floor (logratio) + 1.0) : (ceil (logratio)));
708 sigfigs = QOF_HOW_GET_SIGFIGS (how);
710 if (sigfigs - logratio >= 0)
711 denom = (gint64) (pow (10, sigfigs - logratio));
713 denom = -((gint64) (pow (10, logratio - sigfigs)));
721 if (in.denom == denom)
733 in.num = in.num * (-in.denom);
737 sign = (in.num < 0) ? -1 : 1;
747 temp_a = (in.num < 0) ? -in.num : in.num;
748 temp_bc = in.denom * denom;
749 remainder = temp_a % temp_bc;
750 out.num = temp_a / temp_bc;
759 temp.denom = in.denom;
768 nume =
mult128 (in.num, temp.num);
769 newm =
div128 (nume, temp.denom);
770 remainder =
rem128 (nume, temp.denom);
785 out.num = out.num + 1;
790 out.num = out.num + 1;
797 out.num = out.num + 1;
803 if ((2 * remainder) > in.denom * denom)
804 out.num = out.num + 1;
806 else if ((2 * remainder) > temp.denom)
807 out.num = out.num + 1;
809 else if (((2 * remainder) < remainder) &&
810 (remainder > (temp.denom / 2)))
811 out.num = out.num + 1;
817 if ((2 * remainder) >= in.denom * denom)
818 out.num = out.num + 1;
820 else if ((2 * remainder) >= temp.denom)
821 out.num = out.num + 1;
823 else if (((2 * remainder) < remainder) &&
824 (remainder >= (temp.denom / 2)))
825 out.num = out.num + 1;
831 if ((2 * remainder) > in.denom * denom)
832 out.num = out.num + 1;
833 else if ((2 * remainder) == in.denom * denom)
836 out.num = out.num + 1;
841 if ((2 * remainder) > temp.denom)
842 out.num = out.num + 1;
844 else if (((2 * remainder) < remainder) &&
845 (remainder > (temp.denom / 2)))
847 out.num = out.num + 1;
849 else if ((2 * remainder) == temp.denom)
852 out.num = out.num + 1;
855 else if (((2 * remainder) < remainder) &&
856 (remainder == (temp.denom / 2)))
859 out.num = out.num + 1;
870 out.num = (sign > 0) ? out.num : (-out.num);
886 gint64 num = (in.num < 0) ? (-in.num) : in.num;
887 gint64 denom = in.denom;
904 out.num = in.num / num;
905 out.denom = in.denom / num;
925 if (fabs (in) < 10e-20)
929 logval = log10 (fabs (in));
930 logval = ((logval > 0.0) ?
931 (floor (logval) + 1.0) : (ceil (logval)));
933 sigfigs = QOF_HOW_GET_SIGFIGS (how);
934 if (sigfigs - logval >= 0)
935 denom = (gint64) (pow (10, sigfigs - logval));
937 denom = -((gint64) (pow (10, logval - sigfigs)));
939 how = how & ~QOF_HOW_DENOM_SIGFIG & ~QOF_NUMERIC_SIGFIGS_MASK;
942 int_part = (gint64) (floor (fabs (in)));
943 frac_part = in - (double) int_part;
945 int_part = int_part * denom;
946 frac_part = frac_part * (double) denom;
951 frac_int = (gint64) floor (frac_part);
955 frac_int = (gint64) ceil (frac_part);
959 frac_int = (gint64) frac_part;
964 frac_int = (gint64) rint (frac_part);
968 frac_int = (gint64) floor (frac_part);
969 if (frac_part != (
double) frac_int)
976 out.num = int_part + frac_int;
989 return (gdouble) in.num / (gdouble) in.denom;
991 return (gdouble) (in.num * -in.denom);
1087 gint64 tmpnum = n.num;
1088 gint64 tmpdenom = n.denom;
1091 g_strdup_printf (
"%" G_GINT64_FORMAT
"/%" G_GINT64_FORMAT, tmpnum,
1100 static gchar buff[1000];
1101 static gchar *p = buff;
1102 gint64 tmpnum = n.num;
1103 gint64 tmpdenom = n.denom;
1106 if (p - buff >= 1000)
1109 sprintf (p,
"%" G_GINT64_FORMAT
"/%" G_GINT64_FORMAT, tmpnum,
1118 size_t G_GNUC_UNUSED num_read;
1124 tmpnum = strtoll (str, NULL, 0);
1125 str = strchr (str,
'/');
1129 tmpdenom = strtoll (str, NULL, 0);
1130 num_read = strspn (str,
"0123456789");
1132 n->denom = tmpdenom;
guint64 gcf64(guint64 num, guint64 denom)
QofNumericErrorCode qof_numeric_check(QofNumeric in)
QofNumeric qof_numeric_mul_with_error(QofNumeric a, QofNumeric b, gint64 denom, gint how, QofNumeric *error)
gint qof_numeric_same(QofNumeric a, QofNumeric b, gint64 denom, gint how)
QofNumeric qof_numeric_mul(QofNumeric a, QofNumeric b, gint64 denom, gint how)
gint qof_numeric_compare(QofNumeric a, QofNumeric b)
An exact-rational-number library for QOF.
QofNumeric qof_numeric_sub_with_error(QofNumeric a, QofNumeric b, gint64 denom, gint how, QofNumeric *error)
gint cmp128(QofInt128 a, QofInt128 b)
QofNumeric qof_numeric_error(QofNumericErrorCode error_code)
gint64 rem128(QofInt128 n, gint64 d)
gdouble qof_numeric_to_double(QofNumeric in)
gboolean qof_numeric_equal(QofNumeric a, QofNumeric b)
#define QOF_NUMERIC_RND_MASK
bitmasks for HOW flags.
QofNumeric qof_numeric_div_with_error(QofNumeric a, QofNumeric b, gint64 denom, gint how, QofNumeric *error)
gchar * qof_numeric_dbg_to_string(QofNumeric n)
QofInt128 shift128(QofInt128 x)
QofNumeric qof_numeric_add_with_error(QofNumeric a, QofNumeric b, gint64 denom, gint how, QofNumeric *error)
QofInt128 div128(QofInt128 n, gint64 d)
QofNumeric qof_numeric_abs(QofNumeric a)
gboolean qof_numeric_from_string(const gchar *str, QofNumeric *n)
QofNumeric qof_numeric_reduce(QofNumeric in)
static QofNumeric qof_numeric_create(gint64 num, gint64 denom)
gboolean qof_numeric_zero_p(QofNumeric a)
gboolean qof_numeric_eq(QofNumeric a, QofNumeric b)
QofInt128 add128(QofInt128 a, QofInt128 b)
QofNumeric qof_numeric_neg(QofNumeric a)
QofNumeric qof_numeric_sub(QofNumeric a, QofNumeric b, gint64 denom, gint how)
gchar * qof_numeric_to_string(QofNumeric n)
gboolean qof_numeric_negative_p(QofNumeric a)
gboolean equal128(QofInt128 a, QofInt128 b)
QofNumeric qof_numeric_convert(QofNumeric in, gint64 denom, gint how)
QofNumeric qof_numeric_add(QofNumeric a, QofNumeric b, gint64 denom, gint how)
QofNumeric qof_numeric_from_double(gdouble in, gint64 denom, gint how)
gboolean qof_numeric_positive_p(QofNumeric a)
QofNumeric qof_numeric_div(QofNumeric a, QofNumeric b, gint64 denom, gint how)
QofInt128 mult128(gint64 a, gint64 b)
QofInt128 lcm128(guint64 a, guint64 b)