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
15 * Szabo, Janos Zoltan – initial implementation
18 ******************************************************************************/
31 bool isSpecialFloatValue(const Real
& r
)
33 return ((r
!=r
) || (r
==REAL_INFINITY
) || (r
==-REAL_INFINITY
));
36 string
Real2string(const Real
& r
)
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");
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
);
50 sprintf(tmp
, "%s%.15g%se%d",
51 (sign
== -1) ? "-" : "",
53 (fraction
== integral
) ? ".0" : "",
59 string
Real2code(const Real
& r
)
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");
67 Real exponent
= floor(log10(rabs
));
68 Real mantissa
= rabs
* pow(10.0, -exponent
);
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
);
79 Real
string2Real(const char *s
, const Location
& loc
)
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
;
86 Real r
= strtod(s
, NULL
);
89 loc
.error("Overflow when converting `%s' to a floating point value: %s",
95 FATAL_ERROR("string2Real(): Unexpected error when converting `%s' to real: %s",
101 } // namespace Common
This page took 0.03446 seconds and 5 git commands to generate.