Debian Codegraph @

net/sf/saxon/value/FloatValue.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
6 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7
8 package net.sf.saxon.value;
9
10 import net.sf.saxon.expr.sort.AtomicMatchKey;
11 import net.sf.saxon.expr.sort.AtomicSortComparer;
12 import net.sf.saxon.expr.sort.DoubleSortComparer;
13 import net.sf.saxon.trans.XPathException;
14 import net.sf.saxon.tree.util.FastStringBuffer;
15 import net.sf.saxon.type.AtomicType;
16 import net.sf.saxon.type.BuiltInAtomicType;
17 import net.sf.saxon.type.Converter;
18 import net.sf.saxon.type.ValidationException;
19
20 import java.math.BigDecimal;
21
22 /**
23 * A numeric (single precision floating point) value
24 */
25
26 public final class FloatValue extends NumericValue {
27
28 public static final FloatValue ZERO = new FloatValue((float) 0.0);
29 public static final FloatValue NEGATIVE_ZERO = new FloatValue((float) -0.0);
30 public static final FloatValue ONE = new FloatValue((float) 1.0);
31 public static final FloatValue NaN = new FloatValue(Float.NaN);
32
33 private float value;
34
35 /**
36 * Constructor supplying a float
37 *
38 * @param value the value of the float
39 */
40
41 public FloatValue(float value) {
42 this.value = value;
43 typeLabel = BuiltInAtomicType.FLOAT;
44 }
45
46 /**
47 * Static factory method (for convenience in compiled bytecode)
48 *
49 * @param value the value of the float
50 * @return the FloatValue
51 */
52
53 public static FloatValue makeFloatValue(float value) {
54 return new FloatValue(value);
55 }
56
57 /**
58 * Constructor supplying a float and an AtomicType, for creating
59 * a value that belongs to a user-defined subtype of xs:float. It is
60 * the caller's responsibility to ensure that the supplied value conforms
61 * to the supplied type.
62 *
63 * @param value the value of the NumericValue
64 * @param type the type of the value. This must be a subtype of xs:float, and the
65 * value must conform to this type. The method does not check these conditions.
66 */
67
68 public FloatValue(float value, AtomicType type) {
69 this.value = value;
70 typeLabel = type;
71 }
72
73 /**
74 * Create a copy of this atomic value, with a different type label
75 *
76 * @param typeLabel the type label of the new copy. The caller is responsible for checking that
77 * the value actually conforms to this type.
78 */
79
80 public AtomicValue copyAsSubType(AtomicType typeLabel) {
81 FloatValue v = new FloatValue(value);
82 v.typeLabel = typeLabel;
83 return v;
84 }
85
86 /**
87 * Determine the primitive type of the value. This delivers the same answer as
88 * getItemType().getPrimitiveItemType(). The primitive types are
89 * the 19 primitive types of XML Schema, plus xs:integer, xs:dayTimeDuration and xs:yearMonthDuration,
90 * and xs:untypedAtomic. For external objects, the result is AnyAtomicType.
91 */
92
93 public BuiltInAtomicType getPrimitiveType() {
94 return BuiltInAtomicType.FLOAT;
95 }
96
97 /**
98 * Get the value
99 */
100
101 public float getFloatValue() {
102 return value;
103 }
104
105 public double getDoubleValue() {
106 return (double) value;
107 }
108
109 /**
110 * Get the numeric value converted to a decimal
111 *
112 * @return a decimal representing this numeric value;
113 * @throws ValidationException
114 * if the value cannot be converted, for example if it is NaN or infinite
115 */
116 @Override
117 public BigDecimal getDecimalValue() throws ValidationException {
118 return new BigDecimal((double) value);
119 }
120
121 /**
122 * Return the numeric value as a Java long.
123 *
124 * @return the numeric value as a Java long. This performs truncation
125 * towards zero.
126 * @throws net.sf.saxon.trans.XPathException
127 * if the value cannot be converted
128 */
129 @Override
130 public long longValue() throws XPathException {
131 return (long) value;
132 }
133
134 /**
135 * Get the hashCode. This must conform to the rules for other NumericValue hashcodes
136 *
137 * @see NumericValue#hashCode
138 */
139
140 public int hashCode() {
141 if (value > Integer.MIN_VALUE && value < Integer.MAX_VALUE) {
142 return (int) value;
143 } else {
144 return Double.valueOf(getDoubleValue()).hashCode();
145 }
146 }
147
148 /**
149 * Test whether the value is the double/float value NaN
150 */
151
152 public boolean isNaN() {
153 return Float.isNaN(value);
154 }
155
156 /**
157 * Get the effective boolean value
158 *
159 * @return true unless the value is zero or NaN
160 */
161 public boolean effectiveBooleanValue() {
162 return (value != 0.0 && !Float.isNaN(value));
163 }
164
165
166 /**
167 * Get the value as a String
168 * @return a String representation of the value
169 */
170
171 // public String getStringValue() {
172 // return getStringValueCS().toString();
173 // }
174
175 /**
176 * Get the value as a String
177 *
178 * @return a String representation of the value
179 */
180
181 /*@NotNull*/
182 public CharSequence getPrimitiveStringValue() {
183 return floatToString(value);
184 }
185
186 /**
187 * Get the canonical lexical representation as defined in XML Schema. This is not always the same
188 * as the result of casting to a string according to the XPath rules. For xs:float, the canonical
189 * representation always uses exponential notation.
190 */
191
192 public CharSequence getCanonicalLexicalRepresentation() {
193 FastStringBuffer fsb = new FastStringBuffer(FastStringBuffer.C16);
194 return FloatingPointConverter.appendFloat(fsb, value, true);
195 }
196
197 /**
198 * Internal method used for conversion of a float to a string
199 *
200 * @param value the actual value
201 * @return the value converted to a string, according to the XPath casting rules.
202 */
203
204 public static CharSequence floatToString(float value) {
205 return FloatingPointConverter.appendFloat(new FastStringBuffer(FastStringBuffer.C16), value, false);
206 }
207
208 /**
209 * Negate the value
210 */
211
212 public NumericValue negate() {
213 return new FloatValue(-value);
214 }
215
216 /**
217 * Implement the XPath floor() function
218 */
219
220 public NumericValue floor() {
221 return new FloatValue((float) Math.floor(value));
222 }
223
224 /**
225 * Implement the XPath ceiling() function
226 */
227
228 public NumericValue ceiling() {
229 return new FloatValue((float) Math.ceil(value));
230 }
231
232 /**
233 * Implement the XPath round() function
234 */
235
236 public NumericValue round(int scale) {
237 if (Float.isNaN(value)) {
238 return this;
239 }
240 if (Float.isInfinite(value)) {
241 return this;
242 }
243 if (value == 0.0) {
244 return this; // handles the negative zero case
245 }
246
247 if (scale == 0 && value > Integer.MIN_VALUE && value < Integer.MAX_VALUE) {
248 if (value >= -0.5 && value < 0.0) {
249 return new FloatValue(-0.0f);
250 }
251 return new FloatValue((float) Math.round(value));
252 }
253 DoubleValue d = new DoubleValue(getDoubleValue());
254 d = (DoubleValue) d.round(scale);
255 return new FloatValue(d.getFloatValue());
256 }
257
258 /**
259 * Implement the XPath round-to-half-even() function
260 */
261
262 public NumericValue roundHalfToEven(int scale) {
263 DoubleValue d = new DoubleValue(getDoubleValue());
264 d = (DoubleValue) d.roundHalfToEven(scale);
265 return new FloatValue(d.getFloatValue());
266 }
267
268 /**
269 * Determine whether the value is negative, zero, or positive
270 *
271 * @return -1 if negative, 0 if zero (including negative zero), +1 if positive, NaN if NaN
272 */
273
274 public int signum() {
275 if (Float.isNaN(value)) {
276 return 0;
277 }
278 return compareTo(0);
279 }
280
281 /**
282 * Ask whether this value is negative zero
283 *
284 * @return true if this value is float or double negative zero
285 */
286 @Override
287 public boolean isNegativeZero() {
288 return value == 0.0 && (Float.floatToIntBits(value) & FloatingPointConverter.FLOAT_SIGN_MASK) != 0;
289 }
290
291
292 /**
293 * Determine whether the value is a whole number, that is, whether it compares
294 * equal to some integer
295 */
296
297 public boolean isWholeNumber() {
298 return value == Math.floor(value) && !Float.isInfinite(value);
299 }
300
301 /**
302 * Test whether a number is a possible subscript into a sequence, that is,
303 * a whole number greater than zero and less than 2^31
304 *
305 * @return the number as an int if it is a possible subscript, or -1 otherwise
306 */
307 @Override
308 public int asSubscript() {
309 if (isWholeNumber() && value > 0 && value <= Integer.MAX_VALUE) {
310 return (int) value;
311 } else {
312 return -1;
313 }
314 }
315
316 /**
317 * Get the absolute value as defined by the XPath abs() function
318 *
319 * @return the absolute value
320 * @since 9.2
321 */
322
323 public NumericValue abs() {
324 if (value > 0.0) {
325 return this;
326 } else {
327 return new FloatValue(Math.abs(value));
328 }
329 }
330
331 public int compareTo(NumericValue other) {
332 if (other instanceof FloatValue) {
333 float otherFloat = ((FloatValue) other).value;
334 // Do not rewrite as Float.compare() - see IntelliJ bug IDEA-196419
335 if (value == otherFloat) {
336 return 0;
337 } else if (value < otherFloat) {
338 return -1;
339 } else {
340 return +1;
341 }
342 }
343 if (other instanceof DoubleValue) {
344 return super.compareTo(other);
345 }
346 return compareTo(Converter.NumericToFloat.INSTANCE.convert(other));
347 }
348
349 /**
350 * Compare the value to a long
351 *
352 * @param other the value to be compared with
353 * @return -1 if this is less, 0 if this is equal, +1 if this is greater or if this is NaN
354 */
355
356 public int compareTo(long other) {
357 float otherFloat = (float) other;
358 if (value == otherFloat) {
359 return 0;
360 }
361 return value < otherFloat ? -1 : +1;
362 }
363
364 /**
365 * Get an object that implements XML Schema comparison semantics
366 */
367
368 public Comparable getSchemaComparable() {
369 // Convert negative to positive zero because Float.compareTo() does the wrong thing
370 // Note that for NaN, we return NaN, and rely on the user of the Comparable to use it in a way
371 // that ensures NaN != NaN.
372 return value == 0.0f ? 0.0f : value;
373 }
374
375 /**
376 * Get a value whose equals() method follows the "same key" rules for comparing the keys of a map.
377 *
378 * @return a value with the property that the equals() and hashCode() methods follow the rules for comparing
379 * keys in maps.
380 */
381
382 @Override
383 public AtomicMatchKey asMapKey() {
384 if (isNaN()) {
385 return AtomicSortComparer.COLLATION_KEY_NaN;
386 } else if (Double.isInfinite(value)) {
387 return new DoubleValue(value);
388 } else {
389 try {
390 return new BigDecimalValue(value);
391 } catch (ValidationException e) {
392 // We have already ruled out the values that fail (NaN and INF)
393 throw new AssertionError(e);
394 }
395 }
396 }
397
398 /**
399 * Determine whether two atomic values are identical, as determined by XML Schema rules. This is a stronger
400 * test than equality (even schema-equality); for example two dateTime values are not identical unless
401 * they are in the same timezone.
402 * <p>Note that even this check ignores the type annotation of the value. The integer 3 and the short 3
403 * are considered identical, even though they are not fully interchangeable. "Identical" means the
404 * same point in the value space, regardless of type annotation.</p>
405 * <p>NaN is identical to itself.</p>
406 *
407 * @param v the other value to be compared with this one
408 * @return true if the two values are identical, false otherwise.
409 */
410
411 public boolean isIdentical(/*@NotNull*/ AtomicValue v) {
412 return v instanceof FloatValue && DoubleSortComparer.getInstance().comparesEqual(this, (FloatValue) v);
413 }
414
415 public FloatValue asAtomic() {
416 return this;
417 }
418 }
419