Close

Code: Exponential notation

A project log for reprint: modern printf

reprint is printf redone with decades of hindsight, revamping the semantics and syntax to make a function worthy of Hello World.

analog-twoAnalog Two 04/12/2016 at 16:000 Comments

Exponential notation (aka 'Scientific' notation) represents a numeric value as mantissa and exponent. The mantissa is value in the interval [1, 10) in decimal notation. This notation is convenient for numbers with many digits (really really small fractions or larger numbers). printf only prints floats or doubles in exponential notation, as

/* Would print 4.200000e01 */
float x = 42.0;
printf("%e", x);

In reprint, exponential notation is not bound to the type of input; it is simply a method of output. Both integers and floating point values can be represented in exponential notation:

/* Print 4.200000e1 */
float x = 42.0;
reprint("\f.fr", x);

/* Print 4.200000e1 */
int y = 42;
reprintf("\f.r", y);

Printing integers in exponential notation may seem silly, but it's perfectly valid from a mathematical standpoint. If you are dealing with very large noisy counters, then exponential notation could neaten their appearance.

Discussions