Here you can find the source of doubleToBits(double in, double min, double max, int numBits, int splits)
Parameter | Description |
---|---|
in | the double value |
min | the minimum |
max | the maximum |
numBits | the number of bits |
splits | the number of splits |
public static String doubleToBits(double in, double min, double max, int numBits, int splits)
//package com.java2s; /*// w ww . j a v a2 s .com * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ public class Main { /** * Turns a double into a bit string (0s and 1s). * * @param in the double value * @param min the minimum * @param max the maximum * @param numBits the number of bits * @param splits the number of splits * @return the generated bit string */ public static String doubleToBits(double in, double min, double max, int numBits, int splits) { if (in > max) { in = max; } if (in < min) { in = min; } double sdist = (max - min) / ((double) splits - 1); double dist = in - min; double rat = dist / sdist; int split = (int) Math.round(rat); String bits = Integer.toBinaryString(split); while (bits.length() < numBits) bits = "0" + bits; return bits; } }