• 07-16-2009
bobbinator
Hi everyone,

I have created a library (or header, always forget the difference) that allows you to do trig functions in degrees instead of radiance.

I know that some of you are thinking what is the point because you can just times it by whatever. But when i did this i found that it was confusing to read so i said enough was enough! I made them in degrees. I have also seen on other forums that people always ask if there is a degree function.

Which one is more confusing?
This one:
sin(x * (3.141592 / 180));

or this one? (my one)
dsin(x);

Most information is on the website and as far as i know, this works on code blocks (place it in the include file) but not sure about other IDE's.

Kiwi Software

Select the C++ Library menu and hey presto you have it!
I only ask that if you use it, just tell people of it because I think IDE's should include it...

Robin
• 07-16-2009
laserlight
Quote:

Originally Posted by bobbinator
Which one is more confusing?
This one:
sin(x * (3.141592 / 180));

or this one? (my one)
dsin(x);

If you know what is x and what the functions do, they are roughly as confusing, though for the former you have to worry about whether the conversion is correct, unless you know it offhand.

Instead of (or possibly in addition to) writing wrappers for all the trigonometric functions in the standard library, it would be easier to write two inline functions to convert degrees to/from radians. (They do not already seem to exist in the standard library, apparently.)
• 07-16-2009
bobbinator
So which one do you think is easier?
• 07-16-2009
laserlight
Quote:

Originally Posted by bobbinator
So which one do you think is easier?

Easier in what way? It sounds like you are just looking for a positive confirmation of your motivation, so I am inclined to tell you that the former is easier since I do not need your library.

Quote:

Originally Posted by bobbinator

I am telling you to consider a third option: convert into radians, perform whatever calculation you need to perform, then convert back to degrees. This way you avoid writing wrappers for each trigometic function, and if you do write wrappers anyway, this allows your users to easily adapt trigonometric functions that you did not write wrappers for.

By the way:
Quote:

Originally Posted by bobbinator
Most information is on the website and as far as i know, this works on code blocks (place it in the include file) but not sure about other IDE's.

You probably mean compiler rather than IDE.
• 07-16-2009
bobbinator
No, not looking for motivation, just wanted an honest opinion, thanks for your help :D

yeah, sorry i meant complier (silly me)

When you said you did not want my library, you know you do, its so tempting, just take it, AND FOLLOW THE DEVIL MUHAHAH

More reviews would be nice
• 07-16-2009
laserlight
Okay, so I took at a look at your degmath.h header. One problem I see is that you did not declare those functions as inline, which would likely lead to linker errors if a user included that header in more than one translation unit (as in source file + headers).

Another problem is that you have a using directive (using namespace std) in the global namespace scope. This is a no-no for a header file. You should also #include <cmath> instead of <math.h>. Note that <cmath> has overloads for the various functions, but you only defined the float versions. You should overload for double and long double, for completeness.

Looking at your implementation of dsin(), I notice that you just return the result of sin() directly. This means that your users would be passing a value in degrees, but get an output in radians, which would certainly be confusing.

To illustrate what I was talking about, you could define:
Code:

```inline float deg2rad(float x) {     return x * (3.141593f / 180.0f); } inline float rad2deg(float x) {     return x * (180.0f / 3.141593f); }```
Now if you still want your dsin wrapper of sin, you could write:
Code:

```inline float dsin(float x) {     return rad2deg(std::sin(deg2rad(x))); }```
Oh, and consider placing your library of functions in a suitable namespace rather than dumping them in the global namespace.
• 07-16-2009
bobbinator
Thanks :D

Very quickly, im a beginner so i would like to know two things,
what is a wrapper and what does the inline do?
• 07-16-2009
laserlight
Quote:

Originally Posted by bobbinator
what is a wrapper and what does the inline do?

A wrapper is a rather generic term. In this context, I mean that your functions basically just forward the job to the corresponding functions from the standard library, so in a sense they "wrap" those functions.

The inline keyword suggests that each call of the function be replaced by the function's implementation. Even if this substitution is not actually performed, it is as if it were performed, with respect to how many times the function is defined if its definition appears in more than one translation unit.

Oh, and another relatively minor thing, though it has bitten someone who posted here in the past: identifier names that begin with an underscore followed by an uppercase letter, or that contain consecutive underscores, are reserved to the implementation for any use. As such, you should not #define _DEGMATH_H. You could use say, DEGMATH_H, perhaps prefixing with your namespace name (and your name, etc).

Speaking of header inclusion guards, it looks like you forgot your #ifndef and #endif, and you should also have the #ifndef on the very first line rather than have a header inclusion there.
• 07-16-2009
bobbinator
with your wise advise im guessing you have hundreds of friends :D
• 07-16-2009
bobbinator
Degmath updated
• 07-16-2009
laserlight
Quote:

Degmath updated
I suggest that you post the code here.

I note that you still have not yet properly implemented the header inclusion guard. It looks like some of your functions still return in radians instead of degrees. Hopefully, you will also overload for double and long double.

Here is an example of what I have in mind:
Code:

```#ifndef DEGMATH_H #define DEGMATH_H #include <cmath> namespace degmath { inline float deg2rad(float x) {     return x * (3.141593f / 180.0f); } inline float rad2deg(float x) {     return x * (180.0f / 3.141593f); } inline double deg2rad(double x) {     return x * (3.141593 / 180.0); } inline double rad2deg(double x) {     return x * (180.0 / 3.141593); } inline float dsin(float x) {     return rad2deg(std::sin(deg2rad(x))); } inline double dsin(double x) {     return rad2deg(std::sin(deg2rad(x))); } inline float dcos(float x) {     return rad2deg(std::cos(deg2rad(x))); } inline double dcos(double x) {     return rad2deg(std::cos(deg2rad(x))); } } #endif```
I suppose that with the degmath namespace you could remove the 'd' prefix, but then these functions inherited from the C standard library may also be available in the global namespace, in which case a conflict is likely if you use a using declaration or using directive for degmath.
• 07-16-2009
tabstop
Um, sin doesn't return an angle, so calling rad2deg on the result of sin (or cos, or tan) could only be characterized as Extremely Wrong.

(asin, acos, atan do return angles, so you would call rad2deg(asin(x)) -- note that the argument to asin is also Not An Angle, so shouldn't be transformed.)
• 07-16-2009
laserlight
Quote:

Originally Posted by tabstop
Um, sin doesn't return an angle, so calling rad2deg on the result of sin (or cos, or tan) could only be characterized as Extremely Wrong.

That is true, my mistake.
• 07-16-2009
bobbinator
that's why I stuck with my orig code for calculation becasue i know that they work fine if left alone :D

LaserLight, if i took that code of yours I would feel that it would change from Robin's code to yours LOL :D
Thanks for ALL the help