Real.cc 3.04 KB
Newer Older
Elemer Lelik's avatar
Elemer Lelik committed
1
/******************************************************************************
2
 * Copyright (c) 2000-2019 Ericsson Telecom AB
Elemer Lelik's avatar
Elemer Lelik committed
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v2.0
Elemer Lelik's avatar
Elemer Lelik committed
5
 * which accompanies this distribution, and is available at
6
 * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html
Elemer Lelik's avatar
Elemer Lelik committed
7
8
9
10
11
12
13
14
15
16
17
18
 *
 * Contributors:
 *   Balasko, Jeno
 *   Bibo, Zoltan
 *   Delic, Adam
 *   Forstner, Matyas
 *   Gecse, Roland
 *   Raduly, Csaba
 *   Szabo, Janos Zoltan – initial implementation
 *   Tatarka, Gabor
 *
 ******************************************************************************/
Elemer Lelik's avatar
Elemer Lelik committed
19
20
21
22
23
24
25
26
27
#include "Real.hh"
#include "string.hh"
#include "error.h"
#include "Setting.hh"

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
28
#include <cmath>
Elemer Lelik's avatar
Elemer Lelik committed
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102

namespace Common {

  bool isSpecialFloatValue(const Real& r)
  {
    return ((r!=r) || (r==REAL_INFINITY) || (r==-REAL_INFINITY));
  }

  string Real2string(const Real& r)
  {
    if (r == 0.0) return ((1.0/r)==-REAL_INFINITY) ? string("-0.0e0") : string("0.0e0");
    else if (r == REAL_INFINITY) return string("INF");
    else if (r == -REAL_INFINITY) return string("-INF");
    else if (r != r) return string("NaN");

    Real   rabs     = fabs(r);
    int    sign     = (r / rabs < 0) ? -1 : 1;
    double exponent = floor(log10(rabs));
    double fraction = rabs * pow(10.0, -exponent);
    double integral = floor(fraction);
    char   tmp[64];

    sprintf(tmp, "%s%.15g%se%d",
            (sign == -1) ? "-" : "",
            fraction,
            (fraction == integral) ? ".0" : "",
            (int)exponent);

    return string(tmp);
  }

  string Real2code(const Real& r)
  {
    if (r == 0.0) return ((1.0/r)==-REAL_INFINITY) ? string("-0.0") : string("0.0");
    else if (r == REAL_INFINITY) return string("PLUS_INFINITY");
    else if (r == -REAL_INFINITY) return string("MINUS_INFINITY");
    else if (r != r) return string("NOT_A_NUMBER");
    else {
      Real rabs = fabs(r);
      Real exponent = floor(log10(rabs));
      Real mantissa = rabs * pow(10.0, -exponent);

      char *tmp = mprintf("%s%.15g", r < 0.0 ? "-" : "", mantissa);
      if (floor(mantissa) == mantissa) tmp = mputstr(tmp, ".0");
      if (exponent != 0.0) tmp = mputprintf(tmp, "e%d", (int)exponent);
      string ret_val(tmp);
      Free(tmp);
      return ret_val;
    }
  }

  Real string2Real(const char *s, const Location& loc)
  {
    if (s[0] == '\0' || !strcmp(s, "0.0e0")) return 0.0;
    else if (!strcmp(s, "-INF")) return -REAL_INFINITY;
    else if (!strcmp(s, "INF")) return REAL_INFINITY;
    else if (!strcmp(s, "NaN")) return REAL_NAN;
    errno = 0;
    Real r = strtod(s, NULL);
    switch (errno) {
    case ERANGE:
      loc.error("Overflow when converting `%s' to a floating point value: %s",
        s, strerror(errno));
      break;
    case 0:
      break;
    default:
      FATAL_ERROR("string2Real(): Unexpected error when converting `%s' to real: %s",
        s, strerror(errno));
    }
    return r;
  }

} // namespace Common