1 package org.rosuda.REngine;
3 /** REXPDouble represents a vector of double precision floating point values. */
4 public class REXPDouble extends REXPVector {
5 public double[] payload;
7 /** NA real value as defined in R. Note: it can NOT be used in comparisons, you must use {@link #isNA(double)} instead. */
8 public static final double NA = Double.longBitsToDouble(0x7ff00000000007a2L);
10 /** Java screws up the bits in NA real values, so we cannot compare to the real bits used by R (0x7ff00000000007a2L) but use this value which is obtained by passing the bits through Java's double type */
11 static final long NA_bits = Double.doubleToRawLongBits(Double.longBitsToDouble(0x7ff00000000007a2L));
13 /** checks whether a given double value is a NA representation in R. Note that NA is NaN but not all NaNs are NA. */
14 public static boolean isNA(double value) {
15 /* on OS X i386 the MSB of the fraction is set even though R doesn't set it.
16 Although this is technically a good idea (to make it a QNaN) it's not what R does and thus makes the comparison tricky */
17 return (Double.doubleToRawLongBits(value) & 0xfff7ffffffffffffL) == (NA_bits & 0xfff7ffffffffffffL);
20 /** create real vector of the length 1 with the given value as its first (and only) element */
21 public REXPDouble(double load) {
23 payload=new double[] { load };
26 public REXPDouble(double[] load) {
28 payload=(load==null)?new double[0]:load;
31 public REXPDouble(double[] load, REXPList attr) {
33 payload=(load==null)?new double[0]:load;
36 public int length() { return payload.length; }
38 public Object asNativeJavaObject() {
42 /** return <code>true</code> */
43 public boolean isNumeric() { return true; }
45 /** returns the values represented by this vector */
46 public double[] asDoubles() { return payload; }
48 /** converts the values of this vector into integers by cast */
49 public int[] asIntegers() {
50 int[] a = new int[payload.length];
52 while (i < payload.length) { a[i] = (int) payload[i]; i++; }
56 /** converts the values of this vector into strings */
57 public String[] asStrings() {
58 String[] s = new String[payload.length];
60 while (i < payload.length) { s[i] = ""+payload[i]; i++; }
64 /** returns a boolean vector of the same length as this vector with <code>true</code> for NA values and <code>false</code> for any other values (including NaNs) */
65 public boolean[] isNA() {
66 boolean a[] = new boolean[payload.length];
68 while (i < a.length) { a[i] = isNA(payload[i]); i++; }
72 public String toDebugString() {
73 StringBuffer sb = new StringBuffer(super.toDebugString()+"{");
75 while (i < payload.length && i < maxDebugItems) {
76 if (i>0) sb.append(",");
77 sb.append(payload[i]);
80 if (i < payload.length) sb.append(",..");
81 return sb.toString()+"}";