Here you can find the source of floatToPrefixCoded(float val)
public static String floatToPrefixCoded(float val)
//package com.java2s; /**/*from w w w .j a v a 2 s .c o m*/ * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ public class Main { /** * Expert: Integers are stored at lower precision by shifting off lower bits. The shift count is * stored as <code>SHIFT_START_INT+shift</code> in the first character */ public static final char SHIFT_START_INT = (char) 0x60; /** * Expert: The maximum term length (used for <code>char[]</code> buffer size) * for encoding <code>int</code> values. * @see #intToPrefixCoded(int,int,char[]) */ public static final int BUF_SIZE_INT = 31 / 7 + 2; /** * Convenience method: this just returns: * intToPrefixCoded(floatToSortableInt(val)) */ public static String floatToPrefixCoded(float val) { return intToPrefixCoded(floatToSortableInt(val)); } /** * Expert: Returns prefix coded bits after reducing the precision by <code>shift</code> bits. * This is method is used by {@link NumericTokenStream}. * @param val the numeric value * @param shift how many bits to strip from the right * @param buffer that will contain the encoded chars, must be at least of {@link #BUF_SIZE_INT} * length * @return number of chars written to buffer */ public static int intToPrefixCoded(final int val, final int shift, final char[] buffer) { if (shift > 31 || shift < 0) throw new IllegalArgumentException("Illegal shift value, must be 0..31"); int nChars = (31 - shift) / 7 + 1, len = nChars + 1; buffer[0] = (char) (SHIFT_START_INT + shift); int sortableBits = val ^ 0x80000000; sortableBits >>>= shift; while (nChars >= 1) { // Store 7 bits per character for good efficiency when UTF-8 encoding. // The whole number is right-justified so that lucene can prefix-encode // the terms more efficiently. buffer[nChars--] = (char) (sortableBits & 0x7f); sortableBits >>>= 7; } return len; } /** * Expert: Returns prefix coded bits after reducing the precision by <code>shift</code> bits. * This is method is used by {@link IntRangeBuilder}. * @param val the numeric value * @param shift how many bits to strip from the right */ public static String intToPrefixCoded(final int val, final int shift) { final char[] buffer = new char[BUF_SIZE_INT]; final int len = intToPrefixCoded(val, shift, buffer); return new String(buffer, 0, len); } /** * This is a convenience method, that returns prefix coded bits of an int without * reducing the precision. It can be used to store the full precision value as a * stored field in index. * <p>To decode, use {@link #prefixCodedToInt}. */ public static String intToPrefixCoded(final int val) { return intToPrefixCoded(val, 0); } /** * Converts a <code>float</code> value to a sortable signed <code>int</code>. * The value is converted by getting their IEEE 754 floating-point "float format" * bit layout and then some bits are swapped, to be able to compare the result as int. * By this the precision is not reduced, but the value can easily used as an int. * @see #sortableIntToFloat */ public static int floatToSortableInt(float val) { int f = Float.floatToRawIntBits(val); if (f < 0) f ^= 0x7fffffff; return f; } }