+ Reply to Thread
Results 1 to 15 of 15

Maths using Double type

Hybrid View

  1. #1
    Registered User
    Join Date
    09-26-2011
    Location
    Dumfries, Scotland
    MS-Off Ver
    Excel 2007
    Posts
    5

    Maths using Double type

    Can anyone explain why relatively simple maths do not work in my sub when I have a variable declared as a type of Double?
    I have one declared as integer set as 3 and one declared as double set to 0.6.
    When I check if 3 * 0.2 = 0.6 I get a false... See sample sub below.

    Sub testIt()
    Dim FirstNumber As Double, SecondNumber As Integer
    FirstNumber = 0.6
    SecondNumber = 3
    If (SecondNumber * 0.2) = FirstNumber Then
        MsgBox "Worked"
    Else
        MsgBox "Not Worked >" & (SecondNumber * 0.2) & "<>" & FirstNumber & "<"
    End If
    
    End Sub
    Confused....
    Last edited by SteveCurrie; 09-27-2011 at 06:10 AM.

  2. #2
    Forum Expert Bob Phillips's Avatar
    Join Date
    09-03-2005
    Location
    Wessex
    MS-Off Ver
    Office 2003, 2010, 2013, 2016, 365
    Posts
    3,284

    Re: Maths using Double type

    t is the Floating Point processor.

    Try this to see what is happening

    Sub Test()
    Dim FirstNumber As Double, SecondNumber As Integer
        FirstNumber = 0.6
        SecondNumber = 3
        If (SecondNumber * 0.2) = FirstNumber Then
            MsgBox "Worked"
        Else
            MsgBox "Not Worked >>>" & (SecondNumber * 0.2) & "<>" & FirstNumber & "<<<"
            MsgBox (SecondNumber * 0.2) - FirstNumber
        End If
    End Sub

  3. #3
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Maths using Double type

    Welcome to the forum, Steve.

    Please take a few minutes to read the forum rules, and then edit your post to add CODE tags.

    Thanks.
    Entia non sunt multiplicanda sine necessitate

  4. #4
    Registered User
    Join Date
    09-26-2011
    Location
    Dumfries, Scotland
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Maths using Double type

    Hi Bob,

    Thanks for the extra line of code which now confuses me even more...
    Somewhere along the lines you would think maths would apply?
    My code is basically saying if (3 * 0.2) = 0.6 then... Which it does.
    Now you have me doing something even more bizaar.
    (3 * 0.2) - 0.6 = 0.. right? No according to result I am getting its 5.55111512312578E-17

    Anyone able to explain what floating point precision is and why it affects simple maths.
    Where I am sitting just now is I cannot trust Excel to do simple maths. 3 * 0.2. Simples. No?

  5. #5
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Maths using Double type

    Steve, please edit your post as requested, and all answers will be forthcoming.

  6. #6
    Forum Expert Bob Phillips's Avatar
    Join Date
    09-03-2005
    Location
    Wessex
    MS-Off Ver
    Office 2003, 2010, 2013, 2016, 365
    Posts
    3,284

    Re: Maths using Double type


  7. #7
    Forum Expert Bob Phillips's Avatar
    Join Date
    09-03-2005
    Location
    Wessex
    MS-Off Ver
    Office 2003, 2010, 2013, 2016, 365
    Posts
    3,284

    Re: Maths using Double type

    As I mentioned, this is one of the side effects of floating point arithmetic, using a binary engine for decimal calculations. This post might help with an understanding http://support.microsoft.com/kb/42980

    (admin, he has added code tags, so he is a good boy now).

  8. #8
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Maths using Double type

     
    Sub Test()
     FirstNumber = 0.6
     SecondNumber = 3
    
     If CLng(SecondNumber * 0.2) = CLng(FirstNumber) Then
      MsgBox "Worked"
     Else
      MsgBox "Not Worked >>>" & (SecondNumber * 0.2) & "<>" & FirstNumber & "<<<"
      MsgBox (SecondNumber * 0.2) - FirstNumber
     End If
    End Sub



  9. #9
    Registered User
    Join Date
    09-26-2011
    Location
    Dumfries, Scotland
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Maths using Double type

    [QUOTE=Bob Phillips;2607705]As I mentioned, this is one of the side effects of floating point arithmetic, using a binary engine for decimal calculations. This post might help with an understanding http://support.microsoft.com/kb/42980

    Hi Bob, thanks for the link which does indeed explain it very well. I shall be more careful in future when using the Double type. I always use it when really a Single will do in most cases. Old habit needs broken. Thanks for the guidance.

    Mental note... when comparing results if Double is required then I shall CStr(result) to compare values.... This works in this example but I could not understand why.. Now I do.

  10. #10
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,985

    Re: Maths using Double type

    @snb,
    How does converting to a long integer help in any way? Using your code, if you change FirstNumber to 0.7 that also "works". As does 1.2 or anything else that rounds to 1.
    Last edited by romperstomper; 09-27-2011 at 05:44 AM.
    Everyone who confuses correlation and causation ends up dead.

  11. #11
    Forum Expert Bob Phillips's Avatar
    Join Date
    09-03-2005
    Location
    Wessex
    MS-Off Ver
    Office 2003, 2010, 2013, 2016, 365
    Posts
    3,284

    Re: Maths using Double type

    Quote Originally Posted by romperstomper View Post
    @snb,
    How does converting to a long integer help in any way? Using your code, if you change FirstNumber to 0.7 that also "works". As does 1.2 or anything else that rounds to 1.
    It's called obfuscation!

  12. #12
    Forum Expert Colin Legg's Avatar
    Join Date
    03-30-2008
    Location
    UK
    MS-Off Ver
    365
    Posts
    1,256

    Re: Maths using Double type

    Hi Steve,
    when comparing results if Double is required then I shall CStr(result) to compare values.... This works in this example but I could not understand why.. Now I do.
    How does CStr(Result) help? That's definitely not the solution.

    You either need to build in a tolerance (as suggested in the article which Bob linked to), or use a different data type such as Currency.
    Hope that helps,

    Colin

    RAD Excel Blog

  13. #13
    Registered User
    Join Date
    09-26-2011
    Location
    Dumfries, Scotland
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Maths using Double type

    Hi Colin,

    I have no idea why CStr works but it does...

    If you try my orginal post you will see the msgbox on not matching provides the following output Not Worked >0.6<>0.6<.
    This means the value of SecondNumber * 0.2 = 0.6 and firstNumber also = 0.6.

    So when I cast both sides as string this matches and I do not have the fault.....

  14. #14
    Forum Expert Colin Legg's Avatar
    Join Date
    03-30-2008
    Location
    UK
    MS-Off Ver
    365
    Posts
    1,256

    Re: Maths using Double type

    Hi Steve,

    Ah okay, I see what you mean now with respect to the example you gave in your first post.

    Comparison operators can perform both numeric and string comparisons, but these comparisons can yield different results. Generally speaking, you're best off comparing numbers as numbers rather than numbers as strings. I'll give a different example just to illustrate this -
    Sub testIt()
    
        Const dblNUMBER1 As Double = 5.5
        Const dblNUMBER2 As Double = 100.5
        
        'this returns False because > is performing a numeric comparision
        Debug.Print dblNUMBER1 > dblNUMBER2
        
        'this returns True because > is performing a string comparision
        Debug.Print CStr(dblNUMBER1) > CStr(dblNUMBER2)
    
    End Sub
    So just something to watch out for...

  15. #15
    Registered User
    Join Date
    09-26-2011
    Location
    Dumfries, Scotland
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Maths using Double type

    Hi Colin,

    Yes I can see why comparing numerics as strings could be a threat but not when using equals. In the application I am using things are either an exact match or not so I am comfortable in using CStr(myval) = Cstr(testResult).

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1