Thread: degmath header

  1. #1
    Registered User bobbinator's Avatar
    Join Date
    Jul 2009
    Posts
    29

    degmath header

    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...

    Thanks for your time
    Robin

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.)
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User bobbinator's Avatar
    Join Date
    Jul 2009
    Posts
    29
    So which one do you think is easier?
    (Your answer is ver confusing LOL)

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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
    (Your answer is ver confusing LOL)
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User bobbinator's Avatar
    Join Date
    Jul 2009
    Posts
    29
    No, not looking for motivation, just wanted an honest opinion, thanks for your help

    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

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User bobbinator's Avatar
    Join Date
    Jul 2009
    Posts
    29
    Thanks

    Very quickly, im a beginner so i would like to know two things,
    what is a wrapper and what does the inline do?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User bobbinator's Avatar
    Join Date
    Jul 2009
    Posts
    29
    with your wise advise im guessing you have hundreds of friends

  10. #10
    Registered User bobbinator's Avatar
    Join Date
    Jul 2009
    Posts
    29
    Degmath updated

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.)

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User bobbinator's Avatar
    Join Date
    Jul 2009
    Posts
    29
    that's why I stuck with my orig code for calculation becasue i know that they work fine if left alone

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Obtaining source & destination IP,details of ICMP Header & each of field of it ???
    By cromologic in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-29-2006, 02:49 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM

Tags for this Thread