diff --git a/src/bytedouble.c b/src/bytedouble.c new file mode 100644 index 0000000000000000000000000000000000000000..35b9f65d7615848d9d39aa6cb030be257da190f6 --- /dev/null +++ b/src/bytedouble.c @@ -0,0 +1,32 @@ +/* + * SPDX-FileCopyrightText: 2020 Bruce Cowan <bruce@bcowan.me.uk> + * SPDX-License-Identifier: Apache-2.0 + */ +#include <inttypes.h> +#include <math.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> + +#define DOUBLE_TO_BYTES(d) ((uint8_t *) &(d)) +#define BYTES_TO_DOUBLE(b) (*((double *) (b))) + +int +main (void) +{ + double pi = M_PI; + + uint8_t *bytes = DOUBLE_TO_BYTES (pi); + + printf ("Bytes are ["); + + for (int i = 0; i < 7; i++) + printf ("%02" PRIx8 ", ", bytes[i]); + + printf ("%02" PRIx8 "]\n", bytes[7]); + + double d = BYTES_TO_DOUBLE (bytes); + printf ("Converting back, the value is %f\n", d); + + return 0; +} diff --git a/src/meson.build b/src/meson.build index 10713c14ab8e6674a3ee68c4e1dab7be0546a0c3..2682858a112beaf9a22d5d29bf919ff5fb1d5d58 100644 --- a/src/meson.build +++ b/src/meson.build @@ -1,5 +1,4 @@ -# SPDX-FileCopyrightText: 2018, 2019 Bruce Cowan <bruce@bcowan.me.uk> -# +# SPDX-FileCopyrightText: 2018-2020 Bruce Cowan <bruce@bcowan.me.uk> # SPDX-License-Identifier: CC0-1.0 executable('add', 'add.c', dependencies: utils_dep) @@ -7,6 +6,7 @@ executable('angle', 'angle.c', dependencies: libm) executable('array-length', 'array-length.c', dependencies: types_dep) executable('binary', 'binary.c') executable('binary2', 'binary2.c', dependencies: utils_dep) +executable('bytedouble', 'bytedouble.c') executable('complex', 'complex.c', dependencies: libm) executable('doubles', 'doubles.c') executable('einstein', 'einstein.c')