Instead of null pass down the formal parameter list of the type.
[deliverable/titan.core.git] / compiler2 / Real.cc
CommitLineData
d44e3c4f 1/******************************************************************************
2 * Copyright (c) 2000-2016 Ericsson Telecom AB
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * Balasko, Jeno
10 * Bibo, Zoltan
11 * Delic, Adam
12 * Forstner, Matyas
13 * Gecse, Roland
14 * Raduly, Csaba
15 * Szabo, Janos Zoltan – initial implementation
16 * Tatarka, Gabor
17 *
18 ******************************************************************************/
970ed795
EL
19#include "Real.hh"
20#include "string.hh"
21#include "error.h"
22#include "Setting.hh"
23
24#include <stdio.h>
25#include <stdlib.h>
26#include <errno.h>
27#include <string.h>
28
29namespace Common {
30
31 bool isSpecialFloatValue(const Real& r)
32 {
33 return ((r!=r) || (r==REAL_INFINITY) || (r==-REAL_INFINITY));
34 }
35
36 string Real2string(const Real& r)
37 {
38 if (r == 0.0) return ((1.0/r)==-REAL_INFINITY) ? string("-0.0e0") : string("0.0e0");
39 else if (r == REAL_INFINITY) return string("INF");
40 else if (r == -REAL_INFINITY) return string("-INF");
41 else if (r != r) return string("NaN");
42
43 Real rabs = fabs(r);
44 int sign = (r / rabs < 0) ? -1 : 1;
45 double exponent = floor(log10(rabs));
46 double fraction = rabs * pow(10.0, -exponent);
47 double integral = floor(fraction);
48 char tmp[64];
49
50 sprintf(tmp, "%s%.15g%se%d",
51 (sign == -1) ? "-" : "",
52 fraction,
53 (fraction == integral) ? ".0" : "",
54 (int)exponent);
55
56 return string(tmp);
57 }
58
59 string Real2code(const Real& r)
60 {
61 if (r == 0.0) return ((1.0/r)==-REAL_INFINITY) ? string("-0.0") : string("0.0");
62 else if (r == REAL_INFINITY) return string("PLUS_INFINITY");
63 else if (r == -REAL_INFINITY) return string("MINUS_INFINITY");
64 else if (r != r) return string("NOT_A_NUMBER");
65 else {
66 Real rabs = fabs(r);
67 Real exponent = floor(log10(rabs));
68 Real mantissa = rabs * pow(10.0, -exponent);
69
70 char *tmp = mprintf("%s%.15g", r < 0.0 ? "-" : "", mantissa);
71 if (floor(mantissa) == mantissa) tmp = mputstr(tmp, ".0");
72 if (exponent != 0.0) tmp = mputprintf(tmp, "e%d", (int)exponent);
73 string ret_val(tmp);
74 Free(tmp);
75 return ret_val;
76 }
77 }
78
79 Real string2Real(const char *s, const Location& loc)
80 {
81 if (s[0] == '\0' || !strcmp(s, "0.0e0")) return 0.0;
82 else if (!strcmp(s, "-INF")) return -REAL_INFINITY;
83 else if (!strcmp(s, "INF")) return REAL_INFINITY;
84 else if (!strcmp(s, "NaN")) return REAL_NAN;
85 errno = 0;
86 Real r = strtod(s, NULL);
87 switch (errno) {
88 case ERANGE:
89 loc.error("Overflow when converting `%s' to a floating point value: %s",
90 s, strerror(errno));
91 break;
92 case 0:
93 break;
94 default:
95 FATAL_ERROR("string2Real(): Unexpected error when converting `%s' to real: %s",
96 s, strerror(errno));
97 }
98 return r;
99 }
100
101} // namespace Common
This page took 0.027109 seconds and 5 git commands to generate.