/user/super/com/google/gwt/emul/java/lang/Float.java

https://github.com/scalagwt/google-web-toolkit-svnmirror · Java · 255 lines · 172 code · 37 blank · 46 comment · 39 complexity · 07fc5875a024897a82e709d03d65e827 MD5 · raw file

  1. /*
  2. * Copyright 2008 Google Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package java.lang;
  17. /**
  18. * Wraps a primitive <code>float</code> as an object.
  19. */
  20. public final class Float extends Number implements Comparable<Float> {
  21. public static final float MAX_VALUE = 3.4028235e+38f;
  22. public static final float MIN_VALUE = 1.4e-45f;
  23. public static final float MAX_EXPONENT = 127;
  24. public static final float MIN_EXPONENT = -126;
  25. public static final float MIN_NORMAL = 1.1754943508222875E-38f;
  26. public static final float NaN = 0f / 0f;
  27. public static final float NEGATIVE_INFINITY = -1f / 0f;
  28. public static final float POSITIVE_INFINITY = 1f / 0f;
  29. public static final int SIZE = 32;
  30. private static final long POWER_31_INT = 2147483648L;
  31. private static final long POWER_32_INT = 4294967296L;
  32. public static int compare(float x, float y) {
  33. if (x < y) {
  34. return -1;
  35. } else if (x > y) {
  36. return 1;
  37. } else {
  38. return 0;
  39. }
  40. }
  41. public static int floatToIntBits(float value) {
  42. // Return a canonical NaN
  43. if (isNaN(value)) {
  44. return 0x7fc00000;
  45. }
  46. if (value == 0.0f) {
  47. if (1.0 / value == NEGATIVE_INFINITY) {
  48. return 0x80000000; // -0.0f
  49. } else {
  50. return 0x0;
  51. }
  52. }
  53. boolean negative = false;
  54. if (value < 0.0) {
  55. negative = true;
  56. value = -value;
  57. }
  58. if (isInfinite(value)) {
  59. if (negative) {
  60. return 0xff800000;
  61. } else {
  62. return 0x7f800000;
  63. }
  64. }
  65. // Obtain the 64-bit representation and extract its exponent and
  66. // mantissa.
  67. long l = Double.doubleToLongBits((double) value);
  68. int exp = (int) (((l >> 52) & 0x7ff) - 1023);
  69. int mantissa = (int) ((l & 0xfffffffffffffL) >> 29);
  70. // If the number will be a denorm in the float representation
  71. // (i.e., its exponent is -127 or smaller), add a leading 1 to the
  72. // mantissa and shift it right to maintain an exponent of -127.
  73. if (exp <= -127) {
  74. mantissa = (0x800000 | mantissa) >> (-127 - exp + 1);
  75. exp = -127;
  76. }
  77. // Construct the 32-bit representation
  78. long bits = negative ? POWER_31_INT : 0x0L;
  79. bits |= (exp + 127) << 23;
  80. bits |= mantissa;
  81. return (int) bits;
  82. }
  83. /**
  84. * @skip Here for shared implementation with Arrays.hashCode.
  85. * @param f
  86. * @return hash value of float (currently just truncated to int)
  87. */
  88. public static int hashCode(float f) {
  89. return (int) f;
  90. }
  91. public static float intBitsToFloat(int bits) {
  92. boolean negative = (bits & 0x80000000) != 0;
  93. int exp = (bits >> 23) & 0xff;
  94. bits &= 0x7fffff;
  95. if (exp == 0x0) {
  96. // Handle +/- 0 here, denorms below
  97. if (bits == 0) {
  98. return negative ? -0.0f : 0.0f;
  99. }
  100. } else if (exp == 0xff) {
  101. // Inf & NaN
  102. if (bits == 0) {
  103. return negative ? NEGATIVE_INFINITY : POSITIVE_INFINITY;
  104. } else {
  105. return NaN;
  106. }
  107. }
  108. if (exp == 0) {
  109. // Input is denormalized, renormalize by shifting left until there is a
  110. // leading 1
  111. exp = 1;
  112. while ((bits & 0x800000) == 0) {
  113. bits <<= 1;
  114. exp--;
  115. }
  116. bits &= 0x7fffff;
  117. }
  118. // Build the bits of a 64-bit double from the incoming bits
  119. long bits64 = negative ? 0x8000000000000000L : 0x0L;
  120. bits64 |= ((long) (exp + 896)) << 52;
  121. bits64 |= ((long) bits) << 29;
  122. return (float) Double.longBitsToDouble(bits64);
  123. }
  124. public static native boolean isInfinite(float x) /*-{
  125. return !isFinite(x);
  126. }-*/;
  127. public static native boolean isNaN(float x) /*-{
  128. return isNaN(x);
  129. }-*/;
  130. public static float parseFloat(String s) throws NumberFormatException {
  131. double doubleValue = __parseAndValidateDouble(s);
  132. if (doubleValue > Float.MAX_VALUE) {
  133. return Float.POSITIVE_INFINITY;
  134. } else if (doubleValue < -Float.MAX_VALUE) {
  135. return Float.NEGATIVE_INFINITY;
  136. }
  137. return (float) doubleValue;
  138. }
  139. public static String toString(float b) {
  140. return String.valueOf(b);
  141. }
  142. public static Float valueOf(float f) {
  143. return new Float(f);
  144. }
  145. public static Float valueOf(String s) throws NumberFormatException {
  146. return new Float(Float.parseFloat(s));
  147. }
  148. private final transient float value;
  149. public Float(double value) {
  150. this.value = (float) value;
  151. }
  152. public Float(float value) {
  153. this.value = value;
  154. }
  155. public Float(String s) {
  156. value = parseFloat(s);
  157. }
  158. @Override
  159. public byte byteValue() {
  160. return (byte) value;
  161. }
  162. public int compareTo(Float b) {
  163. if (value < b.value) {
  164. return -1;
  165. } else if (value > b.value) {
  166. return 1;
  167. } else {
  168. return 0;
  169. }
  170. }
  171. @Override
  172. public double doubleValue() {
  173. return value;
  174. }
  175. @Override
  176. public boolean equals(Object o) {
  177. return (o instanceof Float) && (((Float) o).value == value);
  178. }
  179. @Override
  180. public float floatValue() {
  181. return value;
  182. }
  183. /**
  184. * Performance caution: using Float objects as map keys is not recommended.
  185. * Using floating point values as keys is generally a bad idea due to
  186. * difficulty determining exact equality. In addition, there is no efficient
  187. * JavaScript equivalent of <code>floatToIntBits</code>. As a result, this
  188. * method computes a hash code by truncating the whole number portion of the
  189. * float, which may lead to poor performance for certain value sets if Floats
  190. * are used as keys in a {@link java.util.HashMap}.
  191. */
  192. @Override
  193. public int hashCode() {
  194. return hashCode(value);
  195. }
  196. @Override
  197. public int intValue() {
  198. return (int) value;
  199. }
  200. public boolean isInfinite() {
  201. return isInfinite(value);
  202. }
  203. public boolean isNaN() {
  204. return isNaN(value);
  205. }
  206. @Override
  207. public long longValue() {
  208. return (long) value;
  209. }
  210. @Override
  211. public short shortValue() {
  212. return (short) value;
  213. }
  214. @Override
  215. public String toString() {
  216. return toString(value);
  217. }
  218. }