dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: vb6 will and VC98 C++ won't

  1. #1
    Join Date
    Dec 2019
    Posts
    23

    vb6 will and VC98 C++ won't

    I have encountered something that I cannot find a solution for. C++ won't process 33.3333 * 3600 but vb6 will. All variables double. Any ideas?

    Ron

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,561

    Re: vb6 will and VC98 C++ won't

    Let's see your code. What do you mean "won't process"?

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,624

    Re: vb6 will and VC98 C++ won't

    Quote Originally Posted by OldRon View Post
    I have encountered something that I cannot find a solution for. C++ won't process 33.3333 * 3600 but vb6 will. All variables double. Any ideas?

    Ron
    What solution did you expect and what (if any) did you get?
    Victor Nijegorodov

  4. #4
    Join Date
    Dec 2019
    Posts
    23

    Re: vb6 will and VC98 C++ won't

    There haven't been any solutions offered. As for what I was expecting an explanation or correction would be nice. Here's a clue: 33.333 * 3600 will resolve but 33.3333 * 3600 won't resolve.

  5. #5
    Join Date
    Dec 2019
    Posts
    23

    Re: vb6 will and VC98 C++ won't

    The equation resolves prior to being subjected to my rookie code.

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,561

    Re: vb6 will and VC98 C++ won't

    If you want help you should answer questions that are asked

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,248

    Re: vb6 will and VC98 C++ won't

    Quote Originally Posted by OldRon View Post
    The equation resolves prior to being subjected to my rookie code.
    Folks here are glad to help but you have to respond to what has been asked. Like posting the code that works in vb with the inputs and desired output and the same in c++. What variable types you choose will make a big difference, so help us out by posting the code.

  8. #8
    Join Date
    Dec 2019
    Posts
    23

    Re: vb6 will and VC98 C++ won't

    Arjay,

    When I realized that I was the sole owner of the problem then I did not want to waste people's time.

  9. #9
    Join Date
    Feb 2017
    Posts
    514

    Re: vb6 will and VC98 C++ won't

    Quote Originally Posted by OldRon View Post
    Any ideas?
    Sure, rounding!

  10. #10
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,038

    Re: vb6 will and VC98 C++ won't

    Quote Originally Posted by OldRon View Post
    Arjay,

    When I realized that I was the sole owner of the problem then I did not want to waste people's time.
    It is not a waste of our time to reply to a question regarding something happening which is not understood.

    33.3333 * 3600 is actually 119999.88
    33.333 * 3600 is actually 119998.8

    Simple test c++ code gives:

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	cout << "33.3333 * 3600 = " << 33.3333 * 3600 << endl;
    	cout << "33.333 * 3600 = " << 33.333 * 3600 << endl;
    	cout << "100 * 3600 / 3 = " << 100.0 * 3600.0 / 3.0 << endl;
    }
    Code:
    33.3333 * 3600 = 120000
    33.333 * 3600 = 119999
    100 * 3600 / 3 = 120000
    I suspect that the first calculation result (120000) is what is wanted - as 33.3333 is 100 / 3. So 100 / 3 * 3600 is 100 * 3600 / 3 doing multiplication first which gives 360000 / 3 giving 120000.

    When doing non-integer calculations, it is important to remember that some decimal numbers don't represent accurately in binary (just as some decimal rational numbers don't represent accurately as decimal - such as 10 / 3). It is also preferable to express a number rationally where possible - rather than as a decimal. Also, if possible without overflow, then multiplication should be undertaken before division.

    The default output format for decimal numbers is what may be expected taking into account rounding, size of the number etc etc. If this default display is not as required, then alternative display formats can be used. Consider:

    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    int main()
    {
    	cout << fixed << setprecision(2);
    	cout << "33.3333 * 3600 = " << 33.3333 * 3600 << endl;
    	cout << "33.333 * 3600 = " << 33.333 * 3600 << endl;
    	cout << "100 * 3600 / 3 = " << 100.0 * 3600.0 / 3.0 << endl;
    }
    which displays:

    Code:
    33.3333 * 3600 = 119999.88
    33.333 * 3600 = 119998.80
    100 * 3600 / 3 = 120000.00
    which is mathematically correct.

    Note that in C++ calculation result is not always as expected. 6 / 4 is 1 (irrespective of the type of the result) whereas 6.0 / 4.0 is 1.5. integer / integer gives a result of type integer! A negative signed number can be assigned to an unsigned number - it just gives a 2-compliment positive number which is usually very large!).
    Last edited by 2kaud; December 20th, 2019 at 05:15 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.5.2)

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)