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

Hybrid View

  1. #1
    Join Date
    Aug 2002
    Posts
    756

    What is best option here?

    Hi, ALL,
    Consider this code snippet:

    Code:
    class A
    {
    public:
         int GetA() { return m_a; };
         double GetB() { return m_b; };
    private:
         int m_a;
         double m_b;
    }
    
    int main()
    {
         A a;
         int diff = a.GetA() - a.GetB();
    }
    This code compiles fine using MSVC 2010, but do not compile (five warning) on gcc-4.2 (Mac OSX 10.6 Snow Leopard, XCode 4).

    What would be the best way to fix the code to not to produce the warning on gcc and still compile as appropriate on MSVC?

    Thank you.

  2. #2
    Join Date
    Jul 2013
    Posts
    576

    Re: What is best option here?

    Quote Originally Posted by OneEyeMan View Post
    What would be the best way to fix the code
    A compiler could warn about at least two things.

    The most severe is the loss of precision when a double is converted to an int, and the other is that you declare a variable which is then not used. This,

    Code:
    int diff = a.GetA();
    diff -=  static_cast<int>(a.GetB());
    will suppress these warnings but you still lose precision when casting the double to int. To avoid that you need to declare diff a double, like

    Code:
    double diff = a.GetA(); // implicit conversion from int to double == no loss
    diff -=  a.GetB(); // no conversion == no loss
    In my view that's the better option. If calculations call for double, use double throughout.
    Last edited by razzle; October 18th, 2013 at 12:35 AM.

  3. #3
    Join Date
    Aug 2002
    Posts
    756

    Re: What is best option here?

    razzle,
    I would love to declare the diff as double.
    The only problem is that than I need to display it on the screen in a grid as text in integer representation.

    Any idea how to fix it properly?

    Thank you.

  4. #4
    Join Date
    Jul 2013
    Posts
    576

    Re: What is best option here?

    Quote Originally Posted by OneEyeMan View Post
    I would love to declare the diff as double.
    The only problem is that than I need to display it on the screen in a grid as text in integer representation.

    Any idea how to fix it properly?
    You should apply modularization to your program, that is split your program into independent parts according to the principle of separation of concerns.

    http://sunnyday.mit.edu/16.355/parnas-criteria.html

    Performing the calculations is one concern and displaying the results is another so they qualify as modules. The most suitable data structures are used in each module and necessary conversions will take place once and in one place only, namely at the (thin) interface between the otherwise independent modules.

    Modularization can be seen as a forerunner to more fanciful design methods such as object orientation. But modularization is the mother of all good programming so it's more relevant than ever.
    Last edited by razzle; November 6th, 2013 at 04:21 AM.

  5. #5
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: What is best option here?

    Quote Originally Posted by OneEyeMan View Post
    This code compiles fine using MSVC 2010, but do not compile (five warning) on gcc-4.2 (Mac OSX 10.6 Snow Leopard, XCode 4).
    If it only gives warnings and no errors, then it does compile. However, most warnings are useful indications that there is something wrong (or suboptimal) in your code. In MSVC you can set the warning level in the project properties. Always try to use the highest possible level to catch the most errors in your code.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

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

    Re: What is best option here?

    The code below compiles cleanly on my MSVC. Your original code produced a warning message.

    Code:
    class A
    {
    public:
         int GetA() { return m_a; };
         double GetB() { return m_b; };
    private:
         int m_a;
         double m_b;
    };
    
    int main()
    {
         A a;
         int diff = (int)((double)a.GetA() - a.GetB());
    }
    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++23 Compiler: Microsoft VS2022 (17.6.5)

  7. #7
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: What is best option here?

    Quote Originally Posted by OneEyeMan View Post
    ...This code compiles fine using MSVC 2010
    No, it doesn't.
    Even after adding a missing ';' after class declaration, I got two warnings:

    warning C4244: 'initializing' : conversion from 'double' to 'int', possible loss of data
    warning C4189: 'diff' : local variable is initialized but not referenced

    What is gcc telling you?
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  8. #8
    Join Date
    Feb 2013
    Location
    Canada
    Posts
    52

    Re: What is best option here?

    I would go with 2kaud's solution:
    Code:
    class A
    {
    public:
         int GetA() { return m_a; };
         double GetB() { return m_b; };
    private:
         int m_a;
         double m_b;
    };
    
    int main(void)
    {
        A a;
        int diff = (int)((double)a.GetA() - a.GetB());
        std::cout << diff << std::endl;
        return 0;
    }
    As Vladimirf mentioned, you're missing the semicolon after the class declaration. But depending on what kind of precision you want, you could also just convert the double to an integer before it gets subtracted from the other original integer. Keep in mind that the value will be rounded if it is not a whole number, before the subtraction operation, hence why I believe that 2kaud decided to evaluate the expression as 2 double values, before casting the result to an integer.

    As for the 2 warnings that Vladimirf posted:
    Code:
    warning C4244: 'initializing' : conversion from 'double' to 'int', possible loss of data
    warning C4189: 'diff' : local variable is initialized but not referenced
    The first is because there is no explicit cast from double to int. When the compiler tries to fit the double value into an integer datatype, there is a conversion that must happen, and you will lose the decimal precision (because we're not talking about the integral representation of that floating point number; all floating point datatypes have an integer representation). The second is because you are storing the value of 'diff' but not using it. Not truly important in this case, but it does help when you have release code and realize that some values aren't even being used.

    ~Ace
    [sigpic][/sigpic]
    Microsoft MVP .NET Programming (2012 - Present)
    ®Crestron DMC-T Certified Automation Programmer

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

Featured