java math类方法_Java Math类scalb()方法及示例

java math类方法

数学类scalb()方法 (Math class scalb() method)

  • scalb() method is available in java.lang package.

    scalb()方法在java.lang包中可用。

  • scalb() method is used to return the a* 2 raised to the power of scale rounded as an argument as passed in the method. Here, a is the first parameter and scale is the second parameter.

    scalb()方法用于将a * 2返还给在方法中传递的作为参数四舍五入的scale幂。 在此, a是第一个参数, scale是第二个参数。

  • scalb() method is a static method, it is accessible with the class name too.

    scalb()方法是静态方法,也可以使用类名进行访问。

  • scalb() method does not throw any exception.

    scalb()方法不会引发任何异常。

Syntax:

句法:

    public static float scalb(float a , int sf)
    public static double scalb(double a , int sf);

Parameter(s):

参数:

  • a – represents number to be scaled by the power of 2.

    a –表示要乘以2的幂的数字。

  • sf (Scale Factor) – represents number power of 2 used to scale a.

    sf (缩放因子)–表示用于缩放a的2的幂。

Return value:

返回值:

The return type of this method is float/double, – it returns the a*2 raised to the power of sf.

此方法的返回类型为float / double ,它将返回的a * 2提升为sf的幂。

Note:

注意:

  • If we pass "NaN", it returns the same value (i.e. "NaN").

    如果我们传递“ NaN”,它将返回相同的值(即“ NaN”)。

  • If we pass "Double.MIN_EXPONENT" / "Float.MIN_EXPONENT" or "Double.MAX_EXPONENT" / "Float.MAX_EXPONENT", it returns the same value.

    如果我们传递“ Double.MIN_EXPONENT” /“ Float.MIN_EXPONENT”或“ Double.MAX_EXPONENT” /“ Float.MAX_EXPONENT”,则返回相同的值。

  • If we pass "Double.MAX_EXPONENT" / "Float.MAX_EXPONENT", it returns the infinity.

    如果我们传递“ Double.MAX_EXPONENT” /“ Float.MAX_EXPONENT”,它将返回无穷大。

  • If we pass infinity as the first argument, it returns the same value with the same sign.

    如果我们将无穷大作为第一个参数传递,它将返回具有相同符号的相同值。

  • If we pass zero (0) as the first argument, it returns the same value with the same sign.

    如果我们将零(0)作为第一个参数传递,它将返回具有相同符号的相同值。

Java程序演示scalb()方法的示例 (Java program to demonstrate example of scalb() method)

// Java program to demonstrate the example of 
// scalb(double do , int sf) method of Math Class

public class ScalbMethod {
    public static void main(String[] args) {
        // declaring the variables
        double d1 = -0.0;
        double d2 = -7.0 / 0.0;
        double d3 = 20.0;
        int i = 6;

        // Here , we will get (-0.0) because we are passing 
        // parameters whose value is (-0.0,6)
        System.out.println("Math.scalb(d1,i): " + Math.scalb(d1, i));

        // Here , we will get (-Infinity) and we are passing 
        // parameters whose value is (-Infinity,6)
        System.out.println("Math.scalb(d2,i): " + Math.scalb(d2, i));

        // Here , we will get (20.0 * 2 raised to the power of 6.0) 
        // because are passing parameters whose value is (20.0,6)
        System.out.println("Math.scalb(d3,i): " + Math.scalb(d2, i));
    }
}

Output

输出量

E:\Programs>javac ScalbMethod.java
E:\Programs>java ScalbMethod
Math.scalb(d1,i): -0.0
Math.scalb(d2,i): -Infinity
Math.scalb(d3,i): -Infinity

Example 2:

范例2:

// Java program to demonstrate the example of 
// scalb(float fi , int sf) method of Math Class

public class ScalbMethod {
    public static void main(String[] args) {
        // declaring the variables
        float f1 = -0.0f;
        float f2 = -7.0f / 0.0f;
        float f3 = 20.0f;
        int i = 6;

        // Here , we will get (-0.0) because we are passing 
        // parameters whose value is (-0.0,6)
        System.out.println("Math.scalb(f1,i): " + Math.scalb(f1, i));

        // Here , we will get (-Infinity) and we are passing 
        // parameters whose value is (-Infinity,6)
        System.out.println("Math.scalb(f2,i): " + Math.scalb(f2, i));

        // Here , we will get (20.0 * 2 raised to the power of 6.0) 
        // because are passing parameters whose value is (20.0,6)
        System.out.println("Math.scalb(f3,i): " + Math.scalb(f2, i));
    }
}

Output

输出量

E:\Programs>javac ScalbMethod.java
E:\Programs>java ScalbMethod
Math.scalb(d1,i): -0.0
Math.scalb(d2,i): -Infinity
Math.scalb(d3,i): -Infinity


翻译自: https://www.includehelp.com/java/math-class-scalb-method-with-example.aspx

java math类方法

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值