Is number A approximate number B. - Java java.lang

Java examples for java.lang:Math Number

Description

Is number A approximate number B.

Demo Code

/*/*from   w w  w .j av a  2s. c om*/
 *     Copyright 2016-2026 TinyZ
 *
 * Licensed 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.
 */
//package com.java2s;

public class Main {
    /**
     * Is number A approximate number B.
     *
     * @param a       The number a
     * @param b       The number b
     * @param percent The similar percent
     * @return Return true if the number a is approximate number b. otherwise false.
     */
    public static boolean approximate(double a, double b, double percent) {
        if (percent < 0 || percent > 1) {
            throw new IllegalStateException("The param percent is error");
        }
        return (a * percent <= b) && (a * (2 - percent) <= b);
    }
}

Related Tutorials