Commit | Line | Data |
---|---|---|
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 | * Baranyi, Botond | |
11 | * Raduly, Csaba | |
12 | * | |
13 | ******************************************************************************/ | |
970ed795 EL |
14 | #ifndef TTCN3FLOAT_HH_ |
15 | #define TTCN3FLOAT_HH_ | |
16 | ||
17 | #include <math.h> | |
18 | ||
3abe9331 | 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 | |
25 | ||
970ed795 EL |
26 | #ifndef signbit |
27 | // Probably Solaris. | |
28 | // Thankfully, IEEE Std 1003.1, 2004 Edition says that signbit is a macro, | |
29 | // hence it's safe to use ifdef. | |
30 | ||
31 | #ifdef __sparc | |
32 | // Big endian | |
33 | ||
34 | inline int signbitfunc(double d) | |
35 | { | |
36 | return *((unsigned char*)&d) & 0x80; | |
37 | } | |
38 | ||
39 | #else | |
40 | // Probably Intel, assume little endian | |
41 | inline int signbitfunc(double d) | |
42 | { | |
43 | return ((unsigned char*)&d)[sizeof(double)-1] & 0x80; | |
44 | } | |
45 | ||
46 | #endif | |
47 | ||
48 | #define signbit(d) signbitfunc(d) | |
49 | ||
50 | #endif // def signbit | |
51 | ||
52 | /** A class which behaves almost, but not quite, entirely unlike | |
53 | * a floating-point value. | |
54 | * | |
55 | * It is used as a member of a union (in Value.hh); | |
56 | * it MUST NOT have a constructor. | |
57 | */ | |
58 | struct ttcn3float { | |
59 | /// Implicit conversion | |
60 | operator double() const { return value; } | |
61 | ||
62 | /// Assignment from a proper double | |
63 | const ttcn3float& operator=(double d) { | |
64 | value = d; | |
65 | return *this; | |
66 | } | |
67 | ||
68 | /// Address-of, for scanf | |
69 | double* operator&() { return &value; } | |
70 | ||
71 | const ttcn3float& operator+=(double d) { | |
72 | value += d; | |
73 | return *this; | |
74 | } | |
75 | ||
76 | const ttcn3float& operator-=(double d) { | |
77 | value -= d; | |
78 | return *this; | |
79 | } | |
80 | ||
81 | const ttcn3float& operator*=(double d) { | |
82 | value *= d; | |
83 | return *this; | |
84 | } | |
85 | ||
86 | const ttcn3float& operator/=(double d) { | |
87 | value /= d; | |
88 | return *this; | |
89 | } | |
90 | ||
91 | bool operator<(double d) const { | |
92 | if (isnan(value)) { | |
93 | return false; // TTCN-3 special: NaN is bigger than anything except NaN | |
94 | } | |
95 | else if (isnan(d)) { | |
96 | return true; // TTCN-3 special: NaN is bigger than anything except NaN | |
97 | } | |
98 | else if (value==0.0 && d==0.0) { // does not distinguish -0.0 | |
99 | return signbit(value) && !signbit(d); // value negative, d positive | |
100 | } | |
101 | else { // finally, the sensible behavior | |
102 | return value < d; | |
103 | } | |
104 | } | |
105 | ||
106 | bool operator>(double d) const { | |
107 | if (isnan(value)) { | |
108 | return true; // TTCN-3 special: NaN is bigger than anything except NaN | |
109 | } | |
110 | else if (isnan(d)) { | |
111 | return false; // TTCN-3 special: NaN is bigger than anything except NaN | |
112 | } | |
113 | else if (value==0.0 && d==0.0) { // does not distinguish -0.0 | |
114 | return !signbit(value) && signbit(d); // value positive, d negative | |
115 | } | |
116 | else { // finally, the sensible behavior | |
117 | return value > d; | |
118 | } | |
119 | } | |
120 | ||
121 | bool operator==(double d) const { | |
122 | if (isnan(value)) { | |
123 | return !!isnan(d); // TTCN-3 special: NaN is bigger than anything except NaN | |
124 | } | |
125 | else if (isnan(d)) { | |
126 | return false; | |
127 | } | |
128 | else if (value==0.0 && d==0.0) { // does not distinguish -0.0 | |
129 | return signbit(value) == signbit(d); | |
130 | } | |
131 | else { // finally, the sensible behavior | |
132 | return value == d; | |
133 | } | |
134 | } | |
135 | public: | |
136 | double value; | |
137 | }; | |
138 | ||
139 | /** Replacement for a user-defined constructor that ttcn3float can't have */ | |
140 | inline ttcn3float make_ttcn3float(double f) { | |
141 | ttcn3float retval = { f }; | |
142 | return retval; | |
143 | } | |
144 | ||
145 | #endif /* TTCN3FLOAT_HH_ */ |