vb.net - 在 vb.net 中除以两倍是错误的
问题描述
我在我的程序 vb.net 中使用这个函数
Public Function getmeter(ByVal metersize As Double) As Double
Dim result As Double
Dim remain As Double = metersize / size
SplitDouble(remain)
result = Calcuatedemeter(cartons, plateNum)
Return result
End Function
虽然我给出了这个值,metersize =1365.28 size=1.61 结果应该是 848 我在我的计算器和谷歌计算器和duckduckgo计算器的 sci-calculator 中测试它,但在 vb.net 中它告诉我 847.999999 为什么会这样。
这个计算器
Private Function Calcuatedemeter(ByVal cartonnumber As Integer, ByVal platenumber As Integer) As String
Dim result As String = ""
Dim sb As New System.Text.StringBuilder()
If cartonnumber > 0 Then
sb.Append(cartonnumber)
sb.Append(" Carton ")
End If
If Not platenumber = 0 And cartonnumber > 0 Then
sb.Append(" و ")
End If
If platenumber > 0 Then
sb.Append(" " & platenumber & " Peace ")
End If
result = sb.ToString
Return result
End Function
我正在使用这个函数从数据库中获取值
Function getInfoceramic(ByVal gsize As Double, ByVal gcarton As Integer, ByVal gname As String, ByVal gwidth As Double, ByVal glength As Double) As Double
size = CDbl(gsize)
cartonpak = gcarton
name = gname
width = gwidth
length = glength
platesize = (width / 100) * (length / 100)
End Function
解决方案
这是由于双精度值的性质,它们不存储为精确值,而是存储为非精确近似值。
这很好理解并且错误相关(参见:https ://en.wikipedia.org/wiki/Machine_epsilon )
如果您知道您只处理 2 个小数位,则可以使用整数并将其乘以 100 存储,然后仅在您想要输出值时除以 100。
推荐阅读
- php - 如何在不破坏代码的情况下将 isset 添加到 $_GET
- arrays - 斯威夫特我不能在同一个数组上附加两个不同的字符串
- python - 每当另一个变量发生变化时,如何在课堂上更改一个变量?Python
- javascript - Three.js,自定义着色器,填充png纹理和设置透明度?
- c# - 使用反射从属性中获取
- testing - TestCafe 无法在测试运行之外使用 testController (t)(例如,作为跳过测试的条件)
- reactjs - 在常规 JavaScript 中传递 React 状态(头盔)
- actionscript-3 - 在 Workers > Performance Hit 之间传递 20,000 个对象?
- r - 有没有办法使用颜色和阴影来绘制堆积条形图来区分类别?
- elixir - 使用“eval”开始混合发布时无法连接到 libcluster