What is best option here?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: What is best option here?

  1. #1
    Join Date
    Aug 2002
    Posts
    579

    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
    252

    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
    Jul 2005
    Location
    Netherlands
    Posts
    2,012

    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

  4. #4
    Join Date
    Aug 2002
    Posts
    579

    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.

  5. #5
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,328

    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  6. #6
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,526

    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:
    FeinViewer - an integrated GDI objects viewer for Visual C++ Debugger, and more...

  7. #7
    Join Date
    Feb 2013
    Location
    Canada
    Posts
    51

    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

    Microsoft MVP .NET Programming (2012 - Present)
    ®Crestron DMC-T Certified Automation Programmer

  8. #8
    Join Date
    Jul 2013
    Posts
    252

    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 03:21 AM.

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center