Java中double a=121200000*0.000001导致精度丢失如何解决
时间: 2024-03-14 08:47:06 浏览: 135
在Java中,可以使用BigDecimal类来解决double类型精度丢失的问题。BigDecimal可以精确表示任意大小和精度的十进制数值,避免了double类型的精度问题。
使用BigDecimal类来解决上述问题的代码如下:
```
BigDecimal a = new BigDecimal("121200000").multiply(new BigDecimal("0.000001"));
```
首先,将121200000和0.000001都转换成字符串表示,然后使用BigDecimal的multiply方法进行乘法运算,最终得到精确的结果。
需要注意的是,使用BigDecimal类进行数值计算时,应该尽量避免使用double类型的数值进行初始化,而是应该使用字符串或者使用BigDecimal提供的静态方法来初始化。这样可以避免在初始化时就出现精度问题。
相关问题
// Decimal hour of the day at Greenwich double greenwichtime = hour - timezone + minute / 60 + second / 3600; // Days from J2000, accurate from 1901 to 2099 double daynum = 367 * year - 7 * (year + (month + 9) / 12) / 4 + 275 * month / 9 + day - 730531.5 + greenwichtime / 24; //Mean longitude of the sun double mean_long = daynum * 0.01720279239 + 4.894967873; double mean_anom = daynum * 0.01720197034 + 6.240040768; double eclip_long = mean_long + 0.03342305518 * Math.sin(mean_anom) + 0.0003490658504 * Math.sin(2 * mean_anom); double obliquity = 0.4090877234 - 0.000000006981317008 * daynum; double rasc = Math.atan2(Math.cos(obliquity) * Math.sin(eclip_long), Math.cos(eclip_long)); double decl = Math.asin(Math.sin(obliquity) * Math.sin(eclip_long)); double sidereal = 4.894961213 + 6.300388099 * daynum + rlon; double hour_ang = sidereal - rasc; double elevation = Math.asin(Math.sin(decl) * Math.sin(rlat) + Math.cos(decl) * Math.cos(rlat) * Math.cos(hour_ang)); //Local azimuth of the sun double azimuth = Math.atan2(-Math.cos(decl) * Math.cos(rlat) * Math.sin(hour_ang), Math.sin(decl) - Math.sin(rlat) * Math.sin(elevation)); azimuth = into_range(Math.toDegrees(azimuth), 0, 360); elevation = into_range(Math.toDegrees(elevation), -180, 180);抽出常量
您可以将这些常量提取出来,并使用它们来计算方位角(azimuth)和仰角(elevation)。以下是一个示例代码:
```java
import java.util.TimeZone;
public class Main {
private static final double MEAN_LONGITUDE_OFFSET = 4.894967873;
private static final double MEAN_ANOMALY_COEFFICIENT = 0.01720197034;
private static final double ECLIPTIC_LONGITUDE_COEFFICIENT_1 = 0.03342305518;
private static final double ECLIPTIC_LONGITUDE_COEFFICIENT_2 = 0.0003490658504;
private static final double OBLIQUITY_COEFFICIENT_1 = 0.4090877234;
private static final double OBLIQUITY_COEFFICIENT_2 = 0.000000006981317008;
private static final double SIDEREAL_COEFFICIENT_1 = 4.894961213;
private static final double SIDEREAL_COEFFICIENT_2 = 6.300388099;
public static void main(String[] args) {
// 其他变量的定义和赋值
// Decimal hour of the day at Greenwich
double greenwichtime = hour - timezone + minute / 60 + second / 3600;
// Days from J2000, accurate from 1901 to 2099
double daynum = 367 * year - 7 * (year + (month + 9) / 12) / 4 + 275 * month / 9 + day - 730531.5 + greenwichtime / 24;
//Mean longitude of the sun
double mean_long = daynum * MEAN_ANOMALY_COEFFICIENT + MEAN_LONGITUDE_OFFSET;
double mean_anom = daynum * MEAN_ANOMALY_COEFFICIENT + 6.240040768;
double eclip_long = mean_long + ECLIPTIC_LONGITUDE_COEFFICIENT_1 * Math.sin(mean_anom) + ECLIPTIC_LONGITUDE_COEFFICIENT_2 * Math.sin(2 * mean_anom);
double obliquity = OBLIQUITY_COEFFICIENT_1 - OBLIQUITY_COEFFICIENT_2 * daynum;
double rasc = Math.atan2(Math.cos(obliquity) * Math.sin(eclip_long), Math.cos(eclip_long));
double decl = Math.asin(Math.sin(obliquity) * Math.sin(eclip_long));
double sidereal = SIDEREAL_COEFFICIENT_1 + SIDEREAL_COEFFICIENT_2 * daynum + rlon;
double hour_ang = sidereal - rasc;
double elevation = Math.asin(Math.sin(decl) * Math.sin(rlat) + Math.cos(decl) * Math.cos(rlat) * Math.cos(hour_ang));
//Local azimuth of the sun
double azimuth = Math.atan2(-Math.cos(decl) * Math.cos(rlat) * Math.sin(hour_ang), Math.sin(decl) - Math.sin(rlat) * Math.sin(elevation));
azimuth = into_range(Math.toDegrees(azimuth), 0, 360);
elevation = into_range(Math.toDegrees(elevation), -180, 180);
// 其他操作
}
private static double into_range(double value, double min, double max) {
double range = max - min;
return ((value - min) % range + range) % range + min;
}
}
```
这样,您可以在计算方位角和仰角时直接使用这些常量,使代码更加易读和易于维护。
Java中将121200000乘0.000001除360乘2的三一次方,给出代码运算过程以及结果
Java中将121200000乘0.000001除360乘2的三次方,可以使用Math类中的pow方法来实现。具体代码如下:
```
double result = Math.pow(121200000 * 0.000001 / (360 * 2), 3);
System.out.println(result);
```
运算过程如下:
首先计算121200000乘0.000001得到0.1212,然后除以360乘2,得到0.00016833333,再将其三次方,得到0.00000000000000465144。
最终结果为:0.00000000000000465144。
阅读全文