-2

iam using this function in my program 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

while iam giving this Value metersize =1365.28 size=1.61 the result should be 848 i test it in my calculator and in sci-calculator in google calculator and duckduckgo calculator but in vb.net it show me 847.999999 why is that .

this the Calcuatedemeter

 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

and iam gitting the Values From The DataBase Using this funcation

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
Sama PORA
  • 1
  • 2
  • i can't use round function because some time the remain will be calculated as pieces if there is remains this function was working fine in all my calculation only in this values it give me like that ma by there is more values . – Sama PORA Jul 16 '18 at 13:50
  • `SplitDouble` - what is this. `Calcuatedemeter` what is this? You're not showing us all the code – Alex Jul 16 '18 at 13:50
  • If I do `Dim result As Double = 1365.28 / 1.61` the result is 848, as expected. Something is going on in the code you're not showing us. – Alex Jul 16 '18 at 13:51
  • Hey @Alex i dont No why is That Happen here with me it give me 847.999999 not as should 848 and in my client pc the same result. i have add more of my code – Sama PORA Jul 16 '18 at 14:04
  • WHAT IS SplitDouble??? – Alex Jul 16 '18 at 14:08
  • @Alex: Try calculating `(1365.28/1.61)-848` instead. That gives me `-1.13686837721616E-13`. I suspect you are seeing a rounded result rather than the full floating point number. Taking off the integer part might reveal just the minor error. – Chris Jul 16 '18 at 14:11
  • 1
    If you need precision, then use the Decimal data type. – the_lotus Jul 16 '18 at 14:13
  • @Alex SplitDouble used to get the intger value and the floating point its built in function in vb.net , – Sama PORA Jul 16 '18 at 14:16
  • @TnTinMn iam reading about the math broken – Sama PORA Jul 16 '18 at 14:16
  • @the_lotus i will change it to Decimal and See what happen – Sama PORA Jul 16 '18 at 14:17
  • @ the_lotus it worked Fine And give me The 848 After I change it to Decimal – Sama PORA Jul 16 '18 at 14:21
  • It is worth noting that you can still get similar problems with rounding errors while using decimal. Its just that the set of errors will be slightly different. eg 1/3 is still not accurately representable as a decimal so `1/3` can still cause these kind of weird rounding things. – Chris Jul 16 '18 at 14:26

1 Answers1

0

This is due to the nature of double values, they aren't stored as exact values but instead they are stored as a non-precise approximation.

This is pretty well understand and the error assosciated (see: https://en.wikipedia.org/wiki/Machine_epsilon)

For a work around if you know you're only dealing with 2 decimal places you could use integers and store it multiplied by 100, then only divide by 100 when you want to output the value.

Expired Data
  • 668
  • 4
  • 12
  • 2
    presuming it will only ever be 2 decimal places is a pretty big leap of faith... – user2366842 Jul 16 '18 at 13:48
  • 2
    @user2366842 That's why I didn't presume, I said "IF", I was giving a conditional scenario based on the fact that the data provided was two decimal places. Might be useful might not, but I didn't say "Assuming you are dealing with 2 decimal places". In the same sense of saying "If it rains tomorrow, I'll wear a coat", I'm not presuming that it will rain tomorrow, am I? – Expired Data Jul 16 '18 at 14:12