-
-
Notifications
You must be signed in to change notification settings - Fork 179
/
FloatValue.java
497 lines (447 loc) · 17.7 KB
/
FloatValue.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/*
* eXist-db Open Source Native XML Database
* Copyright (C) 2001 The eXist-db Authors
*
* info@exist-db.org
* http://www.exist-db.org
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package org.exist.xquery.value;
import com.ibm.icu.text.Collator;
import net.sf.saxon.tree.util.FastStringBuffer;
import net.sf.saxon.value.FloatingPointConverter;
import org.exist.xquery.Constants;
import org.exist.xquery.ErrorCodes;
import org.exist.xquery.XPathException;
import javax.annotation.Nullable;
import java.math.BigDecimal;
import java.util.function.IntSupplier;
/**
* @author wolf
*/
public class FloatValue extends NumericValue {
// m × 2^e, where m is an integer whose absolute value is less than 2^24,
// and e is an integer between -149 and 104, inclusive.
// In addition also -INF, +INF and NaN.
public final static FloatValue NaN = new FloatValue(Float.NaN);
public final static FloatValue POSITIVE_INFINITY = new FloatValue(Float.POSITIVE_INFINITY);
public final static FloatValue NEGATIVE_INFINITY = new FloatValue(Float.NEGATIVE_INFINITY);
public final static FloatValue ZERO = new FloatValue(0.0E0f);
final float value;
public FloatValue(float value) {
this.value = value;
}
public FloatValue(String stringValue) throws XPathException {
try {
if ("INF".equals(stringValue)) {
value = Float.POSITIVE_INFINITY;
} else if ("-INF".equals(stringValue)) {
value = Float.NEGATIVE_INFINITY;
} else if ("NaN".equals(stringValue)) {
value = Float.NaN;
} else {
value = Float.parseFloat(stringValue);
}
} catch (final NumberFormatException e) {
throw new XPathException(ErrorCodes.FORG0001, "cannot construct "
+ Type.getTypeName(this.getItemType())
+ " from \""
+ getStringValue()
+ "\"");
}
}
public float getValue() {
return value;
}
/*
public float getFloat() throws XPathException {
return value;
}
*/
/* (non-Javadoc)
* @see org.exist.xquery.value.AtomicValue#getType()
*/
public int getType() {
return Type.FLOAT;
}
/* (non-Javadoc)
* @see org.exist.xquery.value.Sequence#getStringValue()
*/
public String getStringValue() throws XPathException {
/*
if (value == Float.POSITIVE_INFINITY)
return "INF";
if (value == Float.NEGATIVE_INFINITY)
return "-INF";
String s = String.valueOf(value);
s = s.replaceAll("\\.0+$", "");
return s;
*/
final FastStringBuffer sb = new FastStringBuffer(20);
//0 is a dummy parameter
FloatingPointConverter.appendFloat(sb, value, false);
return sb.toString();
}
/* (non-Javadoc)
* @see org.exist.xquery.value.NumericValue#isNaN()
*/
public boolean isNaN() {
return Float.isNaN(value);
}
public boolean isInfinite() {
return Float.isInfinite(value);
}
public boolean isZero() {
return Float.compare(value, 0f) == Constants.EQUAL;
}
public boolean isNegative() {
return (Float.compare(value, 0f) < Constants.EQUAL);
}
public boolean isPositive() {
return (Float.compare(value, 0f) > Constants.EQUAL);
}
@Override
protected @Nullable IntSupplier createComparisonWith(final NumericValue other) {
final IntSupplier comparison;
if (other instanceof IntegerValue) {
comparison = () -> BigDecimal.valueOf(value).compareTo(new BigDecimal(((IntegerValue)other).value));
} else if (other instanceof DecimalValue) {
final BigDecimal promoted = new BigDecimal(Float.toString(value));
comparison = () -> promoted.compareTo(((DecimalValue)other).value);
} else if (other instanceof DoubleValue) {
comparison = () -> BigDecimal.valueOf(value).compareTo(BigDecimal.valueOf(((DoubleValue)other).value));
} else if (other instanceof FloatValue) {
comparison = () -> Float.compare(value, ((FloatValue)other).value);
} else {
return null;
}
return comparison;
}
public boolean hasFractionalPart() {
if (isNaN()) {
return false;
}
if (isInfinite()) {
return false;
}
return new DecimalValue(new BigDecimal(value)).hasFractionalPart();
}
/* (non-Javadoc)
* @see org.exist.xquery.value.Sequence#convertTo(int)
*/
public AtomicValue convertTo(int requiredType) throws XPathException {
switch (requiredType) {
case Type.ATOMIC:
case Type.ITEM:
case Type.NUMBER:
case Type.FLOAT:
return this;
case Type.DOUBLE:
return new DoubleValue(value);
case Type.STRING:
return new StringValue(getStringValue());
case Type.DECIMAL:
return new DecimalValue(value);
case Type.INTEGER:
case Type.NON_POSITIVE_INTEGER:
case Type.NEGATIVE_INTEGER:
case Type.LONG:
case Type.INT:
case Type.SHORT:
case Type.BYTE:
case Type.NON_NEGATIVE_INTEGER:
case Type.UNSIGNED_LONG:
case Type.UNSIGNED_INT:
case Type.UNSIGNED_SHORT:
case Type.UNSIGNED_BYTE:
case Type.POSITIVE_INTEGER:
if (!(Float.isInfinite(value) || Float.isNaN(value))) {
return new IntegerValue((long) value, requiredType);
} else {
throw new XPathException(ErrorCodes.FOCA0002, "cannot convert ' xs:float(\""
+ getStringValue()
+ "\")' to "
+ Type.getTypeName(requiredType));
}
case Type.BOOLEAN:
return (value == 0.0f || Float.isNaN(value))
? BooleanValue.FALSE
: BooleanValue.TRUE;
case Type.UNTYPED_ATOMIC:
return new UntypedAtomicValue(getStringValue());
default:
throw new XPathException(ErrorCodes.FORG0001, "cannot cast '"
+ Type.getTypeName(this.getItemType())
+ "(\""
+ getStringValue()
+ "\")' to "
+ Type.getTypeName(requiredType));
}
}
/* (non-Javadoc)
* @see org.exist.xquery.value.NumericValue#negate()
*/
public NumericValue negate() throws XPathException {
return new FloatValue(-value);
}
/* (non-Javadoc)
* @see org.exist.xquery.value.NumericValue#ceiling()
*/
public NumericValue ceiling() throws XPathException {
return new FloatValue((float) Math.ceil(value));
}
/* (non-Javadoc)
* @see org.exist.xquery.value.NumericValue#floor()
*/
public NumericValue floor() throws XPathException {
return new FloatValue((float) Math.floor(value));
}
/* (non-Javadoc)
* @see org.exist.xquery.value.NumericValue#round()
*/
public NumericValue round() throws XPathException {
if (Float.isNaN(value) || Float.isInfinite(value) || value == 0.0) {
return this;
}
if (value >= -0.5 && value < 0.0) {
return new DoubleValue(-0.0);
}
if (value > Integer.MIN_VALUE && value < Integer.MAX_VALUE) {
return new FloatValue((float) Math.round(value));
}
//too big return original value unchanged
return this;
}
/* (non-Javadoc)
* @see org.exist.xquery.value.NumericValue#round(org.exist.xquery.value.IntegerValue)
*/
public NumericValue round(IntegerValue precision) throws XPathException {
if (precision == null) {
return round();
}
if (Float.isNaN(value) || Float.isInfinite(value) || value == 0.0) {
return this;
}
/* use the decimal rounding method */
return (FloatValue) ((DecimalValue) convertTo(Type.DECIMAL)).round(precision).convertTo(Type.FLOAT);
}
@Override
public ComputableValue minus(final ComputableValue other) throws XPathException {
if (Type.subTypeOf(other.getType(), Type.FLOAT)) {
return new FloatValue(value - ((FloatValue) other).value);
} else if (other.getType() == Type.DOUBLE) {
// type promotion - see https://www.w3.org/TR/xpath-31/#promotion
return ((DoubleValue) convertTo(Type.DOUBLE)).minus(other);
} else {
return minus((ComputableValue) other.convertTo(getType()));
}
}
@Override
public ComputableValue plus(final ComputableValue other) throws XPathException {
if (Type.subTypeOf(other.getType(), Type.FLOAT)) {
return new FloatValue(value + ((FloatValue) other).value);
} else if (other.getType() == Type.DOUBLE) {
// type promotion - see https://www.w3.org/TR/xpath-31/#promotion
return ((DoubleValue) convertTo(Type.DOUBLE)).plus(other);
} else {
return plus((ComputableValue) other.convertTo(getType()));
}
}
@Override
public ComputableValue mult(final ComputableValue other) throws XPathException {
switch (other.getType()) {
case Type.FLOAT:
return new FloatValue(value * ((FloatValue) other).value);
case Type.DOUBLE:
// type promotion - see https://www.w3.org/TR/xpath-31/#promotion
return ((DoubleValue) convertTo(Type.DOUBLE)).mult(other);
case Type.DAY_TIME_DURATION:
case Type.YEAR_MONTH_DURATION:
return other.mult(this);
default:
return mult((ComputableValue) other.convertTo(getType()));
}
}
/* (non-Javadoc)
* @see org.exist.xquery.value.NumericValue#div(org.exist.xquery.value.NumericValue)
*/
public ComputableValue div(ComputableValue other) throws XPathException {
if (Type.subTypeOfUnion(other.getType(), Type.NUMBER)) {
//Positive or negative zero divided by positive or negative zero returns NaN.
if (this.isZero() && ((NumericValue) other).isZero()) {
return NaN;
}
//A negative number divided by positive zero returns -INF.
if (this.isNegative() &&
((NumericValue) other).isZero() && ((NumericValue) other).isPositive()) {
return NEGATIVE_INFINITY;
}
//A negative number divided by positive zero returns -INF.
if (this.isNegative() &&
((NumericValue) other).isZero() && ((NumericValue) other).isNegative()) {
return POSITIVE_INFINITY;
}
//Division of Positive by negative zero returns -INF and INF, respectively.
if (this.isPositive() &&
((NumericValue) other).isZero() && ((NumericValue) other).isNegative()) {
return NEGATIVE_INFINITY;
}
if (this.isPositive() &&
((NumericValue) other).isZero() && ((NumericValue) other).isPositive()) {
return POSITIVE_INFINITY;
}
//Also, INF or -INF divided by INF or -INF returns NaN.
if (this.isInfinite() && ((NumericValue) other).isInfinite()) {
return NaN;
}
}
if (Type.subTypeOf(other.getType(), Type.FLOAT)) {
return new FloatValue(value / ((FloatValue) other).value);
} else {
return div((ComputableValue) other.convertTo(getType()));
}
}
@Override
public IntegerValue idiv(final NumericValue other) throws XPathException {
if (other.getType() == Type.DOUBLE) {
// type promotion - see https://www.w3.org/TR/xpath-31/#promotion
return ((DoubleValue) convertTo(Type.DOUBLE)).idiv(other);
} else if (other.getType() == Type.DECIMAL) {
return idiv((FloatValue) other.convertTo(Type.FLOAT));
} else {
final ComputableValue result = div(other);
return new IntegerValue(((IntegerValue) result.convertTo(Type.INTEGER)).getLong());
}
}
@Override
public NumericValue mod(final NumericValue other) throws XPathException {
if (Type.subTypeOf(other.getType(), Type.FLOAT)) {
return new FloatValue(value % ((FloatValue) other).value);
} else if (other.getType() == Type.DOUBLE) {
// type promotion - see https://www.w3.org/TR/xpath-31/#promotion
return ((DoubleValue) convertTo(Type.DOUBLE)).mod(other);
} else {
return mod((NumericValue) other.convertTo(getType()));
}
}
/* (non-Javadoc)
* @see org.exist.xquery.value.NumericValue#abs()
*/
public NumericValue abs() throws XPathException {
return new FloatValue(Math.abs(value));
}
/* (non-Javadoc)
* @see org.exist.xquery.value.NumericValue#max(org.exist.xquery.value.AtomicValue)
*/
public AtomicValue max(Collator collator, AtomicValue other) throws XPathException {
if (Type.subTypeOf(other.getType(), Type.FLOAT)) {
return new FloatValue(Math.max(value, ((FloatValue) other).value));
} else {
return convertTo(other.getType()).max(collator, other);
}
}
public AtomicValue min(Collator collator, AtomicValue other) throws XPathException {
if (Type.subTypeOf(other.getType(), Type.FLOAT)) {
return new FloatValue(Math.min(value, ((FloatValue) other).value));
} else {
return convertTo(other.getType()).min(collator, other);
}
}
/* (non-Javadoc)
* @see org.exist.xquery.value.Item#conversionPreference(java.lang.Class)
*/
public int conversionPreference(Class<?> javaClass) {
if (javaClass.isAssignableFrom(FloatValue.class)) {
return 0;
}
if (javaClass == Long.class || javaClass == long.class) {
return 3;
}
if (javaClass == Integer.class || javaClass == int.class) {
return 4;
}
if (javaClass == Short.class || javaClass == short.class) {
return 5;
}
if (javaClass == Byte.class || javaClass == byte.class) {
return 6;
}
if (javaClass == Double.class || javaClass == double.class) {
return 2;
}
if (javaClass == Float.class || javaClass == float.class) {
return 1;
}
if (javaClass == String.class) {
return 7;
}
if (javaClass == Boolean.class || javaClass == boolean.class) {
return 8;
}
if (javaClass == Object.class) {
return 20;
}
return Integer.MAX_VALUE;
}
/* (non-Javadoc)
* @see org.exist.xquery.value.Item#toJavaObject(java.lang.Class)
*/
@Override
public <T> T toJavaObject(final Class<T> target) throws XPathException {
if (target.isAssignableFrom(FloatValue.class)) {
return (T) this;
} else if (target == Double.class || target == double.class) {
return (T) Double.valueOf(value);
} else if (target == Float.class || target == float.class) {
return (T) Float.valueOf(value);
} else if (target == Long.class || target == long.class) {
return (T) Long.valueOf(((IntegerValue) convertTo(Type.LONG)).getValue());
} else if (target == Integer.class || target == int.class) {
final IntegerValue v = (IntegerValue) convertTo(Type.INT);
return (T) Integer.valueOf((int) v.getValue());
} else if (target == Short.class || target == short.class) {
final IntegerValue v = (IntegerValue) convertTo(Type.SHORT);
return (T) Short.valueOf((short) v.getValue());
} else if (target == Byte.class || target == byte.class) {
final IntegerValue v = (IntegerValue) convertTo(Type.BYTE);
return (T) Byte.valueOf((byte) v.getValue());
} else if (target == String.class) {
return (T) getStringValue();
} else if (target == Boolean.class) {
return (T) Boolean.valueOf(effectiveBooleanValue());
}
throw new XPathException(
"cannot convert value of type "
+ Type.getTypeName(getType())
+ " to Java object of type "
+ target.getName());
}
/* (non-Javadoc)
* @see java.lang.Comparable#compareTo(java.lang.Object)
*/
public int compareTo(Object o) {
final AtomicValue other = (AtomicValue) o;
if (Type.subTypeOf(other.getType(), Type.FLOAT)) {
return Float.compare(value, ((FloatValue) other).value);
} else {
return getType() < other.getType() ? Constants.INFERIOR : Constants.SUPERIOR;
}
}
@Override
public int hashCode() {
return Float.valueOf(value).hashCode();
}
}