Ian Jauslin
summaryrefslogtreecommitdiff
blob: d1fad7e514f15d540dfa00a978a900182d9b96c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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);
}