Go to Post In fact, our robot YOLOs so hard it does drive-bys on the refs. - TrevorKN [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 19-02-2014, 21:07
yash101 yash101 is offline
Curiosity | I have too much of it!
AKA: null
no team
 
Join Date: Oct 2012
Rookie Year: 2012
Location: devnull
Posts: 1,191
yash101 is an unknown quantity at this point
Function-in-a-line!!! (C/C++)

Since I have started vision processing, these are nifty functions, that I've consicified into a single line so they are out of the way of your real code!

Code:
//string funcs, requiring string and sstream
string bool2str(bool in) { stringstream str; str << in; return str.str(); }

string int2str(int inp) { stringstream str; str << inp; return str.str(); }

string double2str(double in) { stringstream str; str << in; return str.str(); }


//math functions!
double tanDeg(double input) { return tan((input) * pi / 180); }
double hypotenuse(double legA, double legB) { return sqrt(((legA*legA) + (legB*legB))); }
double leg(double hypotenuse, double leg) { return sqrt(((hypotenuse*hypotenuse) - (leg*leg))); }
Give me any feedback and post more of these single-liner c functions!
  #2   Spotlight this post!  
Unread 19-02-2014, 21:16
Jared Russell's Avatar
Jared Russell Jared Russell is offline
Taking a year (mostly) off
FRC #0254 (The Cheesy Poofs), FRC #0341 (Miss Daisy)
Team Role: Engineer
 
Join Date: Nov 2002
Rookie Year: 2001
Location: San Francisco, CA
Posts: 3,078
Jared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond repute
Re: Function-in-a-line!!! (C/C++)

There is a <cmath> function for computing the length of the hypotenuse.

http://www.cplusplus.com/reference/cmath/hypot/

For doing numeric type to string conversions, C++11 supports std::to_string, but without C++11 you could still do your stringstream method with a templated function (since the function body is exactly the same regardless of the argument type).

Last edited by Jared Russell : 19-02-2014 at 21:18.
  #3   Spotlight this post!  
Unread 19-02-2014, 22:31
SoftwareBug2.0's Avatar
SoftwareBug2.0 SoftwareBug2.0 is offline
Registered User
AKA: Eric
FRC #1425 (Error Code Xero)
Team Role: Mentor
 
Join Date: Aug 2004
Rookie Year: 2004
Location: Tigard, Oregon
Posts: 486
SoftwareBug2.0 has a brilliant futureSoftwareBug2.0 has a brilliant futureSoftwareBug2.0 has a brilliant futureSoftwareBug2.0 has a brilliant futureSoftwareBug2.0 has a brilliant futureSoftwareBug2.0 has a brilliant futureSoftwareBug2.0 has a brilliant futureSoftwareBug2.0 has a brilliant futureSoftwareBug2.0 has a brilliant futureSoftwareBug2.0 has a brilliant futureSoftwareBug2.0 has a brilliant future
Re: Function-in-a-line!!! (C/C++)

Quote:
Originally Posted by yash101 View Post
Since I have started vision processing, these are nifty functions, that I've consicified into a single line so they are out of the way of your real code!

Code:
//string funcs, requiring string and sstream
string bool2str(bool in) { stringstream str; str << in; return str.str(); }

string int2str(int inp) { stringstream str; str << inp; return str.str(); }

string double2str(double in) { stringstream str; str << in; return str.str(); }


//math functions!
double tanDeg(double input) { return tan((input) * pi / 180); }
double hypotenuse(double legA, double legB) { return sqrt(((legA*legA) + (legB*legB))); }
double leg(double hypotenuse, double leg) { return sqrt(((hypotenuse*hypotenuse) - (leg*leg))); }
Give me any feedback and post more of these single-liner c functions!
Use a template:
Code:
template<typename T>
string as_string(T const& t){
        stringstream ss;
        ss<<t;
        return ss.str();
}
  #4   Spotlight this post!  
Unread 23-02-2014, 19:45
AlexBrinister AlexBrinister is offline
Registered User
AKA: Alex Brinister
FRC #1768 (RoboChiefs)
Team Role: Alumni
 
Join Date: Jan 2013
Rookie Year: 2012
Location: Bolton, MA
Posts: 93
AlexBrinister will become famous soon enough
Re: Function-in-a-line!!! (C/C++)

Templates are the best way to go there. C++11 reportedly has a to_string function in namespace std but it is absent from GCC 4.8.2. Kind of irrelevant seeing as WindRiver uses GCC 3.4.4...

Alex Brinister
Closed Thread


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 02:31.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi