/*
 * Copyright(C) Paul und Scherer (mct.de/mct.net)
 *
 * This example demonstrates how to...
 *
 *  ... print floating-point numbers.
 *
 *  ... use the math lib functions.
 */

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

/*
 * Print floating-point number rounded to
 * the 9th digit after the decimal point.
 */
static void
printd(double d)
{
	double i;				// integral part
	long f = rint(modf(fabs(d), &i)*1e9);	// fixpoint part

	if (f >= 1e9) f -= 1e9, i++;		// correct rounding
	printf("%s%ld.%09ld",
		d < 0? "-": "",			// sign
		(long)i,			// digits before decimal point
		f				//        after  decimal point
	);
}

/*
 * For each x entered print x and f(x).
 *
 * Note: The program expects valid arguments
 * for the function. No error checking done!
 */

#define f(x)	sqrt(x)				// choose f(x)

int
main(void)
{
	char buf[100];				// input buffer
	double x;				// argument

	while (1) {
		fputs("\nEnter x... ", stdout);
		x = atof(fgets(buf, sizeof(buf), stdin));
		fputs("x = ", stdout);
		printd(x);			// print x
		fputs(", f(x) = ", stdout);
		printd(f(x));			// print f(x)
	}
}
