The Teeniest of Tiniest

It took me a while to figure out the significance of the nextafter() function. Though I understand what it does now, I still can’t figure out how it’s useful, though I assume it has a valid purpose otherwise it wouldn’t be in the standard library.

The nextafter() function returns the next valid real number after a given value, up or down. It’s man page format is:

double nextafter(double x, double y);

Argument x is a floating point value, y is a direction, greater than or less than value x. The value returned is the next valid real number before or after argument x.

The nextafter() function is prototyped in the math.h header file. Its inclusion in your code may also require you to link in the math library.

Here’s my test program:

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

int main()
{
    double max,min,f;
   
    f = 0.0;
    max = nextafter(f,1.0);
    min = nextafter(f,-1.0);
    printf("%e max is %e\n",f,max);
    printf("%e min is %e\n",f,min);

    return(0);
}

Variable f is set to zero. Variable max is assigned the next valid real number above zero and variable min is assigned the next value below. I use the %e placeholder for the output because the results are so small; if %f is used instead, output would be 0.000000.

Here’s the output:

0.000000e+00 max is 4.940656e-324
0.000000e+00 min is -4.940656e-324

That’s a teensy, tiny number, which my mathematically-challenged mind questions as to why or how it’s even useful. Then again, I’m just documenting what I’ve found. I’m sure some chemist or astrophysicist would be orgasmic upon seeing a value like 4.940656e-324.

Just to be curious. I changed the assignment of variable f to 100.0 at Line 8. Here’s the updated output:

1.000000e+02 max is 1.000000e+02
1.000000e+02 min is 1.000000e+02

So either the “next after” floating point value is so small it doesn’t register or it just doesn’t exist. Not being a propellerhead, I don’t know the difference. Further, I’m really at a loss as to how this function could prove useful. Perhaps someone at the Jet Propulsion Laboratory can help out?

My goal wasn’t to poke fun at the nextafter() function, though I do enjoy poking fun at math. It’s enjoyable to play with real numbers and my curiosity with the function was to see how small such values really get. But neither the documentation nor my understanding extracts any useful information beyond the trivial tidbits.

2 thoughts on “The Teeniest of Tiniest

  1. The reason the two values are the same with f = 100 is that both calls to nextafter have the second argument lower than f, so they both give the highest representable number lower than 100.

    If you do this:

    f = 100.0;

    max = nextafter(f, 101.0);
    min = nextafter(f, 99.0);

    printf(“%e max is\n%e\n%.64lf\n\n”, f, max, max);
    printf(“%e min is\n%e\n%.64lf\n”, f, min, min);

    you get this:

    1.000000e+02 max is
    1.000000e+02
    100.0000000000000142108547152020037174224853515625000000000000000000

    1.000000e+02 min is
    1.000000e+02
    99.9999999999999857891452847979962825775146484375000000000000000000

    The two outputs with %e are too small for the differences to show up but with %.64lf you can see numbers a tiny bit higher and lower than 100.

    In some languages or environments you can retrieve a value called Epsilon which is the smallest representable value, although with C it seems you have to get it indirectly using nextafter.

    I think it is used to compare real numbers for equality while allowing for floating-point formats like IEEE 754 not being able to store all decimals with exact precision. Something like that anyway.

Leave a Reply