「java减法abs」java减法函数
本篇文章给大家谈谈java减法abs,以及java减法函数对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、java的加减乘除运算
- 2、在Java中什么意思 Math.abs(x)及同类的的公式
- 3、java中如何进行减法运算
- 4、JAVA的加,减,乘,除运算方法
- 5、Java编程,计算复数的加减乘除和比较大小
- 6、怎么用java编程,实现分数的加减乘除运算?
java的加减乘除运算
使用BigDecimal并且一定要用String来够造。
实现方法如下:
import java.math.BigDecimal;
/**
* 由于Java的简单类型不能够精确的对浮点数进行运算,这个工具类提供精
* 确的浮点数运算,包括加减乘除和四舍五入。
*/
public class Arith{
//默认除法运算精度
private static final int DEF_DIV_SCALE = 10;
//这个类不能实例化
private Arith(){
}
/**
* 提供精确的加法运算。
* @param v1 被加数
* @param v2 加数
* @return 两个参数的和
*/
public static double add(double v1,double v2){
BigDecimal b1 = new BigDecimal(Double.toString(v1));
BigDecimal b2 = new BigDecimal(Double.toString(v2));
return b1.add(b2).doubleValue();
}
/**
* 提供精确的减法运算。
* @param v1 被减数
* @param v2 减数
* @return 两个参数的差
*/
public static double sub(double v1,double v2){
BigDecimal b1 = new BigDecimal(Double.toString(v1));
BigDecimal b2 = new BigDecimal(Double.toString(v2));
return b1.subtract(b2).doubleValue();
}
/**
* 提供精确的乘法运算。
* @param v1 被乘数
* @param v2 乘数
* @return 两个参数的积
*/
public static double mul(double v1,double v2){
BigDecimal b1 = new BigDecimal(Double.toString(v1));
BigDecimal b2 = new BigDecimal(Double.toString(v2));
return b1.multiply(b2).doubleValue();
}
/**
* 提供(相对)精确的除法运算,当发生除不尽的情况时,精确到
* 小数点以后10位,以后的数字四舍五入。
* @param v1 被除数
* @param v2 除数
* @return 两个参数的商
*/
public static double div(double v1,double v2){
return div(v1,v2,DEF_DIV_SCALE);
}
/**
* 提供(相对)精确的除法运算。当发生除不尽的情况时,由scale参数指
* 定精度,以后的数字四舍五入。
* @param v1 被除数
* @param v2 除数
* @param scale 表示表示需要精确到小数点以后几位。
* @return 两个参数的商
*/
public static double div(double v1,double v2,int scale){
if(scale0){
throw new IllegalArgumentException(
"The scale must be a positive integer or zero");
}
BigDecimal b1 = new BigDecimal(Double.toString(v1));
BigDecimal b2 = new BigDecimal(Double.toString(v2));
return b1.divide(b2,scale,BigDecimal.ROUND_HALF_UP).doubleValue();
}
/**
* 提供精确的小数位四舍五入处理。
* @param v 需要四舍五入的数字
* @param scale 小数点后保留几位
* @return 四舍五入后的结果
*/
public static double round(double v,int scale){
if(scale0){
throw new IllegalArgumentException(
"The scale must be a positive integer or zero");
}
BigDecimal b = new BigDecimal(Double.toString(v));
BigDecimal one = new BigDecimal("1");
return b.divide(one,scale,BigDecimal.ROUND_HALF_UP).doubleValue();
}
};
在Java中什么意思 Math.abs(x)及同类的的公式
该方法返回x的绝对值,x的取值可以是各种类型参数。
Math.abs(x)=|x|;如果参数是非负数,则返回该参数。如果参数是负数,则返回该参数的相反数。
特殊情况是:
如果参数是正零或负零,那么结果是正零。
如果参数是无穷大,那么结果是正无穷大。
如果参数是 NaN,那么结果就是 NaN。
NAN:
NaN,是Not a Number的缩写。
NaN 用于处理计算中出现的错误情况,比如 0.0 除以 0.0 或者求负数的平方根。对于单精度浮点数,NaN 表示为指数为 emax + 1 = 128(指数域全为 1),且尾数域不等于零的浮点数。
EEE 标准没有要求具体的尾数域,所以 NaN 实际上不是一个,而是一族。不同的实现可以自由选择尾数域的值来表达 NaN。
比如 Java 中的常量 Float.NaN 的浮点数可能表达为 011111111100000000000000,其中尾数域的第一位为 1,其余均为 0(不计隐藏的一位)。
但这取决系统的硬件架构。Java 中甚至允许程序员自己构造具有特定位模式的 NaN 值(通过 Float.intBitsToFloat() 方法)。
比如,程序员可以利用这种定制的 NaN 值中的特定位模式来表达某些诊断信息。
扩展资料
java中math提供用于执行任意精度整数算法 和任意精度小数算法 的类。
同类公式:
java Math类常用的方法:
圆周率:Math.PI
自然对数:Math.E
绝对值:Math.abs
向上取整数:Math.ceil;
向下取整数:Math.floor;
java中如何进行减法运算
setText(""+number1-number2);
因为java是从左到右处理的,在减法运算前有""字符串处理, java就会把之后的都当做字符串处理.改成
setText(""+(number1-number2));
如果减法运算在前面,比如 number1-number2 + "".这样是可以的
JAVA的加,减,乘,除运算方法
首先可以把计算器看成是一个对象就是一个类,它有加、减、乘、除功能,这四个就是这个类的方法;你可以给这个类定义两个成员变量
int
x、int
y
然后分别用这四个方法对x
、y
实行加、减、乘、除并返回其值
代码大概如下:
class
counter{
private
int
x;
private
int
y;
public
counter(){
}
public
counter(int
x,int
y){
this.x=x;
this.y=y;
}
public
double
adding(){
//加运算
return
x+y;
}
public
double
minus(){
//减运算
return
x-y;
}
public
double
times(){
//乘运算
return
x*y;
}
public
double
divide(){
//除运算
return
x/y;
}
}
//测试类
public
class
test{
public
static
void
main(string[]
args){
counter
c=new
counter(5,4);//实例化
system.out.println(c.adding());//输出加的结果
system.out.println(c.minus());//输出减的结果
system.out.println(c.times());//输出乘的结果
system.out.println(c.divide());//输出除的结果
}
}
希望对你有帮助
Java编程,计算复数的加减乘除和比较大小
首先复数是不能比较大小的。
以下是一个复数类,希望能帮助你
public class Complex
{
private final double re; // the real part
private final double im; // the imaginary part
// create a new object with the given real and imaginary parts
public Complex(double real, double imag)
{
re = real;
im = imag;
}
// return a string representation of the invoking Complex object
public String toString() {
if (im == 0) return re + "";
if (re == 0) return im + "i";
if (im 0) return re + " - " + (-im) + "i";
return re + " + " + im + "i";
}
// return abs/modulus/magnitude and angle/phase/argument
public double abs() { return Math.hypot(re, im); } // Math.sqrt(re*re + im*im)
public double phase() { return Math.atan2(im, re); } // between -pi and pi
// return a new Complex object whose value is (this + b)
public Complex plus(Complex b) {
Complex a = this; // invoking object
double real = a.re + b.re;
double imag = a.im + b.im;
return new Complex(real, imag);
}
/**
* return a new Complex object whose value is (this - b)
* 减法
* @param b
* @return
*/
public Complex minus(Complex b) {
Complex a = this;
double real = a.re - b.re;
double imag = a.im - b.im;
return new Complex(real, imag);
}
/**
* return a new Complex object whose value is (this * b)
* 乘以一个复数
* @param b 被乘的复数
* @return
*/
public Complex times(Complex b) {
Complex a = this;
double real = a.re * b.re - a.im * b.im;
double imag = a.re * b.im + a.im * b.re;
return new Complex(real, imag);
}
/**
*scalar multiplication
* return a new object whose value is (this * alpha)/br
* 乘以一个实数
*/
public Complex times(double alpha) {
return new Complex(alpha * re, alpha * im);
}
/**
* return a new Complex object whose value is the conjugate of this/br
* 共轭复数
* @return
*/
public Complex conjugate() { return new Complex(re, -im); }
/**
* return a new Complex object whose value is the reciprocal of this/br
* 倒数 a +bi 的倒数 /br
* a -bi/br
* —————————————/br
* a^2 + b ^2/br
*/
public Complex reciprocal()
{
double scale = re*re + im*im;
return new Complex(re / scale, -im / scale);
}
/**
* return the real part
* @return
*/
public double re() { return re; }
/**
* imaginary part
* @return
*/
public double im() { return im; }
/**
* return a / b
*/
public Complex divides(Complex b)
{
Complex a = this;
return a.times(b.reciprocal());
}
// return a new Complex object whose value is the complex exponential of this
public Complex exp()
{
return new Complex(Math.exp(re) * Math.cos(im), Math.exp(re) * Math.sin(im));
}
// return a new Complex object whose value is the complex sine of this
public Complex sin()
{
return new Complex(Math.sin(re) * Math.cosh(im), Math.cos(re) * Math.sinh(im));
}
// return a new Complex object whose value is the complex cosine of this
public Complex cos()
{
return new Complex(Math.cos(re) * Math.cosh(im), -Math.sin(re) * Math.sinh(im));
}
// return a new Complex object whose value is the complex tangent of this
public Complex tan()
{
return sin().divides(cos());
}
// a static version of plus
public static Complex plus(Complex a, Complex b)
{
double real = a.re + b.re;
double imag = a.im + b.im;
Complex sum = new Complex(real, imag);
return sum;
}
// sample client for testing
public static void main(String[] args) {
Complex a = new Complex(5.0, 6.0);
Complex b = new Complex(-3.0, 4.0);
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("Re(a) = " + a.re());
System.out.println("Im(a) = " + a.im());
System.out.println("b + a = " + b.plus(a));
System.out.println("a - b = " + a.minus(b));
System.out.println("a * b = " + a.times(b));
System.out.println("b * a = " + b.times(a));
System.out.println("a / b = " + a.divides(b));
System.out.println("(a / b) * b = " + a.divides(b).times(b));
System.out.println("conj(a) = " + a.conjugate());
System.out.println("|a| = " + a.abs());
System.out.println("tan(a) = " + a.tan());
}
}
怎么用java编程,实现分数的加减乘除运算?
java编程实现分数的加减乘除运算的步骤如下:
1、打开eclipse,创建一个Java工程,在此工程里新建一个类;
2、在新建的类中,添加4个运算类;
3、在主方法中调用对应的方法即可完成分数的加减乘除运算了。
具体实现代码如下:
public class Demo {
public static void main(String[] args) {
System.out.println(jia(1, 2));
System.out.println(jian(1, 2));
System.out.println(cheng(1, 2));
System.out.println(chu(1, 2));
}
//加法运算
private static float jia(float x,float y) {
return x + y;
}
//减法运算
private static float jian(float x,float y) {
return x - y;
}
//乘法运算
private static float cheng(float x,float y) {
return x * y;
}
//除法运算
private static float chu(float x,float y) {
return x / y;
}
}
java减法abs的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java减法函数、java减法abs的信息别忘了在本站进行查找喔。
发布于:2022-11-22,除非注明,否则均为
原创文章,转载请注明出处。