YaK:: A lexically-ordered formatting of double on x86 [Changes]   [Calendar]   [Search]   [Index]   [PhotoTags]   
[mega_changes]
[photos]

A lexically-ordered formatting of double on x86

See http://en.wikipedia.org/wiki/Binary64

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

union u { double d; char c[8]; };

main()
{
  int m;
  int s;
  int e;
  for (s = -1; s<=1; s+=2) {
    for (e = -19; e < 19; ++e) {
      for (m = 8; m < 15; ++m) {
        union u x;
        x.d = m * s * exp(e);
        if (x.d >= 0) {
          printf("%20g  %02x-%02x-%02x-%02x--%02x-%02x-%02x-%02x\n",
               x.d,
               255 & x.c[7],
               255 & x.c[6],
               255 & x.c[5],
               255 & x.c[4],
               255 & x.c[3],
               255 & x.c[2],
               255 & x.c[1],
               255 & x.c[0]);
        } else {
          printf("%20g  -%02x-%02x-%02x-%02x--%02x-%02x-%02x-%02x\n",
               x.d,
               255 & (255^x.c[7]),
               255 & (255^x.c[6]),
               255 & (255^x.c[5]),
               255 & (255^x.c[4]),
               255 & (255^x.c[3]),
               255 & (255^x.c[2]),
               255 & (255^x.c[1]),
               255 & (255^x.c[0]));
        }
      }
    }
  }
}

strick@uhuru:~/work$ ./a.out  | LANG=C sort -k2  | less | awk '{if ($1 <= p) { print $1 } ; p = $1 }'
-9.1924e+08
strick@uhuru:~/work$
strick@uhuru:~/work$ ./a.out  | LANG=C sort -k2  | less | head -1
         -9.1924e+08  -3e-34-9a-c2--38-09-e7-5a
strick@uhuru:~/work$

strick@uhuru:~/work$ ./a.out | LANG=C sort -k2 | less | head -22

         -9.1924e+08  -3e-34-9a-c2--38-09-e7-5a
         -8.5358e+08  -3e-36-8f-b4--58-9b-7b-65
         -7.8792e+08  -3e-38-84-a6--79-2d-0f-71
         -7.2226e+08  -3e-3a-79-98--99-be-a3-7d
          -6.566e+08  -3e-3c-6e-8a--ba-50-37-89
         -5.9094e+08  -3e-3e-63-7c--da-e1-cb-95
         -5.2528e+08  -3e-40-b0-dd--f6-e6-bf-42
        -3.38169e+08  -3e-4b-d7-f2--05-73-2f-a5
        -3.14014e+08  -3e-4d-48-85--4e-34-19-f5
        -2.89859e+08  -3e-4e-b9-18--96-f5-04-44
        -2.65704e+08  -3e-50-53-57--bf-6b-dd-29
         -2.4155e+08  -3e-53-34-7e--50-ed-b1-c8
        -2.17395e+08  -3e-56-15-a4--e2-6f-86-67
         -1.9324e+08  -3e-58-f6-cb--73-f1-5b-06
        -1.24406e+08  -3e-62-56-e3--52-d9-ff-c7
        -1.15519e+08  -3e-64-75-40--cc-ee-ff-cb
        -1.06633e+08  -3e-66-93-9e--47-03-ff-cf
        -9.77472e+07  -3e-68-b1-fb--c1-18-ff-d3
        -8.88611e+07  -3e-6a-d0-59--3b-2d-ff-d7
         -7.9975e+07  -3e-6c-ee-b6--b5-42-ff-db
        -7.10889e+07  -3e-6f-0d-14--2f-57-ff-df
        -4.57662e+07  -3e-7a-2d-4c--e6-48-7a-c9

strick@uhuru:~/work$ ./a.out | LANG=C sort -k2 | less | tail -22

         4.57662e+07  41-85-d2-b3--19-b7-85-36
         7.10889e+07  41-90-f2-eb--d0-a8-00-20
          7.9975e+07  41-93-11-49--4a-bd-00-24
         8.88611e+07  41-95-2f-a6--c4-d2-00-28
         9.77472e+07  41-97-4e-04--3e-e7-00-2c
         1.06633e+08  41-99-6c-61--b8-fc-00-30
         1.15519e+08  41-9b-8a-bf--33-11-00-34
         1.24406e+08  41-9d-a9-1c--ad-26-00-38
          1.9324e+08  41-a7-09-34--8c-0e-a4-f9
         2.17395e+08  41-a9-ea-5b--1d-90-79-98
          2.4155e+08  41-ac-cb-81--af-12-4e-37
         2.65704e+08  41-af-ac-a8--40-94-22-d6
         2.89859e+08  41-b1-46-e7--69-0a-fb-bb
         3.14014e+08  41-b2-b7-7a--b1-cb-e6-0a
         3.38169e+08  41-b4-28-0d--fa-8c-d0-5a
          5.2528e+08  41-bf-4f-22--09-19-40-bd
          5.9094e+08  41-c1-9c-83--25-1e-34-6a
           6.566e+08  41-c3-91-75--45-af-c8-76
          7.2226e+08  41-c5-86-67--66-41-5c-82
          7.8792e+08  41-c7-7b-59--86-d2-f0-8e
          8.5358e+08  41-c9-70-4b--a7-64-84-9a
          9.1924e+08  41-cb-65-3d--c7-f6-18-a5

(unless otherwise marked) Copyright 2002-2014 YakPeople. All rights reserved.
(last modified 2011-06-02)       [Login]
(No back references.)