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
13 ******************************************************************************/
14 #ifndef TTCN3FLOAT_HH_
15 #define TTCN3FLOAT_HH_
19 /* TTCN-3 float values that have absolute value smaller than this
20 are displayed in exponential notation. */
21 #define MIN_DECIMAL_FLOAT 1.0E-4
22 /* TTCN-3 float values that have absolute value larger or equal than this
23 are displayed in exponential notation. */
24 #define MAX_DECIMAL_FLOAT 1.0E+10
28 // Thankfully, IEEE Std 1003.1, 2004 Edition says that signbit is a macro,
29 // hence it's safe to use ifdef.
34 inline int signbitfunc(double d)
36 return *((unsigned char*)&d) & 0x80;
40 // Probably Intel, assume little endian
41 inline int signbitfunc(double d)
43 return ((unsigned char*)&d)[sizeof(double)-1] & 0x80;
48 #define signbit(d) signbitfunc(d)
52 /** A class which behaves almost, but not quite, entirely unlike
53 * a floating-point value.
55 * It is used as a member of a union (in Value.hh);
56 * it MUST NOT have a constructor.
59 /// Implicit conversion
60 operator double() const { return value; }
62 /// Assignment from a proper double
63 const ttcn3float& operator=(double d) {
68 /// Address-of, for scanf
69 double* operator&() { return &value; }
71 const ttcn3float& operator+=(double d) {
76 const ttcn3float& operator-=(double d) {
81 const ttcn3float& operator*=(double d) {
86 const ttcn3float& operator/=(double d) {
91 bool operator<(double d) const {
93 return false; // TTCN-3 special: NaN is bigger than anything except NaN
96 return true; // TTCN-3 special: NaN is bigger than anything except NaN
98 else if (value==0.0 && d==0.0) { // does not distinguish -0.0
99 return signbit(value) && !signbit(d); // value negative, d positive
101 else { // finally, the sensible behavior
106 bool operator>(double d) const {
108 return true; // TTCN-3 special: NaN is bigger than anything except NaN
111 return false; // TTCN-3 special: NaN is bigger than anything except NaN
113 else if (value==0.0 && d==0.0) { // does not distinguish -0.0
114 return !signbit(value) && signbit(d); // value positive, d negative
116 else { // finally, the sensible behavior
121 bool operator==(double d) const {
123 return !!isnan(d); // TTCN-3 special: NaN is bigger than anything except NaN
128 else if (value==0.0 && d==0.0) { // does not distinguish -0.0
129 return signbit(value) == signbit(d);
131 else { // finally, the sensible behavior
139 /** Replacement for a user-defined constructor that ttcn3float can't have */
140 inline ttcn3float make_ttcn3float(double f) {
141 ttcn3float retval = { f };
145 #endif /* TTCN3FLOAT_HH_ */