- if (to_block_size <= from_block_size)
- {
- int multiplier = from_block_size / to_block_size;
- amt = n * multiplier;
-
- if (amt / multiplier != n)
- {
- /* Overflow occurred during multiplication. We should use
- multiple precision arithmetic here, but we'll be lazy and
- resort to floating point. This can yield answers that
- are slightly off. In practice it is quite rare to
- overflow uintmax_t, so this is good enough for now. */
-
- double damt = n * (double) multiplier;
-
- if (! base)
- sprintf (buf, "%.0f", damt);
- else
- {
- double e = 1;
- power = 0;
-
- do
- {
- e *= base;
- power++;
- }
- while (e * base <= damt && power < sizeof suffixes - 1);
-
- damt /= e;
-
- sprintf (buf, "%.1f%c", damt, suffixes[power]);
- if (4 < strlen (buf))
- sprintf (buf, "%.0f%c", damt, suffixes[power]);
- }
-
- return buf;
- }
- }
- else if (from_block_size == 0)
- amt = 0;
- else
- {
- int divisor = to_block_size / from_block_size;
- int r10 = (n % divisor) * 10;
- int r2 = (r10 % divisor) * 2;
- amt = n / divisor;
- tenths = r10 / divisor;
- rounding = r2 < divisor ? 0 < r2 : 2 + (divisor < r2);
- }
-
+ {
+ int multiplier;
+ int divisor;
+ int r2;
+ int r10;
+ if (to_block_size <= from_block_size
+ ? (from_block_size % to_block_size != 0
+ || (multiplier = from_block_size / to_block_size,
+ (amt = n * multiplier) / multiplier != n))
+ : (from_block_size == 0
+ || to_block_size % from_block_size != 0
+ || (divisor = to_block_size / from_block_size,
+ r10 = (n % divisor) * 10,
+ r2 = (r10 % divisor) * 2,
+ amt = n / divisor,
+ tenths = r10 / divisor,
+ rounding = r2 < divisor ? 0 < r2 : 2 + (divisor < r2),
+ 0)))
+ {
+ /* Either the result cannot be computed easily using uintmax_t,
+ or from_block_size is zero. Fall back on floating point.
+ FIXME: This can yield answers that are slightly off. */
+
+ double damt = n * (from_block_size / (double) to_block_size);
+
+ if (! base)
+ sprintf (buf, "%.0f", adjust_value (inexact_style, damt));
+ else
+ {
+ char suffix[3];
+ char const *psuffix;
+ double e = 1;
+ power = 0;
+
+ do
+ {
+ e *= base;
+ power++;
+ }
+ while (e * base <= damt && power < sizeof suffixes - 1);
+
+ damt /= e;
+
+ suffix[2] = '\0';
+ psuffix = generate_suffix_backwards (suffix + 2, power, base);
+ sprintf (buf, "%.1f%s",
+ adjust_value (inexact_style, damt), psuffix);
+ if (4 + (base == 1000) < strlen (buf))
+ sprintf (buf, "%.0f%s",
+ adjust_value (inexact_style, damt * 10) / 10, psuffix);
+ }
+
+ return buf;
+ }
+ }