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

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/developm...', 1759324664, '216.73.216.88', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/development/IEEE/754/nextafter(122): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78