22.5. A simple command using floating point maths

Programs which require access to the ISO 9899:1999 math functions must link the math library using the -lm option to the linker.

Example 22.3. Command using math functions

This is a simple program which performs operations on floating point numbers.

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/types.h>
#include <unistd.h>
#include <math.h>

int 
main(int argc, char **argv)
{
    int loop;    
    double test_a;
    double test;
    double test_power;
    double test_sqrt;

    test_a = 2.01;
    test = 1.01;
    
    for (loop = 0; loop < 10; loop++) {
	test = test + 0.5;
        test_power = test_power + pow(test_a, test);
        test_sqrt = test_sqrt + sqrt(test_power);
    }

    printf("Should be 2.01   : %g\n", test_a);
    printf("Should be 6.01   : %g\n", test);
    printf("Should be 218.5  : %g\n", test_power);
    printf("Should be 71.027 : %g\n", test_sqrt);
          
    return 0;
}

This is may be compiled directly as: /opt/simtec/able/bin/arm-able-gcc -lm -o floattset floattest.c

The floattest program may now be copied to a location accessible by ABLE (http or tftp server) and executed. The output should be:

>hello
Should be 2.01   : 2.01
Should be 6.01   : 6.01
Should be 218.5  : 218.5
Should be 71.027 : 71.027
>