Search notes:

IEEE-754 / nextafter

The C function nextafter determines the next higher or lower representable double from a given double.
The following C programm calls nextafter() several times for a few select values and uses represent-ieee-754.c to show how the internal bits of the double change:
#include <stdio.h>
#include <math.h>

#include "represent-ieee-754.h"

int main() {

   ieee_754_double_representation r;

   double d = 0; represent_ieee_754_double(d, r);
   
   printf("\nWe start with the representation of the double 0:\n");
   printf("    %s\n", r);

   d=nextafter(d, INFINITY); represent_ieee_754_double(d, r);
   printf("\nThe next higher representable double just increases the value of the");
   printf("\nmantissa by one:\n");
   printf("    %s\n", r);

   d=nextafter(d, INFINITY); represent_ieee_754_double(d, r);
   printf("\nEach call to nextafter() increases the value of the mantissa by one:\n");
   printf("    %s\n", r);

   d=nextafter(d, INFINITY); represent_ieee_754_double(d, r);
   printf("\nAgain...:\n");
   printf("    %s\n", r);

   d=nextafter(1.0/pow(2, 1022), -INFINITY);
   printf("\nWe can continue to increase the mantissa until all bits of the");
   printf("\nmantissa are set:\n");
   represent_ieee_754_double(d, r); printf("    %s\n", r);

   d=nextafter(d, INFINITY); represent_ieee_754_double(d, r);
   printf("\nNow, increasing the double to the next representable value clears");
   printf("\nall mantiassa bits and increases the exponent value by one:\n");
   printf("    %s\n", r);

   d=nextafter(d, INFINITY); represent_ieee_754_double(d, r);
   printf("\nThe increasing of the mantissa value starts over:\n");
   printf("    %s\n", r);

   d=nextafter(1.0/pow(2, 1021), -INFINITY); represent_ieee_754_double(d, r);
   printf("\nUntil all mantissa bits are set again:\n");
   printf("    %s\n", r);

   d=nextafter(d, INFINITY); represent_ieee_754_double(d, r);
   printf("\nAfter which the mantissa value is reset to 0 and the");
   printf("\nexponent value is increased by one:\n");
   printf("    %s\n", r);

}
Github repository about-IEEE-754, path: /nextafter.c
When run, the program prints
We start with the representation of the double 0:
    .... .... | .... .... | .... .... | .... .... | .... .... | .... .... | .... .... | .... .... | 

The next higher representable double just increases the value of the
mantissa by one:
    .... .... | .... .... | .... .... | .... .... | .... .... | .... .... | .... .... | .... ...M | 

Each call to nextafter() increases the value of the mantissa by one:
    .... .... | .... .... | .... .... | .... .... | .... .... | .... .... | .... .... | .... ..M. | 

Again...:
    .... .... | .... .... | .... .... | .... .... | .... .... | .... .... | .... .... | .... ..MM | 

We can continue to increase the mantissa until all bits of the
mantissa are set:
    .... .... | .... MMMM | MMMM MMMM | MMMM MMMM | MMMM MMMM | MMMM MMMM | MMMM MMMM | MMMM MMMM | 

Now, increasing the double to the next representable value clears
all mantiassa bits and increases the exponent value by one:
    .... .... | ...E .... | .... .... | .... .... | .... .... | .... .... | .... .... | .... .... | 

The increasing of the mantissa value starts over:
    .... .... | ...E .... | .... .... | .... .... | .... .... | .... .... | .... .... | .... ...M | 

Until all mantissa bits are set again:
    .... .... | ...E MMMM | MMMM MMMM | MMMM MMMM | MMMM MMMM | MMMM MMMM | MMMM MMMM | MMMM MMMM | 

After which the mantissa value is reset to 0 and the
exponent value is increased by one:
    .... .... | ..E. .... | .... .... | .... .... | .... .... | .... .... | .... .... | .... .... | 

Index