# <math.h> help

• 02-02-2012
evildotaing
<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!!!!!!!!!!!
• 02-02-2012
manasij7479
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
• 02-02-2012
evildotaing
So, give me example how to change it
• 02-02-2012
grumpy
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.
• 02-02-2012
evildotaing
the same problem if using sqrt() function and change it to <cmath>
• 02-02-2012
grumpy
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.
• 02-02-2012
evildotaing
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/
• 02-02-2012
stahta01
Quote:

Originally Posted by evildotaing
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.
• 02-02-2012
evildotaing
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
• 02-02-2012
stahta01
Quote:

Originally Posted by evildotaing
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
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.
• 02-02-2012
iMalc
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.
• 02-02-2012
Ocifer
Quote:

Originally Posted by evildotaing
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.
• 02-02-2012
MWAAAHAAA
Incidentally, since when is the magnitude of a vector equal to the root difference square of its components?
• 02-02-2012
stahta01
Quote:

Originally Posted by MWAAAHAAA
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.
• 02-02-2012
Ocifer
To consolidate the last few posts into a hint, see:

Quote:

Originally Posted by evildotaing
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.