How to square a number?

Hi Gang

I know this might sound like a silly question but how do I square a number? Surely I don't need to use the pow() function? I simply want to square a variable.

Example
n2

OR

(20-n)2

Cheers

Jase :slight_smile:

Multiplying the number by itself works for me.

Hi AWOL

Thanks for getting back to me so quickly. Do you mean I need to do the following?

Example
n * n

OR

(20-n) * (20-n)

Cheers

Jase :slight_smile:

Do you mean I need to do the following?

Yes.

Hi PaulS

Thanks for the reply. Can I do the following?

Example

nn instead of n * n

OR

(20-n)(20-n) instead of (20-n) * (20-n)

Cheers

Jase :slight_smile:

Can I do the following?

No. This is computer programming, not algebra.

PaulS = Legend :wink:

ilovetoflyfpv:
PaulS = Legend :wink:

.... wait for it......

ilovetoflyfpv:
PaulS = Legend :wink:

Indeed. Imagine! Someone who actually learned what "square" means! The man is a genius, and a legned in his own mind.

int square(int x)
{
  return x * x;
}

You're welcome!

... and if you use the inline keyword, I believe that you will even avoid the overhead of a function call:

inline int square(int x)
{
  return x * x;
}

for floats and longs you need of course other parameters.
and you can add error handling

int square(int f)
{
  if (abs(f) < 256) return f * f;   // check if overflow will occur because 256*256 does not fit in an int anymore
  else error();
}

or increase the return type

long square(int f)
{
  return  (1L * f) * f;  // or by means of casting    return  ((long)f)*f;
}

In many courses C there was the macro example for squaring a number.
#define square(x) ((x)*(x))
If you encounter such a #define in code, remove it and replace it with a proper (inline) function as proposed above

Wouldn't a template be simpler and less prone to side-effects?

AWOL:
Wouldn't a template be simpler and less prone to side-effects?

Yes

There's also pow(), but the avr-libc implementation at least expects floating point numbers, and therefore includes the FP math library. If you don't use FP anywhere else, that's a lot of unnecessary baggage. If it's already required elsewhere, then you may as well use it.

Another neat trick is using binary math, but this is only useful when taking 2 to some power. (I think. Someone better at math would likely have valuable insight.) In computing, this is a common requirement, so it's worth mentioning:

E.g., ( 28 ) is the same as ( 1 << 8 ), but the shift op is faarrrr more efficient.

This can also be used to set bit masks. If you wanted to set 20 LSB in an integer, you could do this:

uint32_t  i = (1 << 20);  // 0000 0000  0010 0000  0000 0000  0000 0000
i = i - 1;                // 0000 0000  0001 1111  1111 1111  1111 1111

The "1 << 20" operation can be performed using the _BV() macro. _BV(x) == 1 << x.

( 2 ^ 8 ) is the same as ( 1 << 8 ),

sp. "( 28 ) is the same as ( 1 << 8 ),"
This is C remember, and ^ means something very specific.
2^8 == 0x0A

Uh, yeah... sorry. That was not meant to be C syntax. I think I'll correct that to use super-script text instead.

JimboZA:

ilovetoflyfpv:
PaulS = Legend :wink:

.... wait for it......

...... ary.

PaulS = Legend

Must...resist....obvious...