Math function deg/rad query

This is a discussion on Math function deg/rad query within the C++ Programming forums, part of the General Programming Boards category; I've gone and got myself confused with this little program It's meant to find the height of a triangle in ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    11

    Math function deg/rad query

    I've gone and got myself confused with this little program It's meant to find the height of a triangle in degrees, when the angle of elevation (y) and the distance accross (x) is known, and output it in some sort of table (formatting, however, is not important at this stage.) It does this, but of course only in Radians.

    I put the chunk of code following "float height....." in to convert to degrees, but it seems to do nothing. Even when I change the values, it has no effect on the program.

    Here's the code I have, and note, this is a UK keyboard setup, so I couldn't work out the "hash" symbol. For the sake of this, I'll just write hash. How can I make this thing output Degrees instead of Radians?

    Code:
    hash include <cstdlib>
    hash include <iostream>
    hash include <iomanip>
    hash include <math.h>
    
    using namespace std;
    
    float height (float x, float y)
    {return x*(180/3.141593);
    return y*(180/3.141593);
    }
    
    int main()
    { for (float x=0; x <=60; x+=10)
    { for (float y=0; y <=85; y+=5)
    cout << setw(12) << (tan(y)*x);
    cout << endl;
    }
    }
    Thanks,
    George

  2. #2
    Registered User nempo's Avatar
    Join Date
    Aug 2007
    Posts
    39
    First, you can't return multiple values in c/c++. For this, you need to use pointers in C.
    Second, You need to actually call the function that does the conversion.

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Quote Originally Posted by GeorgeV View Post
    Here's the code I have, and note, this is a UK keyboard setup, so I couldn't work out the "hash" symbol.
    The hash key is next to the return key in the middle row on a standard UK keyboard

    Code:
    float height (float x, float y)
    {return x*(180/3.141593);
    return y*(180/3.141593);
    }
    As nempo said, you can't return multiple values from a function, although since you're using C++, don't use pointers, use references, which will allow you to directly modify the values in-situ. eg,

    Code:
    void height (float & x, float & y)
    {
        x = x *(180/3.141593);
        y = y *(180/3.141593);
    }

    you could also investigate the struct keyword which will allow you to create your own type to combine X and Y together.
    Code:
    struct coordinate
    {
        double x, y;
    };
    
    /* ... */
    
    coordinate to_degrees( double x, double y )
    {
        coordinate coo;
        coo.x = x *(180/3.141593);
        coo.y = y *(180/3.141593);
        return coo;
    }
    (By the way, I reccommend using double's instead of float's - the double type has a much greater precision. the typical precision of float is around 6 significant figures on most modern desktops)

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    1) As Bench82 suggested, use doubles.
    2) Instead of a function that converts two arguments simultaneously, consider a function that converts a single argument, and calling it twice. Then you don't need pointers or references at all, you can use the regular return value. Also, by using the inline keyword, assuming the compiler takes the hint, the function call gets reduced to a single multiplication and there's no more overhead than calling a two-argument function once.
    3) Consider expressing pi symbolically.
    Code:
    #include <cmath>
    
    const double Pi = 4.*std::atan(1.);
    (among other possible expressions for pi).

  5. #5
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Code:
    #define PI 3.14159f
    #define DEGTORAD(x) (float)( (x)*(PI)/(180.0f) )
    You won't need doubles for this particular operation. Floats offer enough precision here to get good results.

    To use this:
    Code:
    float fCosX=cosf(DEGTORAD(angle_in_degrees));
    Last edited by VirtualAce; 08-21-2007 at 10:08 AM.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    Also, indent your program consistently. This is a lot tougher to read
    Code:
    int main()
    { for (float x=0; x <=60; x+=10)
    { for (float y=0; y <=85; y+=5)
    cout << setw(12) << (tan(y)*x);
    cout << endl;
    }
    }
    than this.
    Code:
    int main()
    {
        for (float x=0; x <=60; x+=10)
        {
            for (float y=0; y <=85; y+=5)
                cout << setw(12) << (tan(y)*x);
            cout << endl;
        }
    }
    Code:
    hash include <math.h>
    You do know that it's "#include", not "hash include", right? The '#' symbol is pronounced hash, among other ways. (I've heard it called pound, sharp, number sign, tic-tac-toe board, etc, though hash seems to be the most common.)

    Also, when you use C header files such as <math.h> in C++, you should use the C++-ized header files like <cmath>. (Drop the .h suffix, add a c prefix.) This puts the functions in the namespace std. Though since you just use the entire namespace, it doesn't really matter.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Jul 2006
    Posts
    162
    floats are fine,

    use cmath not math.h,

    pi is already there as M_PI,

    "hash include" ? it's #include,

    #define isn't recommended, use const type var = value;
    Last edited by simpleid; 08-21-2007 at 12:21 PM.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    floats are fine
    Already mentioned. I disagree, but that's only because I've had enough troubles with floats in the past that I always use doubles, just out of habit . . .
    use cmath not math.h
    I said that, and while it's a common thing that people say around here, math.h isn't actually deprecated. (Just recommended.) Using math.h is fine, strictly; the contents will just be in :: instead of std::. But with a using namespace std statement, they're pretty much equivalent.
    pi is already there as M_PI
    Most implementations have M_PI, it's true, but not all. It isn't standard. It's better to calculate PI at runtime, such as with 4.*atan(1), or use your own constant (see google), if you want portability.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Registered User
    Join Date
    Aug 2007
    Posts
    11
    Thanks everybody for all the tips!

    Re the "hash" key: I can't find it on this keyboard. It's pictured on the '3' key, but this only gives the '' sign when combined with 'Shift'.

    Next to the return key you say, Bench82? Unfortunately I'm just not seeing it. (Of course, I won't be programming on this laptop.)

    As someone pointed out, floats offer enough precision for this task, but I've switched to doubles anyway.

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    and people say no one uses trigraphs anymore.
    Code:
    5.2.1.1 Trigraph sequences 1 All occurrences in a source file of the following sequences of three characters (called trigraph sequences12)) are replaced with the corresponding single character. ??= # ??( [ ??/ \ ??) ] ??' ^ ??< { ??! | ??> } ??- ~ No other trigraph sequences exist. Each ? that does not begin one of the trigraphs listed above is not changed. 2 EXAMPLE The following source line printf("Eh???/n"); becomes (after replacement of the trigraph sequence ??/) printf("Eh?\n");
    code block to appease cboard server.

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    > As someone pointed out, floats offer enough precision for this task, but I've switched to doubles anyway.

    Unless your program does a lot of floating-point computation, or you need a large array of floating-point types and can speed up memory access with a smaller type, you may as well use doubles, since they're currently the largest standard floating-point type.

    > and people say no one uses trigraphs anymore.

    I starting programming in C in 1990 and have never used trigraphs, since every keyboard I've used even back then had all the basic characters. I only know about them from K&R2.

    Edit: I'm wrong about doubles being the largest standard floating-point type - actually according to Stroustrup, long double is also standard. From K&R2 it appears that they are standard even in C90. But an 8-byte IEEE 754 double has 52 bits in the mantissa which gives about 15 or 16 decimal digits of precision (more than a pocket calculator), as opposed to about 7 in a 4-byte IEEE 754 float which has 23 mantissa bits, so long double is overkill for most purposes.
    Last edited by robatino; 08-21-2007 at 10:41 PM.

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by robatino View Post
    I starting programming in C in 1990 and have never used trigraphs, since every keyboard I've used even back then had all the basic characters. I only know about them from K&R2.
    I was speaking in absolutist terms.

  13. #13
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    #define isn't recommended, use const type var = value;
    For something as simple as this it's just fine.

  14. #14
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Quote Originally Posted by GeorgeV View Post
    Next to the return key you say, Bench82? Unfortunately I'm just not seeing it. (Of course, I won't be programming on this laptop.)
    In that case your laptop has an American keyboard, but windows is treating it like a UK one.

  15. #15
    Registered User
    Join Date
    Aug 2007
    Posts
    11
    Again, thanks for the help. I'm now using <cmath> instead, and at the moment I'm also using references. So this bit reads something like so:

    Code:
    void height (float & x, float & y)
    {
        x = x *(180/M_PI);
        y = y *(180/M_PI);
    }
    But I'm having trouble calling the 'height' function properly. What is the correct syntax I should be using?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 06:18 AM
  2. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  3. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 08:10 AM
  4. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 07:55 AM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21