Sitzung: Jeden Freitag in der Vorlesungszeit ab 16 Uhr c. t. im MAR 0.005. In der vorlesungsfreien Zeit unregelmäßig (Jemensch da?). Macht mit!

C-Kurs/ieee754/Musterlösung: Unterschied zwischen den Versionen

(pdf -> wiki)
 
 
(2 dazwischenliegende Versionen von einem anderen Benutzer werden nicht angezeigt)
Zeile 10: Zeile 10:
 
#include <stdio.h>
 
#include <stdio.h>
 
#include <stdlib.h>
 
#include <stdlib.h>
 +
 +
typedef union _uint_float_t {
 +
        unsigned int u;
 +
        float f;
 +
} uint_float_t;
  
 
static void dump_header() {
 
static void dump_header() {
Zeile 20: Zeile 25:
 
   putchar('\n');
 
   putchar('\n');
 
}
 
}
 +
 +
 +
static void dump_bits(float f) {
 +
        uint_float_t store;
 +
        store.f = f;
 +
        printf("Data  ");
 +
        signed int bit_index = sizeof(int) * 8 - 1;
 +
        for (; bit_index >= 0; bit_index--) {
 +
                const unsigned int mask = (unsigned int)1 << (bit_index);
 +
                putchar((store.u & mask) ? '1' : '0');
 +
        }
 +
        printf("  %7.3ff\n", f);
 +
}
 +
  
 
int main(int argc, char ** argv) {
 
int main(int argc, char ** argv) {
Zeile 25: Zeile 44:
  
 
         dump_header();
 
         dump_header();
for (int i= 1; i<argc; i++){
+
        for (int i= 1; i<argc; i++){
  argv++;
+
          argv++;
  dump_bits(strtof(*argv, 0));
+
          dump_bits(strtof(*argv, 0));
}
+
        }
 
         dump_header();
 
         dump_header();
  

Aktuelle Version vom 5. März 2013, 17:37 Uhr

/*
** Copyright (C) Sebastian Pipping <sebastian@pipping.org> and Katrin Lang <mail@katrinlang.de>
** Licensed under GPL v3 or later
**
** 2011-08-13 20:06 UTC+2
*/

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>

typedef union _uint_float_t {
        unsigned int u;
        float f;
} uint_float_t;

static void dump_header() {
  printf("Index  31     24      16      8       0\n");
  printf("       |");
  signed int bit_index = sizeof(int) * 8 - 2;
  for (; bit_index >= 0; bit_index--) {
    putchar(bit_index % 8 ? '.' : '|');
  }
  putchar('\n');
}


static void dump_bits(float f) {
        uint_float_t store;
        store.f = f;
        printf("Data   ");
        signed int bit_index = sizeof(int) * 8 - 1;
        for (; bit_index >= 0; bit_index--) {
                const unsigned int mask = (unsigned int)1 << (bit_index);
                putchar((store.u & mask) ? '1' : '0');
        }
        printf("  %7.3ff\n", f);
}


int main(int argc, char ** argv) {
        assert(sizeof(unsigned int) == sizeof(float));

        dump_header();
        for (int i= 1; i<argc; i++){
          argv++;
          dump_bits(strtof(*argv, 0));
        }
        dump_header();

        return 0;
}