-2

I am trying to figure out how to display the square root of a number if it happens to be negative (as it is entered by the user), and if so, display it correctly with the "i" displayed as well. When I do the normal sqrt function, the result is always something like -1.#IND. When I tried using the double complex variables, the positive numbers nor the negative numbers would come out clean.

Below is my code; the comments are what my goal is. The 4 num variables are entered by the user and can be any integer, positive or negative.

 //  Display the square root of each number.  Remember that the user can enter negative numbers and 
//  will need to find the negative root with the "i" displayed.
printf("\nThe square root of %d is %.4f", num1, sqrt(num1));
printf("\nThe square root of %d is %.4f", num2, sqrt(num2));
printf("\nThe square root of %d is %.4f", num3, sqrt(num3));
printf("\nThe square root of %d is %.4f", num4, sqrt(num4));
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
  • AFAIK, there's no short cut option. You have to ensure you pass a non-negative value to `sqrt()`, and print the `i` if the raw value was negative. Even if you use the complex arithmetic functions, I don't think there's a way to print complex values with [`printf()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/printf.html) other than dissecting the real and imaginary parts and printing them separately. – Jonathan Leffler Jan 28 '16 at 04:29
  • 1
    "*When I tried using the double complex variables, the positive numbers nor the negative numbers would come out clean.*" -- What does this mean? That should work, but you haven't shown your code that attempted to do it, only the `sqrt()` call that clearly doesn't work. – Keith Thompson Jan 28 '16 at 04:50
  • 1
    [`sqrt`](http://en.cppreference.com/w/c/numeric/math/sqrt) works with a floating-point type and return the same floating-point type, so how can it accept a complex type? And it doesn't work with negative values because it doesn't return a complex type. Did you read the documentation? – phuclv Jan 28 '16 at 05:36

3 Answers3

3

If you're working with floating point you can use the built-in complex utilities, e.g.:

#include <complex.h>
#include <stdio.h>

int main(void)
{
    double complex num = -4.0;
    double complex s = csqrt(num);

    printf("%.2f + %.2fi\n", creal(s), cimag(s));
}
M.M
  • 138,810
  • 21
  • 208
  • 365
2

You can use:

if ( num1 < 0 )
{
   printf("\nThe square root of %d is %.4fi", num1, sqrt(-num1));
}
else
{
   printf("\nThe square root of %d is %.4f", num1, sqrt(num1));
}
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • 1
    It's normally `3+4i` rather than `3+i4` for a full complex number, so `4i` rather than `i4` for a pure imaginary number, isn't it? But that's nitpicking. – Jonathan Leffler Jan 28 '16 at 04:41
  • @JonathanLeffler, your comment caught me by surprise. I remember (perhaps incorrectly) using `a + ` *i* `b` for most of my life. When look up in Wikipedia, they are using `a + b` *i*. – R Sahu Jan 28 '16 at 04:45
  • It's not impossible that it is a cultural thing — some cultures use one notation and others the other. And it could be my memory at fault; I just looked at an antique 'Science Data Book' and it shows _Z = x + iy = r(cosθ + i sinθ)_. – Jonathan Leffler Jan 28 '16 at 04:52
  • @JonathanLeffler, Phew. For a moment I thought I was losing my mind :) – R Sahu Jan 28 '16 at 04:54
  • Nope: looks like it is me who is suffering from amnesia. I checked two more maths books and they use the _x+iy_ notation. It's a long time since I was last using this stuff…it was at least in another millennium, I think. Wikipedia notes [both notations](https://en.wikipedia.org/wiki/Complex_number#Notation) can be used — and I did a fair amount of working with _j_ instead of _i_ for the reasons stated. – Jonathan Leffler Jan 28 '16 at 04:58
  • @JonathanLeffler `3+4i` is the "normal/correct" convention. Non-constant multipliers, as in in `iy` or `i sinθ`, are a different matter. – Jim Balter Jan 28 '16 at 05:40
  • 1
    @JonathanLeffler when using variables `x+iy` is used, but when using numbers , `3+4i` is used – M.M Jan 28 '16 at 05:49
  • 3
    @JimBalter: Ah; now that was a wrinkle that hadn't occurred to me. Some parts of my memory may not be failing so badly after all, but since I wasn't able to articulate the distinction, maybe that's no real benefit. When I look at one of the maths books harder, I see that it does use _x + iy_ for algebraic complex quantities (symbolic, if you prefer) but _3 + 4i_ for complex numbers. Thank you! – Jonathan Leffler Jan 28 '16 at 05:51
  • 1
    @M.M: see my comment to Jim Balter — and thanks to you too. – Jonathan Leffler Jan 28 '16 at 05:52
1

Pseudocode:

string root(int num) {
    return "" + sqrt(abs(num)) + (num < 0) ? "i":"";
}

Alternatively:

printf("\nThe square root of %d is %.4f%s", num1, sqrt(abs(num1)), (num1 < 0) ? "i":"");
gator
  • 3,465
  • 8
  • 36
  • 76
  • The question is not tagged C++. – R Sahu Jan 28 '16 at 04:30
  • Sure, but this illustrates how to accomplish it in any language. OP can transcribe to C as he sees fit. – gator Jan 28 '16 at 04:31
  • Using the `flag` variable seems odd: you could eliminate it and use `(num < 0) ? "i" : ""`. – Jonathan Leffler Jan 28 '16 at 04:39
  • OP said the inputted numbers are any integer. I considered not using `flag` but I wasn't sure in what order that line would work (if it `sqrt(abs())` before checking the ternary operator). – gator Jan 28 '16 at 04:40