001/*
002 * Java Genetic Algorithm Library (jenetics-8.0.0).
003 * Copyright (c) 2007-2024 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 java.lang.Math.abs;
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 7.2
030 */
031public final class Probabilities {
032        private Probabilities() {}
033
034        private static final long RANGE =
035                (long)Integer.MAX_VALUE - (long)Integer.MIN_VALUE;
036
037        /**
038         * Values smaller than this value are treated as zero.
039         *
040         * @since 7.2
041         */
042        public static final double EPSILON = Math.pow(10, -23);
043
044        /**
045         * Return {@code true} if the given value can be treated as probability
046         * <em>zero</em>.
047         *
048         * @since 7.2
049         *
050         * @param value the probability value to test
051         * @return {@code true} if the given value can be treated as probability 0
052         */
053        public static boolean isZero(final double value) {
054                return equals(0, value, EPSILON);
055        }
056
057        /**
058         * Return {@code true} if the given value can be treated as probability
059         * <em>one</em>.
060         *
061         * @since 7.2
062         *
063         * @param value the probability value to test
064         * @return {@code true} if the given value can be treated as probability 1
065         */
066        public static boolean isOne(final double value) {
067                return equals(1, value, EPSILON);
068        }
069
070        private static boolean
071        equals(final double a, final double b, final double delta) {
072                return abs(a - b) <= delta;
073        }
074
075        /**
076         * Maps the probability, given in the range {@code [0, 1]}, to an
077         * integer in the range {@code [Integer.MIN_VALUE, Integer.MAX_VALUE]}.
078         *
079         * @see #toFloat(int)
080         *
081         * @param probability the probability to widen.
082         * @return the widened probability.
083         */
084        public static int toInt(final double probability) {
085                return (int)(RANGE*probability + Integer.MIN_VALUE);
086        }
087
088        /**
089         * Maps the <i>integer</i> probability, within the range
090         * {@code [Integer.MIN_VALUE, Integer.MAX_VALUE]} back to a float
091         * probability within the range {@code [0, 1]}.
092         *
093         * @see  #toInt(double)
094         *
095         * @param probability the <i>integer</i> probability to map.
096         * @return the mapped probability within the range {@code [0, 1]}.
097         */
098        public static float toFloat(final int probability) {
099                final long value = (long)probability + Integer.MAX_VALUE + 1;
100                return (float)(value/(double) RANGE);
101        }
102}