Thread: <math.h> help

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    135

    <math.h> help

    //Vector.h
    Code:
    class Vector
    {
    public:
    	Vector();
    	Vector(int, int);
    	void setVector(int, int);
    	int getVectorX()const;
    	int getVectorY()const;
    	double CalcMagn(int, int);
    	void print()const;
    private:
    	int x;
    	int y;
    	double magnitude;
    };
    //Vector.cpp
    Code:
    #include "Vector.h"
    #include <iostream>
    #include <stdlib.h>
    #include <math.h>
    
    using namespace std;
     
    Vector::Vector()
    {
    	x = 0;
    	y = 0;
    }
    
    Vector::Vector(int Myx, int Myy)
    {
    	x = Myx;
    	y = Myy;
    }
    
    void Vector::setVector(int Myx, int Myy)
    {
    	x = Myx;
    	y = Myy;
    }
    
    int Vector::getVectorX() const
    {
    	return x;
    }
    
    int Vector::getVectorY() const
    {
    	return y;
    }
    
    double Vector::CalcMagn(int x, int y )
    {
    	int doubX = x * x;
    	int doubY = y * y;
    	double magnitude = doubX - doubY;
    	pow(magnitude, 0.5);
    	return magnitude;
    }
    
    
    void Vector::print()const
    {
    	cout << "Original value1 = [ " << x << " " <<  y << " ] " << endl;
    	cout << "The magnitude is " << magnitude << endl;
    }
    main.cpp
    Code:
    #include <iostream>
    #include "Vector.H"
    #include <stdlib.h>
    #include <math.h>
    
    using namespace std;
    
    void main(void)
    {
    	
    	Vector vectorType;
    	
    	vectorType.setVector(5,6 );
    	vectorType.CalcMagn(5, 6);
    	vectorType.print();
    
    	system ("pause");
    }
    I am now doing two Vector and calculator the magnitude of the 2 Vector using class. Right now, i have problem for the pow function.
    Why is that when i output it, it print -9.25596e + 061 instead of number. I think it might be because the pow only accept double number. But how should I do it that I could calculate using the double variable magnitude instead. Help!!!!!!!!!!!

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Well, -9.25596e + 061 IS a number..!
    In fact, it resembles 0.
    What you want is to change the format of the printing. I believe, this has the ways:
    manipulators - C++ Reference

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    135
    So, give me example how to change it

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    pow() is a function that accepts two arguments, and returns a value. It does not modify its arguments. Lines 40-42
    Code:
        double magnitude = doubX - doubY;
        pow(magnitude, 0.5);
        return magnitude;
    computes the square root of magnitude, discards that value (which is returned by pow()), and returns the original value of magnitude.

    Incidentally, there is a function in <math.h> named sqrt() which computes the square root. And, in C++, the header <cmath> is preferred over <math.h>. And main() returns int, not void.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    135
    the same problem if using sqrt() function and change it to <cmath>

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Read the chapter of any basic C++ (or C) textbook that describes how to use functions. Every elementary textbook I have ever encountered has such a section. Those sections are not written to be ignored.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    Registered User
    Join Date
    Apr 2011
    Posts
    135
    i already know the pow function. Expect that since i am using a variable, the cout do not give a number but instead some weird things. But, i cant change it to a number for pow function since I am asking the user for number . Help what should I do, i am stuck/

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by evildotaing View Post
    i already know the pow function.
    Then why are you NOT using it right in the code you posted?

    Edit: You do realize the local variable "magnitude" below is NOT the private class variable "magnitude".
    Code:
    double magnitude = doubX - doubY;
    Tim S.
    Last edited by stahta01; 02-02-2012 at 09:22 AM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  9. #9
    Registered User
    Join Date
    Apr 2011
    Posts
    135
    Code:
    double doubX = x * x;
    	double doubY = y * y;
    	double count = doubX - doubY;
    	magnitude = pow(count, 0.5);
    	return magnitude;
    Change my code but still the same. Help

  10. #10
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by evildotaing View Post
    Code:
    	magnitude = pow(count, 0.5);
    NOTE: The square root of a negative number is NOT valid for most old compilers; The new ones say NaN for it.

    Code:
    magnitude = pow(abs(count), 0.5);
    Edit: I think you code is NOT very good; but, I am just a C programmer. You might wish to listen to the C++ programmers about making the code better. And main should return int.

    Quote Originally Posted by grumpy View Post
    Incidentally, there is a function in <math.h> named sqrt() which computes the square root. And, in C++, the header <cmath> is preferred over <math.h>. And main() returns int, not void.

    Tim S.
    Last edited by stahta01; 02-02-2012 at 11:50 AM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  11. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You're ignoring the return value of CalcMagn (like you were with pow earlier) and then printing an uninitialised member variable.

    Ask yourself if you need magnitude as a member of the class. If not then get rid of it and don't ignore the return value. If so, then you don't need a return value, just set the member variable directly.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  12. #12
    Registered User
    Join Date
    Apr 2010
    Posts
    88
    Quote Originally Posted by evildotaing View Post
    i already know the pow function. Expect that since i am using a variable, the cout do not give a number but instead some weird things.
    manasij7479 wasn't responding to mess with you. The output you received is a number represented in scientific notation. If you are not comfortable reading scientific notation, and you are not sure if your results are correct follow his post. But scientific notation is not too hard to understand. Just google.
    W7, Ubuntu -- mingw, gcc, g++, code::blocks, emacs, notepad++

  13. #13
    Registered User
    Join Date
    Oct 2006
    Posts
    250
    Incidentally, since when is the magnitude of a vector equal to the root difference square of its components?
    iMalc: Your compiler doesn't accept misspellings and bad syntax, so why should we?
    justin777: I have no idea what you are talking about sorry, I use a laptop and there is no ascii eject or something

  14. #14
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by MWAAAHAAA View Post
    Incidentally, since when is the magnitude of a vector equal to the root difference square of its components?
    I wondered why the magnitude was imaginary; but, I thought maybe it is right.

    Euclidean vector - Wikipedia, the free encyclopedia

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  15. #15
    Registered User
    Join Date
    Apr 2010
    Posts
    88
    To consolidate the last few posts into a hint, see:

    Quote Originally Posted by evildotaing View Post
    Code:
    double doubX = x * x;
    	double doubY = y * y;      
    	double count = doubX - doubY;    /* TAKE A CLOSER LOOK AT THIS LINE, NOT QUITE RIGHT, REVISIT PYTHAGORUS */
    	magnitude = pow(count, 0.5);
    	return magnitude;
    Change my code but still the same. Help
    Also, it's somewhat lazy to have the doubX and doubY identifiers. You are not doubling these numbers. You are squaring them. This is an important distinction, and if you're going to store the square of another stored value, at least label them correctly.

    EDIT: Also, when I first glanced at your code, I assumed you had declared integer components earlier and then converting them to floats into doubX and doubY. Another reason why using "doub" in the identifier can be a misleading.
    Last edited by Ocifer; 02-02-2012 at 05:45 PM.
    W7, Ubuntu -- mingw, gcc, g++, code::blocks, emacs, notepad++

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. hex math
    By kroiz in forum C Programming
    Replies: 25
    Last Post: 01-20-2009, 03:46 PM
  2. Why there is no STL for math in C++?
    By siavoshkc in forum C++ Programming
    Replies: 10
    Last Post: 08-01-2006, 07:01 AM
  3. Help with C++ Math
    By aonic in forum C++ Programming
    Replies: 4
    Last Post: 01-29-2005, 04:40 AM
  4. Basic Math Problem. Undefined Math Functions
    By gsoft in forum C Programming
    Replies: 1
    Last Post: 12-28-2004, 03:14 AM
  5. how much math?
    By Yaj in forum A Brief History of Cprogramming.com
    Replies: 22
    Last Post: 06-06-2002, 09:42 AM