「e的x次方java」E的x次方的泰勒展开式

博主:adminadmin 2023-01-27 12:21:12 313

今天给各位分享e的x次方java的知识,其中也会对E的x次方的泰勒展开式进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

java计算器纳闷写程序啊?要求有界面的哦

这是我自己前几天刚写的,希望对你有用!

import java.awt.Color;

import java.awt.FlowLayout;

import java.awt.Font;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.WindowAdapter;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JTextArea;

import javax.swing.JTextField;

class paiban12{

JFrame frame=new JFrame("计算器");

JPanel jp=new JPanel();

JPanel jp1=new JPanel();

JPanel jp2=new JPanel();

JPanel jp3=new JPanel();

JLabel jl=new JLabel("温馨提示:为了节约您宝贵的时间,请正确输入运算式,如:12+13;");

JLabel jl1=new JLabel("如要计算9的开方数,可先按9键,在按sqrt键即可");

JTextField text=new JTextField(40);

Font fnt=new Font("Serief",Font.BOLD,38);

Font fnt1=new Font("Serief",Font.BOLD,28);

Font fnt2=new Font("Serief",Font.BOLD,20);

JButton but=null;

String str="";

public paiban12(){

final JButton buta=new JButton(""+0);buta.setFont(fnt); jp.add(buta);

final JButton butb=new JButton(""+1);butb.setFont(fnt); jp.add(butb);

final JButton butc=new JButton(""+2);butc.setFont(fnt); jp.add(butc);

final JButton butd=new JButton(""+3);butd.setFont(fnt); jp.add(butd);

final JButton bute=new JButton(""+4);bute.setFont(fnt); jp.add(bute);

final JButton butf=new JButton(""+5);butf.setFont(fnt); jp.add(butf);

final JButton butg=new JButton(""+6);butg.setFont(fnt); jp.add(butg);

final JButton buth=new JButton(""+7);buth.setFont(fnt); jp.add(buth);

final JButton buti=new JButton(""+8);buti.setFont(fnt); jp.add(buti);

final JButton butj=new JButton(""+9);butj.setFont(fnt); jp.add(butj);

final JButton but=new JButton("+");jp.add(but);but.setFont(fnt);

final JButton but2=new JButton("-");jp.add(but2);but2.setFont(fnt);

final JButton but3=new JButton("×");jp.add(but3);but3.setFont(fnt);

final JButton but4=new JButton("÷");jp.add(but4);but4.setFont(fnt);

final JButton but5=new JButton("=");jp.add(but5);but5.setFont(fnt);

final JButton but6=new JButton("清空");jp.add(but6);but6.setFont(fnt1);jp2.add(but6);

final JButton but7=new JButton("sqrt");jp.add(but7);but7.setFont(fnt1);jp2.add(but7);

final JButton but8=new JButton("n次方");jp.add(but8);but8.setFont(fnt2);jp2.add(but8);

jp.setBounds(10, 100, 350, 400);

jp3.setBounds(0, 30, 450, 70);

jp3.add(jl);

jp3.add(jl1);

jp3.setLayout(new GridLayout(3,5,2,1));

frame.add(jp3);

jp.setLayout(new GridLayout(5,5,3,3));

frame.add(jp);

jp2.setBounds(380, 100, 100, 400);

jp2.setLayout(new GridLayout(3,5,3,1));

frame.add(jp2);

frame.setSize(500, 540);

frame.setBackground(Color.BLACK);

//frame.setLayout(new GridLayout(3,3));

text.setBounds(0, 0, 100, 90);

jp1.setLayout(new FlowLayout());

jp1.add(text);

jp1.setBounds(0, 0, 100, 90);

frame.add(jp1);

frame.setLocation(100, 100);

frame.setVisible(true);

but2.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

if(e.getSource()==but2){

str=str+"-";

text.setText(str);

}

}

});

but.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

if(e.getSource()==but){

str=str+"+";

text.setText(str);

}

}

});

but3.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

if(e.getSource()==but3){

str=str+"×";

text.setText(str);

}

}

});

but4.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

if(e.getSource()==but4){

str=str+"÷";

text.setText(str);

}

}

});

but5.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e){

if(e.getSource()==but5){

str=text.getText();int v=0;boolean temp;

char c[]= str.toCharArray();double i=0,j=0,sum=0;

while(true){

if(c[v]=='+'||c[v]=='-'||c[v]=='×'||c[v]=='÷'||c[v]=='0'||c[v]=='^'){

int vb=str.substring(0, v).length();

int nb=str.substring(v+1, str.length()).length();

String str2="\\d{"+vb+"}.\\d{"+nb+"}";

temp=str.matches(str2);

break;

}

v++;

};

if(temp==true){

i=Float.parseFloat(str.substring(0, v));

j=Float.parseFloat(str.substring(v+1,c.length));

if(c[v]=='+'){

sum=i+j;

}

if(c[v]=='-'){

sum=i-j;

}

if(c[v]=='×'){

sum=i*j;

}

if(c[v]=='÷'){

sum=i/j;

}

if(c[v]=='^'){

sum=Math.pow(i, j);

}

text.setText(str+"="+sum);

str="";

}

else{

text.setText("");

str="";

text.setText("温馨提示:请输入正确的计算式!!!");

}

}

}

});

buta.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

if(e.getSource()==buta){

str=str+"0";

text.setText(str);

}

}

});

butb.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

if(e.getSource()==butb){

str=str+"1";

text.setText(str);

}

}

});

butc.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

if(e.getSource()==butc){

str=str+"2";

text.setText(str);

}

}

});

butd.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

if(e.getSource()==butd){

str=str+"3";

text.setText(str);

}

}

});

bute.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

if(e.getSource()==bute){

str=str+"4";

text.setText(str);

}

}

});

butf.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

if(e.getSource()==butf){

str=str+"5";

text.setText(str);

}

}

});

butg.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

if(e.getSource()==butg){

str=str+"6";

text.setText(str);

}

}

});

buth.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

if(e.getSource()==buth){

str=str+"7";

text.setText(str);

}

}

});

buti.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

if(e.getSource()==buti){

str=str+"8";

text.setText(str);

}

}

});

butj.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

if(e.getSource()==butj){

str=str+"9";

text.setText(str);

}

}

});

but6.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

if(e.getSource()==but6){

str="";

text.setText("");

}

}

});

but7.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

if(e.getSource()==but7){

int i=(int) Float.parseFloat(str);

double sum=Math.sqrt(i);

text.setText("sqrt("+i+")="+sum);

str="";

}

}

});

but8.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

if(e.getSource()==but8){

int i=(int) Float.parseFloat(str);

str=str+'^';

text.setText(str);

}

}

});

frame.addWindowListener(new WindowAdapter() {});

}

}

class practise14{

public static void main(String args[]){

new paiban12();

}

}

java中的位运算符及其用法。

位逻辑运算符有“与”(AND)、“或”(OR)、“异或(XOR)”、“非(NOT)”,分别用“”、“|”、“^”、“~”表示。

下面的例子说明了位逻辑运算符:

// Demonstrate the bitwise logical operators.

class BitLogic {

public static void main(String args[]) {

String binary[] = {

"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111",

"1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"

};

int a = 3; // 0 + 2 + 1 or 0011 in binary

int b = 6; // 4 + 2 + 0 or 0110 in binary

int c = a | b;

int d = a b;

int e = a ^ b;

int f = (~a b) | (a ~b);

int g = ~a 0x0f;

System.out.println(" a = " + binary[a]);

System.out.println(" b = " + binary[b]);

System.out.println(" a|b = " + binary[c]);

System.out.println(" ab = " + binary[d]);

System.out.println(" a^b = " + binary[e]);

System.out.println("~ab|a~b = " + binary[f]);

System.out.println(" ~a = " + binary[g]);

}

}

在本例中,变量a与b对应位的组合代表了二进制数所有的 4 种组合模式:0-0,0-1,1-0,和1-1。“|”运算符和“”运算符分别对变量a与b各个对应位的运算得到了变量c和变量d的值。对变量e和f的赋值说明了“^”运算符的功能。字符串数组binary代表了0到15对应的二进制的值。在本例中,数组各元素的排列顺序显示了变量对应值的二进制代码。数组之所以这样构造是因为变量的值n对应的二进制代码可以被正确的存储在数组对应元素binary[n]中。例如变量a的值为3,则它的二进制代码对应地存储在数组元素binary[3]中。~a的值与数字0x0f (对应二进制为0000 1111)进行按位与运算的目的是减小~a的值,保证变量g的结果小于16。因此该程序的运行结果可以用数组binary对应的元素来表示。该程序的输出如下:

a = 0011

b = 0110

a|b = 0111

ab = 0010

a^b = 0101

~ab|a~b = 0101

~a = 1100

左移运算符

左移运算符使指定值的所有位都左移规定的次数。它的通用格式如下所示:

value num

这里,num指定要移位值value移动的位数。也就是,左移运算符使指定值的所有位都左移num位。每左移一个位,高阶位都被移出(并且丢弃),并用0填充右边。这意味着当左移的运算数是int类型时,每移动1位它的第31位就要被移出并且丢弃;当左移的运算数是long类型时,每移动1位它的第63位就要被移出并且丢弃。

在对byte和short类型的值进行移位运算时,你必须小心。因为你知道Java在对表达式求值时,将自动把这些类型扩大为 int型,而且,表达式的值也是int型 。对byte和short类型的值进行移位运算的结果是int型,而且如果左移不超过31位,原来对应各位的值也不会丢弃。但是,如果你对一个负的byte或者short类型的值进行移位运算,它被扩大为int型后,它的符号也被扩展。这样,整数值结果的高位就会被1填充。因此,为了得到正确的结果,你就要舍弃得到结果的高位。这样做的最简单办法是将结果转换为byte型。下面的程序说明了这一点:

// Left shifting a byte value.

class ByteShift {

public static void main(String args[]) {

byte a = 64, b;

int i;

i = a 2;

b = (byte) (a 2);

System.out.println("Original value of a: " + a);

System.out.println("i and b: " + i + " " + b);

}

}

该程序产生的输出下所示:

Original value of a: 64

i and b: 256 0

因变量a在赋值表达式中,故被扩大为int型,64(0100 0000)被左移两次生成值256(10000 0000)被赋给变量i。然而,经过左移后,变量b中惟一的1被移出,低位全部成了0,因此b的值也变成了0。

既然每次左移都可以使原来的操作数翻倍,程序员们经常使用这个办法来进行快速的2的乘法。但是你要小心,如果你将1移进高阶位(31或63位),那么该值将变为负值。下面的程序说明了这一点:

// Left shifting as a quick way to multiply by 2.

class MultByTwo {

public static void main(String args[]) {

int i;

int num = 0xFFFFFFE;

for(i=0; i4; i++) {

num = num 1;

System.out.println(num);

}

}

}

该程序的输出如下所示:

536870908

1073741816

2147483632

-32

初值经过仔细选择,以便在左移 4 位后,它会产生-32。正如你看到的,当1被移进31位时,数字被解释为负值。

右移运算符

右移运算符使指定值的所有位都右移规定的次数。它的通用格式如下所示:

value num

这里,num指定要移位值value移动的位数。也就是,右移运算符使指定值的所有位都右移num位。

下面的程序片段将值32右移2次,将结果8赋给变量a:

int a = 32;

a = a 2; // a now contains 8

当值中的某些位被“移出”时,这些位的值将丢弃。例如,下面的程序片段将35右移2次,它的2个低位被移出丢弃,也将结果8赋给变量a:

int a = 35;

a = a 2; // a still contains 8

用二进制表示该过程可以更清楚地看到程序的运行过程:

00100011 35

2

00001000 8

将值每右移一次,就相当于将该值除以2并且舍弃了余数。你可以利用这个特点将一个整数进行快速的2的除法。当然,你一定要确保你不会将该数原有的任何一位移出。

右移时,被移走的最高位(最左边的位)由原来最高位的数字补充。例如,如果要移走的值为负数,每一次右移都在左边补1,如果要移走的值为正数,每一次右移都在左边补0,这叫做符号位扩展(保留符号位)(sign extension),在进行右移操作时用来保持负数的符号。例如,–8 1 是–4,用二进制表示如下:

11111000 –8

1

11111100 –4

一个要注意的有趣问题是,由于符号位扩展(保留符号位)每次都会在高位补1,因此-1右移的结果总是–1。有时你不希望在右移时保留符号。例如,下面的例子将一个byte型的值转换为用十六进制表示。注意右移后的值与0x0f进行按位与运算,这样可以舍弃任何的符号位扩展,以便得到的值可以作为定义数组的下标,从而得到对应数组元素代表的十六进制字符。

// Masking sign extension.

class HexByte {

static public void main(String args[]) {

char hex[] = {

'0', '1', '2', '3', '4', '5', '6', '7',

'8', '9', 'a', 'b', 'c', 'd', 'e', 'f''

};

byte b = (byte) 0xf1;

System.out.println("b = 0x" + hex[(b 4) 0x0f] + hex[b 0x0f]);

}

}

该程序的输出如下:

b = 0xf1

无符号右移

正如上面刚刚看到的,每一次右移,运算符总是自动地用它的先前最高位的内容补它的最高位。这样做保留了原值的符号。但有时这并不是我们想要的。例如,如果你进行移位操作的运算数不是数字值,你就不希望进行符号位扩展(保留符号位)。当你处理像素值或图形时,这种情况是相当普遍的。在这种情况下,不管运算数的初值是什么,你希望移位后总是在高位(最左边)补0。这就是人们所说的无符号移动(unsigned shift)。这时你可以使用Java的无符号右移运算符,它总是在左边补0。下面的程序段说明了无符号右移运算符。在本例中,变量a被赋值为-1,用二进制表示就是32位全是1。这个值然后被无符号右移24位,当然它忽略了符号位扩展,在它的左边总是补0。这样得到的值255被赋给变量a。

int a = -1;

a = a 24;

下面用二进制形式进一步说明该操作:

11111111 11111111 11111111 11111111 int型- 1的二进制代码

24 无符号右移24位

00000000 00000000 00000000 11111111 int型255的二进制代码由于无符号右移运算符只是对32位和64位的值有意义,所以它并不像你想象的那样有用。因为你要记住,在表达式中过小的值总是被自动扩大为int型。这意味着符号位扩展和移动总是发生在32位而不是8位或16位。这样,对第7位以0开始的byte型的值进行无符号移动是不可能的,因为在实际移动运算时,是对扩大后的32位值进行操作。下面的例子说明了这一点:

// Unsigned shifting a byte value.

class ByteUShift {

static public void main(String args[]) {

char hex[] = {

'0', '1', '2', '3', '4', '5', '6', '7',

'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'

};

byte b = (byte) 0xf1;

byte c = (byte) (b 4);

byte d = (byte) (b 4);

byte e = (byte) ((b 0xff) 4);

System.out.println(" b = 0x"

+ hex[(b 4) 0x0f] + hex[b 0x0f]);

System.out.println(" b 4 = 0x"

+ hex[(c 4) 0x0f] + hex[c 0x0f]);

System.out.println(" b 4 = 0x"

+ hex[(d 4) 0x0f] + hex[d 0x0f]);

System.out.println("( b 0xff) 4 = 0x"

+ hex[(e 4) 0x0f] + hex[e 0x0f]);

}

}

该程序的输出显示了无符号右移运算符对byte型值处理时,实际上不是对byte型值直接操作,而是将其扩大到int型后再处理。在本例中变量b被赋为任意的负byte型值。对变量b右移4位后转换为byte型,将得到的值赋给变量c,因为有符号位扩展,所以该值为0xff。对变量b进行无符号右移4位操作后转换为byte型,将得到的值赋给变量d,你可能期望该值是0x0f,但实际上它是0xff,因为在移动之前变量b就被扩展为int型,已经有符号扩展位。最后一个表达式将变量b的值通过按位与运算将其变为8位,然后右移4位,然后将得到的值赋给变量e,这次得到了预想的结果0x0f。由于对变量d(它的值已经是0xff)进行按位与运算后的符号位的状态已经明了,所以注意,对变量d再没有进行无符号右移运算。

B = 0xf1

b 4 = 0xff

b 4 = 0xff

(b 0xff) 4 = 0x0f

位运算符赋值

所有的二进制位运算符都有一种将赋值与位运算组合在一起的简写形式。例如,下面两个语句都是将变量a右移4位后赋给a:

a = a 4;

a = 4;

同样,下面两个语句都是将表达式a OR b运算后的结果赋给a:

a = a | b;

a |= b;

下面的程序定义了几个int型变量,然后运用位赋值简写的形式将运算后的值赋给相应的变量:

class OpBitEquals {

public static void main(String args[]) {

int a = 1;

int b = 2;

int c = 3;

a |= 4;

b = 1;

c = 1;

a ^= c;

System.out.println("a = " + a);

System.out.println("b = " + b);

System.out.println("c = " + c);

}

}

该程序的输出如下所示:

a = 3

b = 1

c = 6

用Java语言编程,计算数学常数e的值,e=1+1/1!+1/2!+1/3!+.....。

package e;

public class Main { public static void main(String[] args) {

double e=1,sum=1; //e的初值为1,sum用来存放n!

int i=1;

while(sumMath.pow(10, 1000000)){ //当sum大于10的1000000次方的时候我们认为已近似的

sum=i*sum; 相等了,如果这个数设置的更大就会更加接近e

e=1.0/sum+e;

i++;

}

System.out.println("e="+e);

}} 程序是经验证的,可以在编译环境上运行!绝对没问题~

C语言中abs,fabs,pow,exp分别指什么?具体如何使用?

abs(x):整数x的绝对值。

fabs(x):浮点数(小数)x的绝对值。

pow(a, x):a的x次方,a和x是浮点数,返回值是浮点数(即使a和x都是整数,也会被转换成浮点数,因此整数运算可能损失精度,造成误差)。

exp(x):e的x次方,x是浮点数,e是自然对数的底数(一个无理数,值为2.71828....)

关于e的x次方java和E的x次方的泰勒展开式的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。