java 0.00变成0_Java Double值= 0.01更改为0.009999999999999787
时间: 2023-07-15 20:15:53 浏览: 93
这是由于 Java 中的浮点数精度问题导致的。在计算机中,浮点数是以二进制形式存储的,而有些十进制小数并不能精确地转换为有限的二进制小数。因此,当我们对浮点数进行计算时,可能会出现舍入误差,导致结果并不完全准确。
在你的例子中,0.01这个十进制小数无法精确地转换为有限的二进制小数,因此在计算机中存储时会产生一个近似值。当你将这个近似值赋值给一个 Double 类型的变量时,它会保留这个近似值并进行后续计算,这会导致舍入误差。
解决这个问题的方法是使用 BigDecimal 类型来进行精确计算,这种类型可以表示任意精度的十进制数。例如,你可以使用以下代码来将 0.01 转换为 BigDecimal 类型并进行计算:
```
BigDecimal a = new BigDecimal("0.01");
BigDecimal b = new BigDecimal("0.009999999999999787");
System.out.println(a.equals(b)); // 输出 true
```
这样可以避免舍入误差,得到正确的结果。
相关问题
if(frame_rx.can_id == 0x31){ geometry_msgs::Twist real_motion; int16_t vel_cms = 0; double vel_ms = 0; int16_t angle_deg_100 = 0; double angle_rad = 0; vel_cms = ((frame_rx.data[1] << 8) & 0xffff) | frame_rx.data[0]; angle_deg_100 = (((frame_rx.data[3] << 8) & 0xffff) | frame_rx.data[2]); vel_ms = (double)vel_cms / param_vc; angle_rad = (double)angle_deg_100 / 100.0 * (M_PI / 180.0); real_motion.linear.x = vel_ms; real_motion.angular.z = angle_rad; motion_pub.publish(real_motion); }
| frame_rx.data[0]) / 100; vel_ms = vel_cms / 100; angle_deg_100 = ((frame_rx.data[3] << 8)
| frame_rx.data[2]) / 100; angle_rad = angle_deg_100 * 0.01;实际运动的速度cms = ((frame_rx.data[1] << 8) | frame_rx.data[0]) / 100,速度ms = vel_cms / 100,角度deg_100 = ((frame_rx.data[3] << 8) | frame_rx.data[2]) / 100,角度rad = angle_deg_100 * 0.01。
On Error Resume Next Set acadApp = Nothing Set acadApp = GetObject(, "AutoCAD.Application") acadApp.Visible = True If Err Then MsgBox "未检测到打开的CAD图形! ", 64, Space(22) & "提 醒": Exit Sub Set cadDoc = acadApp.ActiveDocument With cadDoc .Application.WindowState = 3: Set MoSpace = .ModelSpace: Set Auti = .Utility End With Set MYSpace = cadDoc.ModelSpace Set mytxt = cadDoc.TextStyles.Add("Standard") With mytxt: .SetFont "宋体", False, False, 0, 0: .width = 1: End With '文字宽高比 cadDoc.ActiveTextStyle = mytxt Dim Gnt(2) As Double, Hnt(13) As Double, HT As Object, ot(0) As Object Gnt(0) = 0: Gnt(1) = 0: Gnt(2) = 0 Set GCBZ = cadDoc.Blocks.Add(Gnt, "*U") '匿名块高程标志 Hnt(0) = 0: Hnt(1) = 桩位直径 / 2 Hnt(2) = 0.035: Hnt(3) = 桩位直径 / 2 - 0.25 Hnt(4) = 0.01: Hnt(5) = 桩位直径 / 2 - 0.25 Hnt(6) = 0.01: Hnt(7) = 0 Hnt(8) = -0.01: Hnt(9) = 0 Hnt(10) = -0.01: Hnt(11) = 桩位直径 / 2 - 0.25 Hnt(12) = -0.035: Hnt(13) = 桩位直径 / 2 - 0.25 Set obj = MoSpace.AddLightWeightPolyline(Hnt): obj.Closed = True '闭合块 Set HT = GCBZ.AddHatch(0, "SOLID", True) '向匿名图块添加图案,0-acHatchObject,SOLID-图案(颜色)名称 Set ot(0) = obj With HT: .AppendOuterLoop (ot): .Color = 256: End With '块颜色随层(☆此句不能少!) obj.Delete 转换为VB.NET
Dim acadApp As Object = Nothing
Dim cadDoc As Object = Nothing
Dim MoSpace As Object = Nothing
Dim Auti As Object = Nothing
Dim MYSpace As Object = Nothing
Dim mytxt As Object = Nothing
Dim GCBZ As Object = Nothing
Dim obj As Object = Nothing
Dim HT As Object = Nothing
Dim ot(0) As Object
On Error Resume Next
acadApp = GetObject(, "AutoCAD.Application")
acadApp.Visible = True
If Err.Number <> 0 Then
MsgBox("未检测到打开的CAD图形! ", 64, Space(22) & "提 醒")
Exit Sub
End If
cadDoc = acadApp.ActiveDocument
With cadDoc
.Application.WindowState = 3
MoSpace = .ModelSpace
Auti = .Utility
End With
MYSpace = cadDoc.ModelSpace
mytxt = cadDoc.TextStyles.Add("Standard")
With mytxt
.SetFont("宋体", False, False, 0, 0)
.width = 1
End With
cadDoc.ActiveTextStyle = mytxt
Dim Gnt(2) As Double
Dim Hnt(13) As Double
Gnt(0) = 0
Gnt(1) = 0
Gnt(2) = 0
GCBZ = cadDoc.Blocks.Add(Gnt, "*U")
Hnt(0) = 0
Hnt(1) = 桩位直径 / 2
Hnt(2) = 0.035
Hnt(3) = 桩位直径 / 2 - 0.25
Hnt(4) = 0.01
Hnt(5) = 桩位直径 / 2 - 0.25
Hnt(6) = 0.01
Hnt(7) = 0
Hnt(8) = -0.01
Hnt(9) = 0
Hnt(10) = -0.01
Hnt(11) = 桩位直径 / 2 - 0.25
Hnt(12) = -0.035
Hnt(13) = 桩位直径 / 2 - 0.25
obj = MoSpace.AddLightWeightPolyline(Hnt)
obj.Closed = True
HT = GCBZ.AddHatch(0, "SOLID", True)
ot(0) = obj
With HT
.AppendOuterLoop(ot)
.Color = 256
End With
obj.Delete
阅读全文