-2

Considering I have a ^ b where both are real numbers, for which values of a and b I will have a complex answer $(m+n*i)$.

I figured out that one case is when the following is true:

a<0 and b = (2*k+1)/(2n) ; k in Z and n in N

Any ideas how can this be implemented in C++?

p.s. ^ -is power.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
SuTron
  • 387
  • 5
  • 16
  • What have *you* tried so far? If you show me code, I will think about it. – TobiMcNamobi Nov 20 '14 at 16:18
  • just for my curiosity - what language is that? \wedge b etc – pm100 Nov 20 '14 at 16:20
  • @pm100: It's latex. `\wedge` is the mathematical "and" symbol, which looks a bit like ^. – Mike Seymour Nov 20 '14 at 16:21
  • @TobiMcNamobi I tried giving random values to k & n, but it is perhaps the worst method. – SuTron Nov 20 '14 at 16:22
  • @AndrewMedico: Exponentiation. A negative number raised to a non-integer power might not be real (for example, (-1)^(1/2) is `sqrt(-1)`, or `i`). – Mike Seymour Nov 20 '14 at 16:25
  • Checking a rational representation of a number in a computer is pretty much impossible because of [round-off errors](http://floating-point-gui.de/) – anatolyg Nov 20 '14 at 16:25
  • @AndrewMedico that is the point. I want it to be complex, and need the statements to be implemented but don't know how! I need to check if be is (2k+1)/2n type to insure that I have a complex result. – SuTron Nov 20 '14 at 16:26
  • I am really interested what is the reason, some "clever" guys give bad reputation to this question??? – SuTron Nov 20 '14 at 16:29
  • @MikaelEgibyan: You can't simply implement the statements. You'll have to come up with an algorithm to determine whether such `k` and `n` exist. That's rather difficult in C++, since integer types have finite range, and there's no way to represent arbitrary real numbers. It's somewhat easier if you can constrain `b` to be rational. – Mike Seymour Nov 20 '14 at 16:30
  • 1
    @MikaelEgibyan When asking on Stack Overflow, you are expected to try solving your problem by yourself. You should usually post your code here, and ask a specific question. Your question, as it is formulated now, shows that you didn't try any code. This is not bad by itself, but that means that if people give you an answer in a form of code, you will not understand it and be able to use it. – anatolyg Nov 20 '14 at 16:32
  • @MikeSeymour what if b is just rational? will it be easier? Though I need b to be real. – SuTron Nov 20 '14 at 16:32
  • @anatolyg is that a reason to give a bad feedback? I said what i tried, but it was bad. Do I need to place here a function which is doing random choice of numbers??? Maybe I didn't try other thins, because I don't know what to try. That's the reason I am here. – SuTron Nov 20 '14 at 16:35
  • @MikaelEgibyan: Yes, if it's rational, `num/den`, then just rearrange `num = 2*k+1` and `den = 2n` to see whether `k` and `n` are integers. `b` can't be real since there's no way to represent a real number exactly; the best we have are approximations like `float`. – Mike Seymour Nov 20 '14 at 16:37
  • I don't have values for k and n... – SuTron Nov 20 '14 at 16:43
  • @MikaelEgibyan Yes, please, show us your function. Or say bye-bye to your rep. – TobiMcNamobi Nov 20 '14 at 16:47
  • How do you define your power function? Am I allowed to say that (2i)^4 is 16 so 16^(1/4) is 2i and thus not real? – Marc Glisse Nov 20 '14 at 16:56

1 Answers1

1

As I mentioned in a comment, checking a rational representation of a number in a computer is pretty much impossible because of round-off errors. You may be able to work around it, but maybe you can just ignore the special case of negative numbers?

One definition of exponentiation says that negative numbers cannot be raised to any power at all (according to this definition, (-1)^2 doesn't exist). To express this in C++:

bool can_has_power(double a, double b)
{
    if (a < 0)
        return false;
    else if (a == 0)
        return b != 0;
    else
        return true;
}

Maybe you want to allow the special case of integer powers? Here:

bool can_has_power(double a, double b)
{
    if (a < 0)
        return fmod(b, 1) == 0;
    else if (a == 0)
        return b != 0;
    else
        return true;
}

BTW here is some additonal consideration, which you may find enlightening. Non-integer numbers in computers are usually represented using floating-point or fixed-point encoding. Both of these systems represent the numbers as a ratio whose demoninator is a power of 2; for example:

b = m / n; n = 2^52

In this representation, all non-integer numbers have an odd numerator and even denominator! If you use b = 1/7 in your hypothetical program, the exact value 1/7 will be rounded to something like 643371375338642/2^52, which, according to the rules of rational numbers must be adjusted by the GCD of the numerator and denominator: 321685687669321/2^51. If you use your definition of exponentiation, then no negative number can be raised to any non-integer power!

anatolyg
  • 26,506
  • 9
  • 60
  • 134