File: Real.cc

package info (click to toggle)
eclipse-titan 6.5.0-1
  • links: PTS
  • area: main
  • in suites: buster
  • size: 101,128 kB
  • sloc: cpp: 259,139; ansic: 47,560; yacc: 22,554; makefile: 14,074; sh: 12,630; lex: 9,101; xml: 5,362; java: 4,849; perl: 3,784; awk: 48; php: 32; python: 13
file content (102 lines) | stat: -rw-r--r-- 3,109 bytes parent folder | download
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/******************************************************************************
 * Copyright (c) 2000-2018 Ericsson Telecom AB
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html
 *
 * Contributors:
 *   Balasko, Jeno
 *   Bibo, Zoltan
 *   Delic, Adam
 *   Forstner, Matyas
 *   Gecse, Roland
 *   Raduly, Csaba
 *   Szabo, Janos Zoltan – initial implementation
 *   Tatarka, Gabor
 *
 ******************************************************************************/
#include "Real.hh"
#include "string.hh"
#include "error.h"
#include "Setting.hh"

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <cmath>

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