| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 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 |
|
| 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 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public FloatValue(float value) { |
| 42 |
this.value = value; |
| 43 |
typeLabel = BuiltInAtomicType.FLOAT; |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public static FloatValue makeFloatValue(float value) { |
| 54 |
return new FloatValue(value); |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
public FloatValue(float value, AtomicType type) { |
| 69 |
this.value = value; |
| 70 |
typeLabel = type; |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 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 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
public BuiltInAtomicType getPrimitiveType() { |
| 94 |
return BuiltInAtomicType.FLOAT; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
public float getFloatValue() { |
| 102 |
return value; |
| 103 |
} |
| 104 |
|
| 105 |
public double getDoubleValue() { |
| 106 |
return (double) value; |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
@Override |
| 117 |
public BigDecimal getDecimalValue() throws ValidationException { |
| 118 |
return new BigDecimal((double) value); |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
@Override |
| 130 |
public long longValue() throws XPathException { |
| 131 |
return (long) value; |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 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 |
|
| 150 |
|
| 151 |
|
| 152 |
public boolean isNaN() { |
| 153 |
return Float.isNaN(value); |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
public boolean effectiveBooleanValue() { |
| 162 |
return (value != 0.0 && !Float.isNaN(value)); |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
public CharSequence getPrimitiveStringValue() { |
| 183 |
return floatToString(value); |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
public CharSequence getCanonicalLexicalRepresentation() { |
| 193 |
FastStringBuffer fsb = new FastStringBuffer(FastStringBuffer.C16); |
| 194 |
return FloatingPointConverter.appendFloat(fsb, value, true); |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
public static CharSequence floatToString(float value) { |
| 205 |
return FloatingPointConverter.appendFloat(new FastStringBuffer(FastStringBuffer.C16), value, false); |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
public NumericValue negate() { |
| 213 |
return new FloatValue(-value); |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
public NumericValue floor() { |
| 221 |
return new FloatValue((float) Math.floor(value)); |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
public NumericValue ceiling() { |
| 229 |
return new FloatValue((float) Math.ceil(value)); |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
|
| 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; |
| 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 |
|
| 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 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
public int signum() { |
| 275 |
if (Float.isNaN(value)) { |
| 276 |
return 0; |
| 277 |
} |
| 278 |
return compareTo(0); |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 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 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
public boolean isWholeNumber() { |
| 298 |
return value == Math.floor(value) && !Float.isInfinite(value); |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 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 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 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 |
|
| 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 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 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 |
|
| 366 |
|
| 367 |
|
| 368 |
public Comparable getSchemaComparable() { |
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
return value == 0.0f ? 0.0f : value; |
| 373 |
} |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 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 |
|
| 393 |
throw new AssertionError(e); |
| 394 |
} |
| 395 |
} |
| 396 |
} |
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
public boolean isIdentical( 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 |
|