35 #include "qofmath128.c"
50 if ((0 < in.num) || (-4 > in.num))
68 if (b.denom == a.denom)
71 if ((b.denom < a.denom) && ((a.denom % b.denom) == 0))
73 if ((a.denom < b.denom) && ((b.denom % a.denom) == 0))
75 lcm =
lcm128 (a.denom, b.denom);
125 if ((a.num == 0) && (a.denom != 0))
143 if ((a.num < 0) && (a.denom != 0))
161 if ((a.num > 0) && (a.denom != 0))
182 if (a.denom == b.denom)
191 if ((a.denom > 0) && (b.denom > 0))
207 aa = a.num * a.denom;
208 bb = b.num * b.denom;
225 return ((a.num == b.num) && (a.denom == b.denom));
237 if ((a.denom == b.denom) && (a.denom > 0))
238 return (a.num == b.num);
239 if ((a.denom > 0) && (b.denom > 0))
246 #if ALT_WAY_OF_CHECKING_EQUALITY
249 if (ra.denom != rb.denom)
251 if (ra.num != rb.num)
256 if ((a.denom < 0) && (b.denom < 0))
270 return ((a.num * -a.denom * b.denom) == b.num);
272 return (a.num == (b.num * a.denom * -b.denom));
274 return ((a.num * b.denom) == (a.denom * b.num));
310 if (a.denom == b.denom)
339 if (a.denom == b.denom)
341 sum.num = a.num + b.num;
355 lcd = qof_numeric_lcd (a, b);
358 ca =
mult128 (a.num, lcd / a.denom);
361 cb =
mult128 (b.num, lcd / b.denom);
376 denom = qof_numeric_lcd (a, b);
416 if (a.denom == b.denom)
429 denom = qof_numeric_lcd (a, b);
445 bignume =
mult128 (a.num, b.num);
446 bigdeno =
mult128 (a.denom, b.denom);
447 product.num = a.num * b.num;
448 product.denom = a.denom * b.denom;
462 bignume =
mult128 (a.num, b.num);
463 bigdeno =
mult128 (a.denom, b.denom);
464 product.num = a.num * b.num;
465 product.denom = a.denom * b.denom;
478 product = reduce128 (bignume, product.denom);
489 product.num = bignume.lo;
491 product.num = -product.num;
493 product.denom = bigdeno.lo;
494 if (0 == product.denom)
500 if (product.denom < 0)
502 product.num = -product.num;
503 product.denom = -product.denom;
527 if (a.denom == b.denom)
529 else if (a.denom == 0)
547 if (a.denom == b.denom)
549 quotient.num = a.num;
550 quotient.denom = b.num;
565 nume =
mult128 (a.num, b.denom);
566 deno =
mult128 (b.num, a.denom);
573 gint64 gcf_nume =
gcf64 (ra.num, rb.num);
574 gint64 gcf_deno =
gcf64 (rb.denom, ra.denom);
576 nume =
mult128 (ra.num / gcf_nume, rb.denom / gcf_deno);
577 deno =
mult128 (rb.num / gcf_nume, ra.denom / gcf_deno);
582 quotient.num = sgn * nume.lo;
583 quotient.denom = deno.lo;
586 else if (0 == deno.
isbig)
588 quotient = reduce128 (nume, deno.lo);
606 quotient.num = sgn * nume.lo;
607 quotient.denom = deno.lo;
608 if (0 == quotient.denom)
614 if (quotient.denom < 0)
616 quotient.num = -quotient.num;
617 quotient.denom = -quotient.denom;
624 denom = qof_numeric_lcd (a, b);
671 gdouble ratio, logratio;
683 switch (how & QOF_NUMERIC_DENOM_MASK)
697 if (in.denom != denom)
709 logratio = log10 (ratio);
710 logratio = ((logratio > 0.0) ?
711 (floor (logratio) + 1.0) : (ceil (logratio)));
713 sigfigs = QOF_HOW_GET_SIGFIGS (how);
715 if (sigfigs - logratio >= 0)
716 denom = (gint64) (pow (10, sigfigs - logratio));
718 denom = -((gint64) (pow (10, logratio - sigfigs)));
726 if (in.denom == denom)
738 in.num = in.num * (-in.denom);
742 sign = (in.num < 0) ? -1 : 1;
752 temp_a = (in.num < 0) ? -in.num : in.num;
753 temp_bc = in.denom * denom;
754 remainder = temp_a % temp_bc;
755 out.num = temp_a / temp_bc;
764 temp.denom = in.denom;
773 nume =
mult128 (in.num, temp.num);
774 newm =
div128 (nume, temp.denom);
775 remainder =
rem128 (nume, temp.denom);
790 out.num = out.num + 1;
795 out.num = out.num + 1;
802 out.num = out.num + 1;
808 if ((2 * remainder) > in.denom * denom)
809 out.num = out.num + 1;
811 else if ((2 * remainder) > temp.denom)
812 out.num = out.num + 1;
814 else if (((2 * remainder) < remainder) &&
815 (remainder > (temp.denom / 2)))
816 out.num = out.num + 1;
822 if ((2 * remainder) >= in.denom * denom)
823 out.num = out.num + 1;
825 else if ((2 * remainder) >= temp.denom)
826 out.num = out.num + 1;
828 else if (((2 * remainder) < remainder) &&
829 (remainder >= (temp.denom / 2)))
830 out.num = out.num + 1;
836 if ((2 * remainder) > in.denom * denom)
837 out.num = out.num + 1;
838 else if ((2 * remainder) == in.denom * denom)
841 out.num = out.num + 1;
846 if ((2 * remainder) > temp.denom)
847 out.num = out.num + 1;
849 else if (((2 * remainder) < remainder) &&
850 (remainder > (temp.denom / 2)))
852 out.num = out.num + 1;
854 else if ((2 * remainder) == temp.denom)
857 out.num = out.num + 1;
860 else if (((2 * remainder) < remainder) &&
861 (remainder == (temp.denom / 2)))
864 out.num = out.num + 1;
875 out.num = (sign > 0) ? out.num : (-out.num);
891 gint64 num = (in.num < 0) ? (-in.num) : in.num;
892 gint64 denom = in.denom;
909 out.num = in.num / num;
910 out.denom = in.denom / num;
930 if (fabs (in) < 10e-20)
934 logval = log10 (fabs (in));
935 logval = ((logval > 0.0) ?
936 (floor (logval) + 1.0) : (ceil (logval)));
938 sigfigs = QOF_HOW_GET_SIGFIGS (how);
939 if (sigfigs - logval >= 0)
940 denom = (gint64) (pow (10, sigfigs - logval));
942 denom = -((gint64) (pow (10, logval - sigfigs)));
944 how = how & ~QOF_HOW_DENOM_SIGFIG & ~QOF_NUMERIC_SIGFIGS_MASK;
947 int_part = (gint64) (floor (fabs (in)));
948 frac_part = in - (double) int_part;
950 int_part = int_part * denom;
951 frac_part = frac_part * (double) denom;
956 frac_int = (gint64) floor (frac_part);
960 frac_int = (gint64) ceil (frac_part);
964 frac_int = (gint64) frac_part;
969 frac_int = (gint64) rint (frac_part);
973 frac_int = (gint64) floor (frac_part);
974 if (frac_part != (
double) frac_int)
981 out.num = int_part + frac_int;
994 return (gdouble) in.num / (gdouble) in.denom;
996 return (gdouble) (in.num * -in.denom);
1092 gint64 tmpnum = n.num;
1093 gint64 tmpdenom = n.denom;
1096 g_strdup_printf (
"%" G_GINT64_FORMAT
"/%" G_GINT64_FORMAT, tmpnum,
1105 static gchar buff[1000];
1106 static gchar *p = buff;
1107 gint64 tmpnum = n.num;
1108 gint64 tmpdenom = n.denom;
1111 if (p - buff >= 1000)
1114 sprintf (p,
"%" G_GINT64_FORMAT
"/%" G_GINT64_FORMAT, tmpnum,
1130 #ifdef QOF_DEPRECATED
1133 &tmpnum, &tmpdenom, &num_read) < 2)
1138 tmpnum = strtoll (str, NULL, 0);
1139 str = strchr (str,
'/');
1143 tmpdenom = strtoll (str, NULL, 0);
1144 num_read = strspn (str,
"0123456789");
1147 n->denom = tmpdenom;
1154 #ifdef _QOF_NUMERIC_TEST
1163 g_strdup_printf (
"<ERROR> [%" G_GINT64_FORMAT
" / %"
1164 G_GINT64_FORMAT
"]", in.num, in.denom);
1169 g_strdup_printf (
"[%" G_GINT64_FORMAT
" / %" G_GINT64_FORMAT
1170 "]", in.num, in.denom);
1185 printf (
"add 100ths/error : %s + %s = %s + (error) %s\n\n",
1186 qof_numeric_print (a), qof_numeric_print (b),
1187 qof_numeric_print (c), qof_numeric_print (err));
1190 printf (
"sub 100ths/error : %s - %s = %s + (error) %s\n\n",
1191 qof_numeric_print (a), qof_numeric_print (b),
1192 qof_numeric_print (c), qof_numeric_print (err));
1195 printf (
"mul 100ths/error : %s * %s = %s + (error) %s\n\n",
1196 qof_numeric_print (a), qof_numeric_print (b),
1197 qof_numeric_print (c), qof_numeric_print (err));
1200 printf (
"div 100ths/error : %s / %s = %s + (error) %s\n\n",
1201 qof_numeric_print (a), qof_numeric_print (b),
1202 qof_numeric_print (c), qof_numeric_print (err));
1204 printf (
"multiply (EXACT): %s * %s = %s\n",
1205 qof_numeric_print (a), qof_numeric_print (b),
1209 printf (
"multiply (REDUCE): %s * %s = %s\n",
1210 qof_numeric_print (a), qof_numeric_print (b),