1
package org.apache.lucene.util;
4
* Copyright 2005 The Apache Software Foundation
6
* Licensed under the Apache License, Version 2.0 (the "License");
7
* you may not use this file except in compliance with the License.
8
* You may obtain a copy of the License at
10
* http://www.apache.org/licenses/LICENSE-2.0
12
* Unless required by applicable law or agreed to in writing, software
13
* distributed under the License is distributed on an "AS IS" BASIS,
14
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
* See the License for the specific language governing permissions and
16
* limitations under the License.
19
public class TestSmallFloat extends LuceneTestCase {
21
// original lucene byteToFloat
22
static float orig_byteToFloat(byte b) {
23
if (b == 0) // zero is a special case
26
int exponent = (b >> 3) & 31;
27
int bits = ((exponent+(63-15)) << 24) | (mantissa << 21);
28
return Float.intBitsToFloat(bits);
31
// original lucene floatToByte (since lucene 1.3)
32
static byte orig_floatToByte_v13(float f) {
33
if (f < 0.0f) // round negatives up to zero
36
if (f == 0.0f) // zero is a special case
39
int bits = Float.floatToIntBits(f); // parse float into parts
40
int mantissa = (bits & 0xffffff) >> 21;
41
int exponent = (((bits >> 24) & 0x7f) - 63) + 15;
43
if (exponent > 31) { // overflow: use max value
48
if (exponent < 0) { // underflow: use min value
53
return (byte)((exponent << 3) | mantissa); // pack into a byte
56
// This is the original lucene floatToBytes (from v1.3)
57
// except with the underflow detection bug fixed for values like 5.8123817E-10f
58
static byte orig_floatToByte(float f) {
59
if (f < 0.0f) // round negatives up to zero
62
if (f == 0.0f) // zero is a special case
65
int bits = Float.floatToIntBits(f); // parse float into parts
66
int mantissa = (bits & 0xffffff) >> 21;
67
int exponent = (((bits >> 24) & 0x7f) - 63) + 15;
69
if (exponent > 31) { // overflow: use max value
74
if (exponent < 0 || exponent == 0 && mantissa == 0) { // underflow: use min value
79
return (byte)((exponent << 3) | mantissa); // pack into a byte
83
public void testByteToFloat() {
84
for (int i=0; i<256; i++) {
85
float f1 = orig_byteToFloat((byte)i);
86
float f2 = SmallFloat.byteToFloat((byte)i, 3,15);
87
float f3 = SmallFloat.byte315ToFloat((byte)i);
88
assertEquals(f1,f2,0.0);
89
assertEquals(f2,f3,0.0);
91
float f4 = SmallFloat.byteToFloat((byte)i,5,2);
92
float f5 = SmallFloat.byte52ToFloat((byte)i);
93
assertEquals(f4,f5,0.0);
97
public void testFloatToByte() {
98
assertEquals(0, orig_floatToByte_v13(5.8123817E-10f)); // verify the old bug (see LUCENE-2937)
99
assertEquals(1, orig_floatToByte(5.8123817E-10f)); // verify it's fixed in this test code
100
assertEquals(1, SmallFloat.floatToByte315(5.8123817E-10f)); // verify it's fixed
102
// test some constants
103
assertEquals(0, SmallFloat.floatToByte315(0));
104
assertEquals(1, SmallFloat.floatToByte315(Float.MIN_VALUE)); // underflow rounds up to smallest positive
105
assertEquals(255, SmallFloat.floatToByte315(Float.MAX_VALUE) & 0xff); // overflow rounds down to largest positive
106
assertEquals(255, SmallFloat.floatToByte315(Float.POSITIVE_INFINITY) & 0xff);
108
// all negatives map to 0
109
assertEquals(0, SmallFloat.floatToByte315(-Float.MIN_VALUE));
110
assertEquals(0, SmallFloat.floatToByte315(-Float.MAX_VALUE));
111
assertEquals(0, SmallFloat.floatToByte315(Float.NEGATIVE_INFINITY));
114
// up iterations for more exhaustive test after changing something
115
int num = atLeast(100000);
116
for (int i = 0; i < num; i++) {
117
float f = Float.intBitsToFloat(random.nextInt());
118
if (Float.isNaN(f)) continue; // skip NaN
119
byte b1 = orig_floatToByte(f);
120
byte b2 = SmallFloat.floatToByte(f,3,15);
121
byte b3 = SmallFloat.floatToByte315(f);
125
byte b4 = SmallFloat.floatToByte(f,5,2);
126
byte b5 = SmallFloat.floatToByte52(f);
132
// Do an exhaustive test of all possible floating point values
133
// for the 315 float against the original norm encoding in Similarity.
134
// Takes 75 seconds on my Pentium4 3GHz, with Java5 -server
135
public void testAllFloats() {
136
for(int i = Integer.MIN_VALUE;;i++) {
137
float f = Float.intBitsToFloat(i);
138
if (f==f) { // skip non-numbers
139
byte b1 = orig_floatToByte(f);
140
byte b2 = SmallFloat.floatToByte315(f);
141
if (b1!=b2 || b2==0 && f>0) {
142
fail("Failed floatToByte315 for float " + f + " source bits="+Integer.toHexString(i) + " float raw bits=" + Integer.toHexString(Float.floatToRawIntBits(i)));
145
if (i==Integer.MAX_VALUE) break;