+ Reply to Thread
Results 1 to 15 of 15

Maths using Double type

  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.

    Please Login or Register  to view this content.
    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

    Please Login or Register  to view this content.

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

    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
    2003, 2010
    Posts
    40,678

    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

    Please Login or Register  to view this content.



  9. #9
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    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.
    Remember what the dormouse said
    Feed your head

  10. #10
    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!

  11. #11
    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.

  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 -
    Please Login or Register  to view this content.
    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