Float square of int

I was googling like and hour but still I could not find anything about my problem.

Is there a way to square a number like: 5.14th square of number 5,
WITHOUT using any cmath function?

with pow function it would look like: pow(5, 5.14);

Thanks for any help.
Last edited on
That would come as quite complicated, but it's doable. Though, to do that, you would surely need the knowledge of laws of indices,
logarithms and antilogs... Why do you need it? Often using a simple function from cmath is much simpler, and it had much more testing than the function you would be able to produce.

Though, this seems to be a code that works:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
float pow(float base, int exp)
{
	float total = 1;
	int increment;

	if(exp > 0)
		increment = -1;
	if(exp < 0)
	{
		increment = 1;
		base = 1/base;
	}

	while(exp)
	{
		total *= base;
		exp += increment;
	}

	return total;
}


But it's not good enough for the decimal exponents, but here is what you can do, the decimal part of the exponent, is basically 0.14, which is a root of something, 14/100. Go a bit into it and it will be possible to write a function, but for what I know, khanacademy has some videos that should help. You have to take the whole part of the thing, as you have 5 to the power of 5.14, so 5.14 rounded gives 5, and then you run it through the function I gave above, and then you see for a way of getting a root function, and you take the rest, 0.14 and see what root it is, it's quite a messy one, 1/(50/7), but with a bit of work, you could get a function that works for you. Then you multiply the result from the whole part with the result from the root part, which gives you the final result. Hopefully this helps.

Also for the calculation of the root, I recommend looking into Heron's method.

http://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method

Here is an older post that went through that:

http://www.cplusplus.com/forum/beginner/57798/

Good luck!
Last edited on
Also, a really basic method for this thing would be doing it the power/root way... Basically, 5 to the power of 5.14 is the same as:

(5 to the power of 514) time (100th root of 5), it's a funny thought and could help you with solving the problem. Though without editing, this method would be extremely slow.
Topic archived. No new replies allowed.