導航:首頁 > 編程語言 > java求x的y次方

java求x的y次方

發布時間:2021-03-16 23:54:43

java中怎樣怎樣算出求函數的冪次方

java中通常進行數學運算的東西都在Math類中,求函數的冪次方就是Math類中的pow方法:public static double pow(doublea,doubleb),返回第一個參數的第二個參數次冪的值。

例如求2的3次方,代碼如下:

public class test {

public static void main(String[] args) {

double a= Math.pow(2, 3);

}

}

運行結果為8

(1)java求x的y次方擴展閱讀:

Math 類包含用於執行基本數學運算的方法,如初等指數、對數、平方根和三角函數。

與 StrictMath 類的某些數學方法不同,並非 Math 類所有等價函數的實現都定義為返回逐位相同的結果。此類在不需要嚴格重復的地方可以得到更好的執行。

默認情況下,很多 Math 方法僅調用 StrictMath 中的等價方法來完成它們的實現。建議代碼生成器使用特定於平台的本機庫或者微處理器指令(可用時)來提供 Math 方法更高性能的實現。這種更高性能的實現仍然必須遵守 Math 的規范。

實現規范的質量涉及到兩種屬性,即返回結果的准確性和方法的單調性。浮點 Math 方法的准確性根據 ulp(units in the last place,最後一位的進退位)來衡量。對於給定的浮點格式,特定實數值的 ulp 是包括該數值的兩個浮點值的差。當作為一個整體而不是針對具體參數討論方法的准確性時,引入的 ulp 數用於任何參數最差情況下的誤差。

如果一個方法的誤差總是小於 0.5 ulp,那麼該方法始終返回最接近准確結果的浮點數;這種方法就是正確舍入。一個正確舍入的方法通常能得到最佳的浮點近似值;然而,對於許多浮點方法,進行正確舍入有些不切實際。

相反,對於Math 類,某些方法允許誤差在 1 或 2 ulp 的范圍內。非正式地,對於 1 ulp的誤差范圍,當准確結果是可表示的數值時,應該按照計算結果返回准確結果;否則,返回包括准確結果的兩個浮點值中的一個。對於值很大的准確結果,括弧的一端可以是無窮大。除了個別參數的准確性之外,維護不同參數的方法之間的正確關系也很重要。

因此,大多數誤差大於 0.5 ulp 的方法都要求是半單調的:只要數學函數是非遞減的,浮點近似值就是非遞減的;同樣,只要數學函數是非遞增的,浮點近似值就是非遞增的。並非所有準確性為 1
ulp 的近似值都能自動滿足單調性要求。

Oracle官方API介面-Java™ Platform, Standard Edition 7 API Specification


Ⅱ 用java語言:編程輸入兩個數整數想x,y,求x的y次冪。

Math.Pow(x,y)不是整數也支持

Ⅲ Java 用遞歸求x的n次方 輸入參數

importjava.util.Scanner;


publicclassTest{
publicstaticvoidmain(String[]args){
Scannersc=newScanner(System.in);
System.out.println("輸入x:");
intx=sc.nextInt();
System.out.println("輸入n:");
intn=sc.nextInt();
System.out.println(function(x,n));

}
publicstaticintfunction(intx,intn){
if(n>0){
returnx*function(x,n-1);
}
return1;
}
}

Ⅳ java 問題 math.pow(x,y)不是x的y次方嗎 為什麼算圓的面積這么寫呢謝謝

3.14159*Math.pow(radius,2);

Ⅳ 求JAVA編寫的計算器,用AWT編寫,且可以算x的y次方,x開y次根號

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class Counter extends WindowAdapter
{
static JFrame f=new JFrame("計算器");
static JTextField text1=new JTextField("0.");
static String source="";
static String cal="";
static String object="";
static boolean flag=false;
static boolean flag1=true;
static boolean flag2=false;
public void init()
{
try
{
Container c=f.getContentPane();
JPanel pan1=new JPanel();
JButton b1=new JButton("1");
JButton b2=new JButton("2");
JButton b3=new JButton("3");
JButton b4=new JButton("4");
JButton b5=new JButton("5");
JButton b6=new JButton("6");
JButton b7=new JButton("7");
JButton b8=new JButton("8");
JButton b9=new JButton("9");
JButton b0=new JButton("0");
JButton b11=new JButton("+");
JButton b12=new JButton("-");
JButton b13=new JButton("*");
JButton b14=new JButton("/");
JButton b15=new JButton(".");
JButton b16=new JButton("=");
JButton bclar=new JButton("清零");
text1.setHorizontalAlignment(JTextField.RIGHT);
c.add(text1,"North");
c.add(pan1);
A aa=new A();
Result re=new Result();
Opertion op=new Opertion();
Clar cl=new Clar();
b1.addActionListener(aa);
b2.addActionListener(aa);
b3.addActionListener(aa);
b4.addActionListener(aa);
b5.addActionListener(aa);
b6.addActionListener(aa);
b7.addActionListener(aa);
b8.addActionListener(aa);
b9.addActionListener(aa);
b0.addActionListener(aa);
b11.addActionListener(op);
b12.addActionListener(op);
b13.addActionListener(op);
b14.addActionListener(op);
b16.addActionListener(re);
b15.addActionListener(aa);
bclar.addActionListener(cl);
pan1.add(b1);
pan1.add(b2);
pan1.add(b3);
pan1.add(b11);
pan1.add(b4);
pan1.add(b5);
pan1.add(b6);
pan1.add(b12);
pan1.add(b7);
pan1.add(b8);
pan1.add(b9);
pan1.add(b13);
pan1.add(b0);
pan1.add(b15);
pan1.add(b16);
pan1.add(b14);
pan1.add(bclar);
f.setSize(200,220);
f.setVisible(true);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}

}
class A implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String a=text1.getText();
String s=e.getActionCommand();
if(a.equals("0.")||a.equals("+")||a.equals("-")||a.equals("*")||a.equals("/"))
text1.setText(s);
else {
if(flag2)
{
text1.setText(s);
flag2=false;
}
else
text1.setText(a+s);

}
}
}
class Opertion implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
cal=e.getActionCommand();
if(flag1==true)
source=text1.getText();
text1.setText(cal);
flag1=false;
flag=true;
}
}
class Result implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
double num1;
num1=Double.parseDouble(source);
object=text1.getText();
double num2;
num2=Double.parseDouble(object);
double result=0;
if(cal.equals("+"))
result=num1+num2;
if(cal.equals("-"))
result=num1-num2;
if(cal.equals("*"))
result=num1*num2;
if(cal.equals("/"))
if(num2==0)
text1.setText("除數不能為0");
else
result=num1/num2;
String s1=Double.toString(result);
text1.setText(s1);
flag1=true;
flag2=true;
}
}
class Clar implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
text1.setText("0.");
}
}

public static void main(String[] args)
{
Counter count=new Counter();
count.init();
}

public void windowClosing(WindowEvent e){
System.exit(1);
}
public void windowOpened(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
public void windowClosed(WindowEvent e){}
public void windowActivated(WindowEvent e){}
public void windowDeactivated(WindowEvent e){}
}

Ⅵ java:編寫程序,求x的y次方,x為double型,y為int型

例如:
double x=1.2;
int y=2;
System.out.println(Math.pow(x, y));
返回值是double型的

其實這個查下API就可以知道的

Ⅶ java里如何寫x乘y的n次方

使用Math.pow方法

Math.pow(x*y,n);//表示x乘以y後的n次方

補充:
1、定義
1)public static double pow(double a, double b)

Math.pow() 方法包含兩個參數。這兩個參數包括底數和指數
2)參數
a -- 底數.
b -- 指數
3)返回值
此方法返回 ab.

Ⅷ java 例如y的n次冪等於x,在知道x和y情況下如何求n

package com.demo;

public class demo {

public static void main(String[] args) {
System.out.println(new demo().getLog(100, 10));
}

public double getLog(double x,double y)
{
return Math.log(x)/Math.log(y);
}

}

Ⅸ 麻煩哪位JAVA編程高手幫我看看,這個計算器中的X的Y次方怎麼做 怎麼把科學和標准型分開 ,選擇

僅供參考。

Ⅹ 編寫java循環程序,從鍵盤輸入一個雙精度數x和一個整數y,使用循環語句,求x的y次方

double result =0;
double x= 0;
for(int i=1;i<=y)
{
result = result*x;
}

閱讀全文

與java求x的y次方相關的資料

熱點內容
網路中常用的傳輸介質 瀏覽:518
文件如何使用 瀏覽:322
同步推密碼找回 瀏覽:865
樂高怎麼才能用電腦編程序 瀏覽:65
本機qq文件為什麼找不到 瀏覽:264
安卓qq空間免升級 瀏覽:490
linux如何刪除模塊驅動程序 瀏覽:193
at89c51c程序 瀏覽:329
怎麼創建word大綱文件 瀏覽:622
裊裊朗誦文件生成器 瀏覽:626
1054件文件是多少gb 瀏覽:371
高州禁養區內能養豬多少頭的文件 瀏覽:927
win8ico文件 瀏覽:949
仁和數控怎麼編程 瀏覽:381
項目文件夾圖片 瀏覽:87
怎麼在東芝電視安裝app 瀏覽:954
plc顯示數字怎麼編程 瀏覽:439
如何辨別假網站 瀏覽:711
寬頻用別人的賬號密碼 瀏覽:556
新app如何佔有市場 瀏覽:42

友情鏈接