File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\value\ValueFloat.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.value;
8
9 import java.sql.PreparedStatement;
10 import java.sql.SQLException;
11 import org.h2.constant.ErrorCode;
12 import org.h2.message.DbException;
13
14 /**
15 * Implementation of the REAL data type.
16 */
17 public class ValueFloat extends Value {
18
19 /**
20 * Float.floatToIntBits(0.0F).
21 */
22 public static final int ZERO_BITS = Float.floatToIntBits(0.0F);
23
24 /**
25 * The precision in digits.
26 */
27 static final int PRECISION = 7;
28
29 /**
30 * The maximum display size of a float.
31 * Example: -1.12345676E-20
32 */
33 static final int DISPLAY_SIZE = 15;
34
35 private static final ValueFloat ZERO = new ValueFloat(0.0F);
36 private static final ValueFloat ONE = new ValueFloat(1.0F);
37
38 private final float value;
39
40 private ValueFloat(float value) {
41 this.value = value;
42 }
43
44 public Value add(Value v) {
45 ValueFloat v2 = (ValueFloat) v;
46 return ValueFloat.get(value + v2.value);
47 }
48
49 public Value subtract(Value v) {
50 ValueFloat v2 = (ValueFloat) v;
51 return ValueFloat.get(value - v2.value);
52 }
53
54 public Value negate() {
55 return ValueFloat.get(-value);
56 }
57
58 public Value multiply(Value v) {
59 ValueFloat v2 = (ValueFloat) v;
60 return ValueFloat.get(value * v2.value);
61 }
62
63 public Value divide(Value v) {
64 ValueFloat v2 = (ValueFloat) v;
65 if (v2.value == 0.0) {
66 throw DbException.get(ErrorCode.DIVISION_BY_ZERO_1, getSQL());
67 }
68 return ValueFloat.get(value / v2.value);
69 }
70
71 public Value modulus(Value v) {
72 ValueFloat other = (ValueFloat) v;
73 if (other.value == 0) {
74 throw DbException.get(ErrorCode.DIVISION_BY_ZERO_1, getSQL());
75 }
76 return ValueFloat.get(value % other.value);
77 }
78
79 public String getSQL() {
80 if (value == Float.POSITIVE_INFINITY) {
81 return "POWER(0, -1)";
82 } else if (value == Float.NEGATIVE_INFINITY) {
83 return "(-POWER(0, -1))";
84 } else if (Double.isNaN(value)) {
85 // NaN
86 return "SQRT(-1)";
87 }
88 String s = getString();
89 if (s.equals("-0.0")) {
90 return "-CAST(0 AS REAL)";
91 }
92 return s;
93 }
94
95 public int getType() {
96 return Value.FLOAT;
97 }
98
99 protected int compareSecure(Value o, CompareMode mode) {
100 ValueFloat v = (ValueFloat) o;
101 return Float.compare(value, v.value);
102 }
103
104 public int getSignum() {
105 return value == 0 ? 0 : (value < 0 ? -1 : 1);
106 }
107
108 public float getFloat() {
109 return value;
110 }
111
112 public String getString() {
113 return String.valueOf(value);
114 }
115
116 public long getPrecision() {
117 return PRECISION;
118 }
119
120 public int getScale() {
121 return 0;
122 }
123
124 public int hashCode() {
125 long hash = Float.floatToIntBits(value);
126 return (int) (hash ^ (hash >> 32));
127 }
128
129 public Object getObject() {
130 return Float.valueOf(value);
131 }
132
133 public void set(PreparedStatement prep, int parameterIndex) throws SQLException {
134 prep.setFloat(parameterIndex, value);
135 }
136
137 /**
138 * Get or create float value for the given float.
139 *
140 * @param d the float
141 * @return the value
142 */
143 public static ValueFloat get(float d) {
144 if (d == 1.0F) {
145 return ONE;
146 } else if (d == 0.0F) {
147 // unfortunately, -0.0 == 0.0, but we don't want to return
148 // 0.0 in this case
149 if (Float.floatToIntBits(d) == ZERO_BITS) {
150 return ZERO;
151 }
152 }
153 return (ValueFloat) Value.cache(new ValueFloat(d));
154 }
155
156 public int getDisplaySize() {
157 return DISPLAY_SIZE;
158 }
159
160 public boolean equals(Object other) {
161 if (!(other instanceof ValueFloat)) {
162 return false;
163 }
164 return compareSecure((ValueFloat) other, null) == 0;
165 }
166
167 }
168