Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
4.20/5 (2 votes)
See more:
What is the return type of a value that is in degrees minutes seconds format?
Posted
Updated 18-Apr-12 3:20am
v2
Comments
bbirajdar 18-Apr-12 9:13am    
Not clear
Sumal.V 18-Apr-12 9:18am    
In order to calculate degrees in the format- 28°36'40.124"
And return this value in an edit box, what is the return type to use?
bbirajdar 18-Apr-12 10:24am    
There is no built in return type for your requirement. Use 'string'
Sumal.V 18-Apr-12 10:30am    
cheers.. thank you

Whatever type the function declares. If this is your function then you must know what the type is. If it is someone else's then look at the definition. However, judging by your comment above you are looking for a string.
 
Share this answer
 
It is better you use your own data types for these value. You can use a structure which has following definition for the ease of operation on these datas.
C++
struct Coordinate
{
    int Degree;
    int Minute;
    float Seconds;
};
// function that returns Coordinate
Coordinate GetCoordinate()
{
    Coordinate CoordObj;
    CoordObj.Degree = 28;
    CoordObj.Minute = 36;
    CoordObj.Seconds = 40.5;
    return CoordObj;
}
 
Share this answer
 
Comments
Niklas L 19-Apr-12 4:02am    
A good solution, but it leaves an unnecessarily large memory footprint, which you will suffer from when copying instances around. A single double (or float) would probably do. 5'ed anyway, for making it an OO solution.
JackDingler 19-Apr-12 12:09pm    
Well, you could use a single quad word divided into bit fields too...

This would be a large footprint if you're trying to run in 16k of RAM. You wouldn't happen to be a time traveler from the 1970s would you?

The solution provided has some advantages. Its easy to code to. Uses primitive types that will load and process fast. Doesn't require additional math to find the value of the elements.

If you find that you need millions of these and space becomes a problem, only then you should put in the work to optimize the storage.

As far as copying the instances around, use the 'const & operator' if that's a concern.
I would say the return type should be an Angle (or Bearing, ..). Create a class Angle to use. Internally it can use a floating point member for the decimal value. Then add a string formating member function to get the desired string when needed.

Advantages: Easy to perform calculations, and you can reduce the risk of mixing units, such as degrees and radians.

I use the following if it helps you in any way
C++
class angle
{
public:
	enum unit
	{
		rad = 0,
		deg = 1
	};
	angle() : r(0.) {}
	explicit angle(double a, unit u = rad) {
		if (u == rad) r = a; else r = a*M_PI/180;
	}
	angle(const angle& fi) : r(fi.r) {}
	double radians() const { return r; }
	double degrees() const { return r*180/M_PI; }

	// -pi <= a < pi
	void normalize() {
		while(r<-M_PI)r+=2*M_PI;while(r>=M_PI)r-=2*M_PI;
	}
	// 0 <= a < 2pi
	void normalize_positive() {
		while(r<0.)r+=2*M_PI;while(r>=2*M_PI)r-=2*M_PI;
	}

	angle& operator=  (const angle &fi) { if (&fi != this) r = fi.r; return *this; }
	angle& operator+= (const angle &fi) { r += fi.r; return *this; }
	angle& operator-= (const angle &fi) { r -= fi.r; return *this; }
	bool   operator== (const angle &fi) { return r == fi.r; }
	bool   operator>  (const angle &fi) { return r > fi.r; }
	bool   operator<  (const angle &fi) { return r < fi.r; }
	bool   operator>= (const angle &fi) { return r >= fi.r; }
	bool   operator<= (const angle &fi) { return r <= fi.r; }
	angle  operator-  () { return angle(-r); }
	angle  operator-  (const angle& fi) { return angle(r-fi.r); }
protected:
	double r;
};

You can easily add your formating functions to this class, or better yet, create a formating class that does the job on an angle instance.
 
Share this answer
 
Comments
JackDingler 19-Apr-12 12:12pm    
Good example,

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900