Results 1 to 7 of 7

Thread: [RESOLVED] Double appears to have different values in VB6 and C++

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2011
    Posts
    87

    Resolved [RESOLVED] Double appears to have different values in VB6 and C++

    I have a variable, dblScoreFD, which is declared in my Visual Basic 6 project as a double and then passed on to my C++ DLL and then back again

    I have had strange things happen and after a lot of digging I discover that in C++ it has the value
    1030.7899314588

    whereas when it immediately comes back to VB6 it has the value
    1030.78993145882

    The screen shots are below.

    Any help appreciated
    Bob
    .
    Attached Images Attached Images   

  2. #2
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Double appears to have different values in VB6 and C++

    There are a ton of factors that can combine to make high precision comparisons fail.

    Everything from differences in how a "double" is internally implemented, to implicit intermediate operations you aren't aware of, to differing compiler optimizations chosen, etc.

    If you want the level of precision you seem to be after scaled integers may be your best bet (VB6's Currency type or Variant Decimal subtype come to mind). Floating point arithmetic has limited precision by its nature and approximation errors accumulate.

  3. #3
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: Double appears to have different values in VB6 and C++

    more than likely it's a display issue ... if the values were truly different, you would see the larger number in C++ and the truncated one upon returning to VB6... since VB6 reports the longer number... that is what it is getting from the C++ code... which tells me that the tooltip is simply truncating the display.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Double appears to have different values in VB6 and C++

    That String compare is bizarre anyway. CStr() is locale-aware, so some users where decimal point is not "." would never see the VB code test as equal.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    May 2011
    Posts
    87

    Re: Double appears to have different values in VB6 and C++

    Quote Originally Posted by dilettante View Post
    That String compare is bizarre anyway. CStr() is locale-aware, so some users where decimal point is not "." would never see the VB code test as equal.
    Agreed! That was just a test. The nub of the problem is that both sides of the equation are revealed to have the same value by tooltip value and both are of the same type but VB6 is saying the values are NOT the same. Here is the process

    1. Find a solution and note the Score in C++
    2. In Vb6 see if this score is ALREADY present in the Pool, if not put it in
    3. Repeat the process

    I am finding duplicate entries and the reason is that it finds a solution and puts it into the Pool. It then finds the SAME solution again but when it does the comparison it refuses to accept that it is the same value but it DOES accept they are different if I convert them both to a string (like I say, just a test)

    So, it seems to me that passing the double to the DLL and back to VB6 seems to have subtle and unexpected effects at the limits of precision
    Thanks for comments
    Bob

    Edit: See Screen Shots below - how did it get past the first test?
    .
    .
    Attached Images Attached Images   
    Last edited by wavering; Jan 9th, 2013 at 04:01 AM.

  6. #6
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Double appears to have different values in VB6 and C++

    There is marshaling involved in moving the arguments between VB and C - I am doing a lot of that with VB.Net and C++ right now myself. How do you pass the parameters??

    I can't imagine the score-precision is too important - you are doing floating point math here so you have to accept rounding and mantisa display issues.

    Can you lose that digit on both sides so that you "force" the values to be the same - and thus eliminate the dups like that?

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  7. #7
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: Double appears to have different values in VB6 and C++

    Converting a double to string often loses accuracy so identical strings do not indicate identical doubles.

    Passing the double to and from the DLL will not alter the double.

    You could use your own level of accuracy for equality comparisons...
    Code:
    Const TINY As Double = 0.000000001
    
    If Abs(PageStatsFD.Score(lngRank) - dblScoreFD) < TINY Then...
    W o t . S i g

Posting Permissions

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



Click Here to Expand Forum to Full Width