Ian Jauslin
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils.c')
-rw-r--r--src/utils.c79
1 files changed, 79 insertions, 0 deletions
diff --git a/src/utils.c b/src/utils.c
new file mode 100644
index 0000000..d1fad7e
--- /dev/null
+++ b/src/utils.c
@@ -0,0 +1,79 @@
+/*
+Copyright 2016 Ian Jauslin
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+#include "utils.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+// define MPFR_USE_FILE to enable the use of mpfr_printf
+#define MPFR_USE_FILE
+#include <mpfr.h>
+#include <math.h>
+#include "array.h"
+
+// stringify macros
+#define STR(S) XSTR(S)
+#define XSTR(S) #S
+
+// print a double at maximal precision
+int fprint_double(FILE* file, double x){
+ fprintf(file, "% ." STR(__DBL_DIG__) "le",x);
+ return(0);
+}
+
+// print a long double at maximal precision
+int fprint_ldouble(FILE* file, long double x){
+ fprintf(file, "% ." STR(__LDBL_DIG__) "Le",x);
+ return(0);
+}
+
+// log10(2)
+#define LOG2 0.3010299956639812
+// print an mpfr at maximal precision
+int fprint_mpfr(FILE* file, mpfr_t x){
+ // the printf format
+ array_char printf_format;
+ array_char_init(&printf_format,12);
+ array_char_snprintf(&printf_format,"%% .%dRe\0", (int)((mpfr_get_default_prec()-1)*LOG2));
+ mpfr_fprintf(file, printf_format.values, x);
+ array_char_free(printf_format);
+ return(0);
+}
+
+
+// print information about data types
+int print_datatype_info(FILE* file){
+
+ fprintf(file, " int8_t = " STR(__INT8_TYPE__) "\n");
+ fprintf(file, " int16_t = " STR(__INT16_TYPE__) "\n");
+ fprintf(file, " int32_t = " STR(__INT32_TYPE__) "\n");
+ fprintf(file, " int64_t = " STR(__INT64_TYPE__) "\n");
+
+ fprintf(file, "\n");
+
+ fprintf(file, " double: precision: " STR(__DBL_MANT_DIG__) ", emax: " STR(__DBL_MAX_EXP__) ", emin: " STR(__DBL_MIN_EXP__) "\n");
+ fprintf(file, "long double: precision: " STR(__LDBL_MANT_DIG__) ", emax: " STR(__LDBL_MAX_EXP__) ", emin: " STR(__LDBL_MIN_EXP__) "\n");
+
+ fprintf(file, "\n");
+
+ #if _MPFR_PREC_FORMAT == 2
+ fprintf(file, "mpfr precision and emax: int\n");
+ #elif _MPFR_PREC_FORMAT == 3
+ fprintf(file, "mpfr precision and emax: long int\n");
+ #endif
+
+ return(0);
+}