「java声明Math」java声明数组的几种写法

博主:adminadmin 2023-01-21 20:15:08 216

今天给各位分享java声明Math的知识,其中也会对java声明数组的几种写法进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

java math.floor是什么意思

描述

java.lang.Math.floor(double a) 返回最大的(最接近正无穷大)double值小于或等于参数,并等于一个整数。

特殊情况:

如果参数值已经等于某个整数,那么结果是一样的参数。

如果参数是NaN或无穷大或正零或负零,那么结果是一样的参数。

声明

以下是java.lang.Math.floor()方法的声明

public static double floor(double a)

返回值

此方法返回最大的(最接近正无穷大)浮点值小于或等于参数,并等于某个整数。

实例

下面的例子说明了如何使用lang.Math.floor()方法。

package com.yiibai;

import java.lang.*;

public class MathDemo {

   public static void main(String[] args) {

      // get two double numbers

      double x = 60984.1;

      double y = -497.99;

      // call floor and print the result

      System.out.println("Math.floor(" + x + ")=" + Math.floor(x));

      System.out.println("Math.floor(" + y + ")=" + Math.floor(y));

      System.out.println("Math.floor(0)=" + Math.floor(0));

   }

}

让我们来编译和运行上面的程序,这将产生以下结果:

Math.floor(60984.1)=60984.0

Math.floor(-497.99)=-498.0

Math.floor(0)=0.0

java语言中要使用函数Math头文件应该怎样写。急~~

Math类属于java.lang包中的函数,所以在使用的过程中,不需要导入这个库,可以直接使用的。

package test;

public class promble02 {

public static void main(String[] args) {

// TODO Auto-generated method stub

double pi = Math.PI;

System.out.println(pi);

}

}

Java ME中的Math.pow()方法使用详解

使用 Java 开发移动设备应用程序时 可能需要用到特定 Java VM 所没有的数学方法 本文将专门解决 Java ME 没有 幂 方法 Math pow() 的问题 我们将演示使用三种不同的方法开发同一个 ME 应用程序 并从中选出最佳的编程解决方案

要讨论此问题 我们先考察整数和分数幂参数 将我们的分析限于正实数 我们将演示求整数问题和小数问题的解集相对而言比较容易(而不考虑指数的符号) 在大多数情况下 我们将使用示例问题 n = / 其中我们会求出 n 的良好估计或实际解 如果初始指数事先不可用 则此问题的其他解(包括牛顿法和割线法)不易编程 虽然二分法是可行的解决方案 但我们将关注传统上不为人所探究的三个方法 第一个是简单的(不过有时效率低下)几何衰变算法 而第二个方法将利用 Math sqrt() 方法并保证在不超过 次迭代中收敛到一个近似解 第三个方法将使用泰勒级数逼近法求对数并对泰勒级数进行欧拉转换

产生整数解的 ME Math pow() 方法

传统上 Java Math pow() 方法包含两个参数 这两个参数包括底数和指数 我们假定(最初)这两个参数均为整数 然后求出 ME 中与 Java 方法使用相同参数的 Math pow() 方法的可编程解 此处 可编程解相当简单 如示例 所示 在本例中 我们仅运行以指数值为指标的倍乘循环

示例

int pow( int x  int y) /*we define the power method with     base x and power y (i e  x^y)*/    {     int z = x;     for( int i =  ; i  y; i++ )z *= x;     return    }

当然 有人可能会发现需要求出非整数幂的值 正实数的简单解(无需访问 Math pow() 方法)可能涉及使用 Math log() 例如 请考虑 / 的情况 利用 / *ln( ) = 中自然对数的结果 要得到最终解 需要利用指数 (特别指出 e = ) 在这种情况下 可能不需要使用幂函数 遗憾的是 Java ME 也不支持 Math log() 方法 没有 Math pow() 或 Math log() 方法时 我们会考虑使用朴素的 强力 试探性方法 应用 Math sqrt() 方法以及自然对数(和欧拉 e)的泰勒级数逼近来求得 Java ME 问题的解

使用几何衰变算法作为强力解的 ME Math pow()

Java ME 的早期实现包括浮点主数据类型 float 和 double 最近 已添加了这些类型 现在我们将 Math pow() 声明中的整型参数替换为 double 数据类型

可能需要在 Java ME Math pow() 幂方法中使用小数指数 我们提供的生成 Math pow() 的第一种方法是使用几何衰变算法的朴素的 强力 试探性方法 简单而言 衰变算法以一个大于已知解的值开始 然后应用某个方法来衰变该值 直到该值非常逼近该解(有关简单线性衰变算法的演示 请参见示例 ) 在我们的例子中 将进一步演示向上述解收敛的几何形式

示例

/* This example illustrates a simplistic decay algorithm that we will assume     converges to our desired solution (a positive integer) */    int n; // assume that n is the solution to the number we are trying to find     int varX =  ; //assume that we know the solution is less than or equal to      while( varX    )     {     varX  =  ; // decrement by      if( varX == n)return varX;     }

在示例 中 我们从 开始递减 直到找到预期的数字 假定预期数字是一个正整数 这种类型的算法构成了强力试探性方法的基础

使用类似的方法 我们可在遇到小数时应用此算法 假定我们需要求出 n 的值 其中 n = / 要使用衰变算法 我们必须首先找到一个合适的起点 该点要等于或大于解本身 这对于带有正指数的正实数很容易做到 对于我们的示例 要对此解进行编程 对方法两边求立方 得到 n = 当然 此方程与 n = 等效 之后 我们的起始值将变为 我们知道 n 必须小于 (因为 n = ) 注意 如果限于正实数 则此推导方法同样适用于任何正指数值 现在 我们可能需要设计一个循环来产生 n 的 充分接近 预期数字的解 我们再来看示例 它适合于所有正底数和正指数

示例

double pow( double x  double y ) //we define our new power method for fractions     {     int den =  ; // specify arbitrary denominator     int num = (int)(y*den); // find numerator     int s = (num/den)+ ;     /***********************************************************************     ** Variable  s  provides the power for which we multiply the base to find     ** our starting search value  For example  if we seek a solution for     ** n =  ^( / )  then we will use  ^  or   as our starting value (which is     ** generated in our next section of code ) Why? The solution for our     ** problem (given that the base is positive) will always be less than or     ** equal to the base times the numerator power     ************************************************************************/    /***********************************************************************     ** Because we set the denominator to an arbitrary high value     ** we must attempt to reduce the fraction  In the example below     ** we find the highest allowable fraction that we can use without     ** exceeding the limitation of our primitive data types     ************************************************************************/    double z = Double MAX_VALUE;     while( z = Double MAX_VALUE )     {     den  = ; // decrement denominator     num = (int)(y*den); // find numerator     s = (num/den)+ ; // adjust starting value     // find value of our base number to the power of numerator     z = x;     for( int i =  ; i  num; i++ )z *= x;     }     /***********************************************************************     ** Now we are going to implement the decay algorithm to find     ** the value of  n     ************************************************************************/    /***********************************************************************     ** We now find  n  to the power of  s  We will then decrement  n     ** finding the value of  n  to the power of the denominator  This     ** value  variable  a  will be pared to  z  If the  a  is nearly     ** equal to  z  then we will return  n  our desired result     ************************************************************************/    double n = x; // We define  n  as our return value (estimate) for  x     // find  n  to the power of  s     for( int i =  ; i  s; i++)n *= x;     // Begin decay loop     while( n    )     {     double a = n; //proxy for n     // find  a  the value of  n  to the power of denominator     for( int i =  ; i  den; i++ )a *= n;     // pare  a  to  z  Is the value within the hundred thousandth?     // if so  return  n     double check  = a z;     double check  = z a;     if( check    || check     ) return n;     n *=  ;// We arbitrarily use a decay of  % per iteration     }     // value could not be found  return      return  ;     }

本示例演示了衰变算法的使用方法 您会注意到 n 的值(解的估计值)将按 % 强制递减 您可能需要根据编程精度要求来改变此值 也可能考虑包括编程逻辑 该逻辑用于将前一迭代解与当前迭代进行比较 然后 如果有改善继续进行迭代 但是 如果解已回归 则返回前一个值

这里讲述的解只处理正指数 如果值为负会出现什么情况呢?下面我们将解决这种意外情况

处理负指数

要再增加一层复杂度 假定正在向 Math pow() 方法传递负指数 在这种情况下 指数为负 一种简单的解决方案是将底数转换为小数 使指数为正 例如 可转换为 ( / ) 我们以可编程的方式用底数 x 来除 用 来乘 y(参见示例 )

示例

if( y    )     {     x = ( /x); // convert base number to fraction     y *=  ; // make exponent positive     }

lishixinzhi/Article/program/Java/hx/201311/26818

java为什么要将math类声明为最终类

废话,跟String类不是一样的么?就是不允许你覆盖其中的方法嘛。因为那些计算方法都是固定的,基本上永远都不会变了,所以就没有继承的必要。用的时候直接用就是了。如果你自己有你自己的特殊计算,自己另外写类吧。

关于java声明Math和java声明数组的几种写法的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。