001/*
002 * Java Genetic Algorithm Library (jenetics-7.1.0).
003 * Copyright (c) 2007-2022 Franz Wilhelmstötter
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 *
017 * Author:
018 *    Franz Wilhelmstötter (franz.wilhelmstoetter@gmail.com)
019 */
020package io.jenetics.internal.math;
021
022import static io.jenetics.internal.math.Basics.pow;
023
024/**
025 * Mathematical functions regarding probabilities.
026 *
027 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
028 * @since 1.4
029 * @version 5.2
030 */
031public final class Probabilities {
032        private Probabilities() {}
033
034        private static final long INT_RANGE = pow(2, 32) - 1;
035
036        /**
037         * Maps the probability, given in the range {@code [0, 1]}, to an
038         * integer in the range {@code [Integer.MIN_VALUE, Integer.MAX_VALUE]}.
039         *
040         * @see #toInt(double)
041         * @see #toFloat(int)
042         *
043         * @param probability the probability to widen.
044         * @return the widened probability.
045         */
046        public static int toInt(final float probability) {
047                return Math.round(INT_RANGE*probability + Integer.MIN_VALUE);
048        }
049
050        /**
051         * Maps the probability, given in the range {@code [0, 1]}, to an
052         * integer in the range {@code [Integer.MIN_VALUE, Integer.MAX_VALUE]}.
053         *
054         * @see #toInt(float)
055         * @see #toFloat(int)
056         *
057         * @param probability the probability to widen.
058         * @return the widened probability.
059         */
060        public static int toInt(final double probability) {
061                return (int)(Math.round(INT_RANGE*probability) + Integer.MIN_VALUE);
062        }
063
064        /**
065         * Maps the <i>integer</i> probability, within the range
066         * {@code [Integer.MIN_VALUE, Integer.MAX_VALUE]} back to a float
067         * probability within the range {@code [0, 1]}.
068         *
069         * @see #toInt(float)
070         * @see  #toInt(double)
071         *
072         * @param probability the <i>integer</i> probability to map.
073         * @return the mapped probability within the range {@code [0, 1]}.
074         */
075        public static float toFloat(final int probability) {
076                final long value = (long)probability + Integer.MAX_VALUE;
077                return (float)(value/(double)INT_RANGE);
078        }
079}