Like Tree3Likes

Function

This is a discussion on Function within the C++ Programming forums, part of the General Programming Boards category; The file compiles correct, I just want to make sure i assigned the function and variables correctly because when i ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    30

    Function

    The file compiles correct, I just want to make sure i assigned the function and variables correctly because when i run the program, the output is always 0 reguardless of the inputted numbers.

    Code:
    #include <iostream>#include <cmath>
    
    
    using namespace std;
    
    
    const double G = 6.673 * pow ( 10, -8 );
    
    
    int main ()
    
    
    {
    
    
        double value_1, value_2, distance, gravitational_force;
        char response='y';
        while (response == 'y' || response == 'y')
    
    
        {
    
    
        cout << "Welcome to the Gravitational Force program." << endl;
        cout << endl;
    
    
            {
    
    
            cout << "Please enter the mass of the first object: ";
            cin >> value_1;
            cout << endl;
    
    
            cout << "Please enter the mass of the second object: ";
            cin >> value_2;
            cout << endl;
    
    
            cout << "Please enter the distance between the two objects: ";
            cin >> distance;
            cout << endl;
    
    
            int gravitational_force = ( G * value_1 * value_2 ) / pow ( distance , 2 );
    
    
            cout << "The gravitation force between the two objects is ";
            cout << gravitational_force;
            cout << " dynes.";
            cout << endl;
    
    
            }
    
    
        cout << "Would you like to run the program again? (Y for yes, anything else for no) ";
        cin >>response;
    
    
        }
    
    
        return 0;
    
    
    }

  2. #2
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Code:
     {
    
    
            cout << "Please enter the mass of the first object: ";
            cin >> value_1;
            cout << endl;
    
    
            cout << "Please enter the mass of the second object: ";
            cin >> value_2;
            cout << endl;
    
    
            cout << "Please enter the distance between the two objects: ";
            cin >> distance;
            cout << endl;
    
    
            int gravitational_force = ( G * value_1 * value_2 ) / pow ( distance , 2 );
    
    
            cout << "The gravitation force between the two objects is ";
            cout << gravitational_force;
            cout << " dynes.";
            cout << endl;
    
    
            }
    You might want to take a look at the red part in the above code snippet. Also, your while loop doesn't make sense, i believe you meant for it to loop while the user inputs either 'y' or 'Y' but you made both letters lower case so it's just gibberish atm.
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,208
    And, puhhh....lease, don't square a value using pow(). distance*distance does mathematically the same thing as pow(distance, 2.0) with less fuss.
    Right 98% of the time, and don't care about the other 3%.

  4. #4
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Quote Originally Posted by grumpy View Post
    And, puhhh....lease, don't square a value using pow(). distance*distance does mathematically the same thing as pow(distance, 2.0) with less fuss.
    He needs <cmath> for his gravitational constant anyways, might aswell use pow() for the calculation itself aswell.
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    30
    If I were to change "int" to say float or double, would that make it better. The while loop is to prompt a response, y-yes, i fixed the upper/lower case.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,455
    Quote Originally Posted by iGuardian View Post
    If I were to change "int" to say float or double, would that make it better?
    Yes, since you are storing a floating point number (a number with decimals) into a type which does not support decimals, so the decimals are cut off. Changing the type will fix that.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,208
    Quote Originally Posted by Neo1 View Post
    He needs <cmath> for his gravitational constant anyways, might aswell use pow() for the calculation itself aswell.
    You missed my point, neo.

    I am not suggesting avoiding using math functions completely, and I don't describe needing <cmath> as "fuss". Even with <cmath> included, pow(x,2.0) is not an effective way of squaring x, both in terms of readability, and (depending on how pow() is implemented) in terms of performance or numerical properties.

    I've also seen people using exp(0.0) in place of 1.0, seen others doing exp(log(x)*2) to square x, and even seen people doing exp(log(a)+log(b)) or pow(10.0, log10(a)+log10(b)) to multiply a and b in what they claim is production code. Such approaches are mathematically correct, but they are unnecessary, obfuscate the code, and have some numerical attributes that are undesirable in real-world code.
    Right 98% of the time, and don't care about the other 3%.

  8. #8
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Quote Originally Posted by grumpy View Post
    You missed my point, neo.

    I am not suggesting avoiding using math functions completely, and I don't describe needing <cmath> as "fuss". Even with <cmath> included, pow(x,2.0) is not an effective way of squaring x, both in terms of readability, and (depending on how pow() is implemented) in terms of performance or numerical properties.
    Talking about efficiency in this context seems a bit extreme, this is hardly a performance intensive application. If you have to ask for help on a forum like this, you are probably not concerned about an extra clock cycle or two. Readability and concise coding is infinitely more important. I do believe you could argue that pow(x, 2.0) is more semantically correct than x * x. It seems we are splitting hairs though, so i will end this discussion here.
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,455
    grumpy also made a point of how "x * x" is easier to read than pow(x, 2.0).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Quote Originally Posted by Elysia View Post
    grumpy also made a point of how "x * x" is easier to read than pow(x, 2.0).
    He didn't say HOW it is easier to read, he just concluded that it is. I disagree.

    pow(x, 2.0) is x to the power of two, or x squared.
    x * x is x multiplied by x.

    One could argue that they are mathematically identical (and rightly so), however, if the goal is to create self-documenting code (which is often desirable, i'm sure we can agree), then x * x is an obfuscation of pow(x, 2.0), although a very insignificant one, atleast if what you meant to calculate was x to the power of 2.

    Conside the widely known formula e=mc2, i would argue that using the pow() function in this case would make it easier for an outside observer to connect the code to the mass-energy equivalence:
    Code:
    int e = m * pow(c, 2.0);
    
    int e = m * c * c
    Simply because pow(c, 2.0) reads out "c squared". Atleast to me it does.
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,455
    Well, I'll leave that to grumpy to expand upon. But pow(x, 2.0) looks kind of verbose, whilst x * x does not. Although, I am neutral on the subject. It would seem that the opinions on this goes both ways and seeing as it's not going to be a big bottleneck by itself, it doesn't seem like it would warrant optimization unless proven to be a bottleneck.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,236
    I really tried hard to find a way to defend the use of pow() to square a number, but I just can't. There are a ton of reasons it's insane. Ridiculous performance overhead notwithstanding, the only reason pow() even exists is to allow exponentiating to non-integer, or large, powers. If you want to change the data type of the involved expressions, you'd better pray an overload of pow() is available -- multiplying a number with itself will work whatever data type it is. And saying that x * x is somehow an obfuscation of exponentiation just means you need some remedial math courses. Are you suggesting that pow() is somehow closer to mathematical notation? First of all, it obviously isn't, second of all, there's no need for it to be, third of all, you don't seem to complain that multiplication is done by some weird "star" operator that isn't used for scalar multiplication in mathematics.

    And ninth of all, or whatever I'm up to now, using pow() requires you to write the magic number "2.0" into your code. You can leave a magic number sitting there, or you could do something really weird like #define THE_POWER_OF_THE_GRAVITY_LAW 2.0 somewhere, and then some jackass would probably think it could be changed.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  13. #13
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Quote Originally Posted by brewbuck View Post
    And saying that x * x is somehow an obfuscation of exponentiation just means you need some remedial math courses.
    So would you not say that 1+1+1+5 = 8 is an obfuscation of 3+5 = 8? Is x * x * x * x * x * x * x * x not an obfuscation of pow(x, 8)? Why is this any different than pow(x, 2)?

    Are you suggesting that pow() is somehow closer to mathematical notation?
    That's exactly what im suggesting.

    First of all, it obviously isn't
    What a stellar argument, you have convinced me.

    you don't seem to complain that multiplication is done by some weird "star" operator that isn't used for scalar multiplication in mathematics.
    That is a property of the language, how is this relevant in any way? We're talking about writing concise and self-documenting code, in short, good coding practice. But however we choose to write our code, we still have to adhere to the rules of the language we are using, when have i ever contested this?

    And ninth of all, or whatever I'm up to now
    I believe you're at 4 now, perhaps "you need some remedial math courses"?

    using pow() requires you to write the magic number "2.0" into your code. You can leave a magic number sitting there, or you could do something really weird like #define THE_POWER_OF_THE_GRAVITY_LAW 2.0 somewhere, and then some jackass would probably think it could be changed.
    This is not a property of the pow() function, this is a property of the standard mathematical notation. To express x to the power of 2, you need to include the number 2, whether you like it or not, you're trying to get around this fact by optimizing out the call to pow(). If you're trying to express x to the power of whatever, you use the pow() function, why should there be a special case for 2? Just for brevity? Is it because you feel it's safe to assume that whoever is going to read your code can automatically make the connection between pow(x, 2.0) and x * x? Can you not see that this is an obfuscation? (albeit a small and insignificant one, as i said, we are splitting hairs here)
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  14. #14
    Registered User
    Join Date
    Jun 2005
    Posts
    6,208
    Quote Originally Posted by Elysia View Post
    Well, I'll leave that to grumpy to expand upon.
    brewbuck has expanded on my concerns quite well.

    The classical mathematical definition of "x squared" (I can't use the superscript/subscript notation here) is multiplying x by itself i.e. x*x. The raising of a value to a non-integer power, such as is done by pow(), is an extension of that.

    Students are taught very early on in primary school that * it is one of the symbols representing multiplication. There are therefore few people who do not grasp quite easily that * is multiplication. The fact that multiplication is taught quite early is why the notion of "squaring a value" is often taught as "multiply the value by itself". It is not an accident that most high level programming languages use it for the same purpose.

    pow(), on the other hand, requires specific knowledge of C or C++ math library. I have yet to encounter a beginner to C or C++ who can tell you what pow() does, unless they have actually read up on it. The only people to whom pow() is self-documenting are those who, for one reason or another, have put in the effort to learn the C/C++ math library (and some other languages that have their roots in C or C++, such as Java).

    As evidence of that, beginners in C and C++ (and Java) forums regularly ask questions that amount to "how can I raise a value to a power?". Another common mistake by beginners to C and C++ is using ^ for raising a value to a power (I actually think that using ^ for a bitwise operation is a flaw of C, but that's another story). I have yet to see anyone asking "how can I multiply two values together?" in the forums, except in specialised and advanced contexts (such as asking about how to implement multiplication for a tailored numerical type).

    I am not saying that pow(), or any other math function, should not be used. They all have their place.
    Last edited by grumpy; 09-26-2011 at 05:06 AM.
    Right 98% of the time, and don't care about the other 3%.

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,456
    Quote Originally Posted by Neo1
    So would you not say that 1+1+1+5 = 8 is an obfuscation of 3+5 = 8? Is x * x * x * x * x * x * x * x not an obfuscation of pow(x, 8)? Why is this any different than pow(x, 2)?
    I agree, pow(x, 2) is an obfuscation of x * x

    Quote Originally Posted by Neo1
    If you're trying to express x to the power of whatever, you use the pow() function, why should there be a special case for 2?
    It is quite common for me to try to express x to the power of 1. Would you suggest that I write: pow(x, 1)? If not, why should there be a special case for 1? Just for brevity? Is it because you feel it's safe to assume that whoever is going to read your code can automatically make the connection between pow(x, 1.0) and x? Can you not see that this is an obfuscation?

    Jokes aside, I think that there is indeed a line to be drawn somewhere. You draw the line at 1. I usually draw the line at 2, but I can extend it to 3, i.e., I would write (x * x) and I can accept (x * x * x), but I would consider (x * x * x * x) to be time to reach for pow or an equivalent. It is easy for me to draw the line at 2 because (x * x) is both readable and efficient, whereas pow(x, 2) is at best equally readable and probably less efficient. Furthermore, it may require unnecessary type conversion.
    iMalc likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

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

Similar Threads

  1. Replies: 15
    Last Post: 06-09-2009, 02:19 AM
  2. Replies: 2
    Last Post: 02-26-2009, 10:48 PM
  3. Print function: sending a function.. through a function?
    By scarlet00014 in forum C Programming
    Replies: 3
    Last Post: 11-05-2008, 04:03 PM
  4. Replies: 14
    Last Post: 03-02-2008, 12:27 PM
  5. Replies: 9
    Last Post: 01-02-2007, 03:22 PM

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