6 // unnecessary to instantialize unless subclassed
9 public static float bytesToFloat(byte[] bytes, int j, boolean isBigEndian) throws Exception {
10 return intToFloat(bytesToInt(bytes, j, isBigEndian));
13 public static int bytesToInt(byte[] bytes, int j, boolean isBigEndian) {
14 int n = (isBigEndian ? (bytes[j + 3] & 0xff) | (bytes[j + 2] & 0xff) << 8
15 | (bytes[j + 1] & 0xff) << 16 | (bytes[j] & 0xff) << 24
16 : (bytes[j++] & 0xff) | (bytes[j++] & 0xff) << 8
17 | (bytes[j++] & 0xff) << 16 | (bytes[j++] & 0xff) << 24);
21 * return (n > 0x7FFFFFFF ? n - 0x100000000 : n);
29 public static float intToFloat(int x) throws Exception {
31 * see http://en.wikipedia.org/wiki/Binary32
33 * [sign] [8 bits power] [23 bits fraction]
34 * 0x80000000 0x7F800000 0x7FFFFF
40 * if (x == 0) return 0;
41 * var o = javajs.util.BC;
42 * if (o.fracIEEE == null)
44 * var m = ((x & 0x7F800000) >> 23);
45 * return ((x & 0x80000000) == 0 ? 1 : -1) * o.shiftIEEE((x & 0x7FFFFF) | 0x800000, m - 149);
49 return Float.intBitsToFloat(x);
54 * see http://en.wikipedia.org/wiki/Binary64
56 * not concerning ourselves with very small or very large numbers and getting
57 * this exactly right. Just need a float here.
64 public static float bytesToDoubleToFloat(byte[] bytes, int j, boolean isBigEndian) {
66 // IEEE754: sign (1 bit), exponent (11 bits), fraction (52 bits).
67 // seeeeeee eeeeffff ffffffff ffffffff ffffffff xxxxxxxx xxxxxxxx xxxxxxxx
68 // b1 b2 b3 b4 b5 ---------float ignores----
75 * var o = javajs.util.BC;
76 * var b1, b2, b3, b4, b5;
79 * b1 = bytes[j] & 0xFF;
80 * b2 = bytes[j + 1] & 0xFF;
81 * b3 = bytes[j + 2] & 0xFF;
82 * b4 = bytes[j + 3] & 0xFF;
83 * b5 = bytes[j + 4] & 0xFF;
85 * b1 = bytes[j + 7] & 0xFF;
86 * b2 = bytes[j + 6] & 0xFF;
87 * b3 = bytes[j + 5] & 0xFF;
88 * b4 = bytes[j + 4] & 0xFF;
89 * b5 = bytes[j + 3] & 0xFF;
91 * var s = ((b1 & 0x80) == 0 ? 1 : -1);
92 * var e = (((b1 & 0x7F) << 4) | (b2 >> 4)) - 1026;
93 * b2 = (b2 & 0xF) | 0x10;
94 * return s * (o.shiftIEEE(b2, e) + o.shiftIEEE(b3, e - 8) + o.shiftIEEE(b4, e - 16)
95 * + o.shiftIEEE(b5, e - 24));
101 d = Double.longBitsToDouble((((long) bytes[j]) & 0xff) << 56
102 | (((long) bytes[j + 1]) & 0xff) << 48
103 | (((long) bytes[j + 2]) & 0xff) << 40
104 | (((long) bytes[j + 3]) & 0xff) << 32
105 | (((long) bytes[j + 4]) & 0xff) << 24
106 | (((long) bytes[j + 5]) & 0xff) << 16
107 | (((long) bytes[j + 6]) & 0xff) << 8
108 | (((long) bytes[7]) & 0xff));
110 d = Double.longBitsToDouble((((long) bytes[j + 7]) & 0xff) << 56
111 | (((long) bytes[j + 6]) & 0xff) << 48
112 | (((long) bytes[j + 5]) & 0xff) << 40
113 | (((long) bytes[j + 4]) & 0xff) << 32
114 | (((long) bytes[j + 3]) & 0xff) << 24
115 | (((long) bytes[j + 2]) & 0xff) << 16
116 | (((long) bytes[j + 1]) & 0xff) << 8
117 | (((long) bytes[j]) & 0xff));
124 private static float[] fracIEEE;
126 private static void setFracIEEE() {
127 fracIEEE = new float[270];
128 for (int i = 0; i < 270; i++)
129 fracIEEE[i] = (float) Math.pow(2, i - 141);
130 // System.out.println(fracIEEE[0] + " " + Parser.FLOAT_MIN_SAFE);
131 // System.out.println(fracIEEE[269] + " " + Float.MAX_VALUE);
135 * only concerned about reasonable float values here -- private but not designated; called by JavaScript
141 static double shiftIEEE(double f, int i) {
142 if (f == 0 || i < -140)
145 return Float.MAX_VALUE;
146 return f * fracIEEE[i + 140];
151 // for (int i = -50; i < 50; i++) {
152 // float f = i * (float) (Math.random() * Math.pow(2, Math.random() * 100 - 50));
153 // int x = Float.floatToIntBits(f);
154 // int m = ((x & 0x7F800000) >> 23);
155 // float f1 = (float) (f == 0 ? 0 : ((x & 0x80000000) == 0 ? 1 : -1) * shiftIEEE((x & 0x7FFFFF) | 0x800000, m - 149));
156 // System.out.println(f + " " + f1);
158 // System.out.println("binarydo");