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.
Re: What is best option here?
Quote:
Originally Posted by
OneEyeMan
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.
Re: What is best option here?
Quote:
Originally Posted by
OneEyeMan
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.
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.
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());
}
Re: What is best option here?
Quote:
Originally Posted by
OneEyeMan
...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?
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
Re: What is best option here?
Quote:
Originally Posted by
OneEyeMan
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.