]> gerrit.simantics Code Review - simantics/r.git/blob - bundles/org.simantics.r.scl/src/org/rosuda/REngine/REXPDouble.java
Restructured R repository for Tycho POMless builds.
[simantics/r.git] / bundles / org.simantics.r.scl / src / org / rosuda / REngine / REXPDouble.java
1 package org.rosuda.REngine;
2
3 /** REXPDouble represents a vector of double precision floating point values. */
4 public class REXPDouble extends REXPVector {
5         public double[] payload;
6         
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);
9         
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));
12         
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);
18         }
19
20         /** create real vector of the length 1 with the given value as its first (and only) element */
21         public REXPDouble(double load) {
22                 super();
23                 payload=new double[] { load };
24         }
25         
26         public REXPDouble(double[] load) {
27                 super();
28                 payload=(load==null)?new double[0]:load;
29         }
30
31         public REXPDouble(double[] load, REXPList attr) {
32                 super(attr);
33                 payload=(load==null)?new double[0]:load;
34         }
35         
36         public int length() { return payload.length; }
37
38         public Object asNativeJavaObject() {
39                 return payload;
40         }
41
42         /** return <code>true</code> */
43         public boolean isNumeric() { return true; }
44
45         /** returns the values represented by this vector */
46         public double[] asDoubles() { return payload; }
47
48         /** converts the values of this vector into integers by cast */
49         public int[] asIntegers() {
50                 int[] a = new int[payload.length];
51                 int i = 0;
52                 while (i < payload.length) { a[i] = (int) payload[i]; i++; }
53                 return a;
54         }
55
56         /** converts the values of this vector into strings */
57         public String[] asStrings() {
58                 String[] s = new String[payload.length];
59                 int i = 0;
60                 while (i < payload.length) { s[i] = ""+payload[i]; i++; }
61                 return s;
62         }
63         
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];
67                 int i = 0;
68                 while (i < a.length) { a[i] = isNA(payload[i]); i++; }
69                 return a;
70         }
71         
72         public String toDebugString() {
73                 StringBuffer sb = new StringBuffer(super.toDebugString()+"{");
74                 int i = 0;
75                 while (i < payload.length && i < maxDebugItems) {
76                         if (i>0) sb.append(",");
77                         sb.append(payload[i]);
78                         i++;
79                 }
80                 if (i < payload.length) sb.append(",..");
81                 return sb.toString()+"}";
82         }
83         
84 }