Outcurses: Difference between revisions
| Line 8: | Line 8: | ||
==Metric prefixes== | ==Metric prefixes== | ||
Not ncurses-related, but it's UI, so I stuck it here. Fed a <tt>uintmax_t</tt> (large enough to represent any unsigned integer type), a buffer, and a base (almost always 1000 or 1024), <tt>enmetric</tt> renders the number into the buffer such that the characteristic (material to the left of the decimal point) is less than the base. For numbers greater than the base, this means some metric suffix will be employed, and that the displayed number will have a mantissa (e.g. 1234 with a base of 1000 becomes 1.234K). Numbers through 2^89 are properly handled, using the suffixes Y, Z, E, P, T, G, M, and K. Use of base 1024 is necessary to properly employ the [https://en.wikipedia.org/wiki/Kibibyte metric units of digital information]. For instance, a "10 terabyte" hard drive typically has 10^13 == 10,000,000,000,000 (ten trillion) bytes, but 10 * 2^40 == 10 * 1,024 * 1,024 * 1,024 is 10,995,116,277,760 bytes aka 10 tibibytes. A kilobyte is 97.7% of a kibibyte, but a terabyte is only 91% of a tibibyte. | Not ncurses-related, but it's UI, so I stuck it here. Fed a <tt>uintmax_t</tt> (large enough to represent any unsigned integer type), a buffer, and a base (almost always 1000 or 1024), <tt>enmetric</tt> renders the number into the buffer such that the characteristic (material to the left of the decimal point) is less than the base. For numbers greater than the base, this means some metric suffix will be employed, and that the displayed number will have a mantissa (e.g. 1234 with a base of 1000 becomes 1.234K). Numbers through 2^89 are properly handled, using the suffixes Y, Z, E, P, T, G, M, and K. Use of base 1024 is necessary to properly employ the [https://en.wikipedia.org/wiki/Kibibyte metric units of digital information]. For instance, a "10 terabyte" hard drive typically has 10^13 == 10,000,000,000,000 (ten trillion) bytes, but 10 * 2^40 == 10 * 1,024 * 1,024 * 1,024 is 10,995,116,277,760 bytes aka 10 tibibytes. A kilobyte is 97.7% of a kibibyte, but a terabyte is only 91% of a tibibyte. | ||
Three characters of mantissa are used for characteristics less than 10; two characters are used for characteristics greater than 9 but less than 100; one character is used for characteristics greater than 99. Note that the most precision is available for the smallest characteristics, which is desirable, since the mantissa represents a greater portion of the total in these cases. | |||
The necessary output buffer is a fixed size for a given base. For 1000, at most 7 <tt>chars</tt> are necessary: | The necessary output buffer is a fixed size for a given base. For 1000, at most 7 <tt>chars</tt> are necessary: | ||
| Line 18: | Line 20: | ||
* 1 for the NUL terminator | * 1 for the NUL terminator | ||
An optional universal suffix can be supplied to <tt>enmetric</tt>; this is printed following the metric suffix. This is intended to support 'i' when the base is 1024. In this maximal case, 9 <tt>chars</tt> are necessary. Less space than this can always be used; the output will be left-padded with spaces. The minimal output is two bytes (a single digit and a NUL). | An optional universal suffix can be supplied to <tt>enmetric</tt>; this is printed following the metric suffix. This is intended to support 'i' when the base is 1024. In this maximal case (base > 1000, universal suffix), 9 <tt>chars</tt> are necessary. Less space than this can always be used; the output will be left-padded with spaces. The minimal output is two bytes (a single digit and a NUL). | ||
If the <tt>omitdec</tt> boolean is true, no decimal point or mantissa will be printed when said mantissa would be all zeroes. | If the <tt>omitdec</tt> boolean is true, no decimal point or mantissa will be printed when said mantissa would be all zeroes. | ||