header and math.h file

I've been experimenting with namespaces. Everything has gone well, and in the process a problem jumped out: when I try to get the foo namespace function to work-->return pow(a , b) in foo.h I get garbage values. If I use normal math functions that don't require additional header files, it works fine. To declare pow(a,b) the program needs math.h but I'm not having luck positioning it.
These approaches are not ideal, mostly for learning.
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include "math.h" //just to make the example more exciting
#include "foo.h"
#include "goo.h"
#include<iostream>

using namespace std;



int main()
{
	{
    using namespace goo;
	cout<<dosomething(4,3)<<endl;
	}

	{
	using namespace foo;
	cout<<dosomething(4,50)<<endl;
	}

	cout<<pow(3,3);
	return 0;
}


foo.h
[code]
#ifndef FOO_H_
#define FOO_H_

namespace foo
{

int dosomething(int a, int b)
{
double result = pow ( a, b);
return result; // pow(a,b);
}
}



#endif /* FOO_H_ */

[code/]

goo.h

[code]

#ifndef GOO_H_
#define GOO_H_

namespace goo
{
int dosomething(int a, int b)
{
return (a*b);
}
}



#endif /* GOO_H_ */

[code/]

Output:
12
-2147483648
27
Last edited on
Have you tried std::pow from #include <cmath> ? In general you shouldn't use the old C .h headers unless you have a good reason.
you are trying to do 4**50, that'll likely overflow
Hi Ne555

??? - sorry, not seeing it.
FYI, you messed up the code tags. You need to finish with [/code], not [code/].

If you actually looked at what you posted after posting, you'd have seen that straight away.
??? - sorry, not seeing it.


I'm not @ne555, but he was showing you that you are calculating an extremely large number. 4^50 fits into a 64-bit int, but does not fit into a 32-bit int. Your system apparently uses a 32-bit int, and since the double value you are calculating is so large, the resultant int value is garbage.
Last edited on
OMG

I thought for whatever reason it was 4 * 50 sorry...

pow(4,50)= 1.2676506002282294e+30

I have a 64 bit system.

= 1.8446744073709552 x 10^19 range

or

-9.223372036854776 x 10^19 to +9.223372036854776 x 10^19

Yes, serious overflow.
Last edited on
Topic archived. No new replies allowed.