1 1 vote int main() { int a, b; /* Some code which initializes "a" here ... */ if (a < 0) { a = -a; } b = sqrt (a); } Above code will always work for calculating the square root of any valid integer value on a system . [T/F] Programming in C programming-in-c + – dd 2.7k views answer comment Share Follow Print See all 6 Comments 6 6 Comments reply Show 3 previous comments dd commented Dec 27, 2016 reply Follow flag b I'll be integer. That's obvious. You can choose appreciate type for b, and make sqrt result as precise as possible. Other than this problem ? 0 0 replyShare thor commented Dec 27, 2016 reply Follow flag no. No problem for any valid integer on system. 0 0 replyShare pC commented Dec 27, 2016 reply Follow flag @Debashish_deka but the validity of this statement will depend on compiler right ? Will we able to get a general answer for this ? I really doubt this get executed correctly always. Moreover for large -ve numbers , will it work ? 0 0 replyShare Please log in or register to add a comment.
2 2 votes #include <stdio.h> int main() { int a=-4, b; /* Some code which initializes "a" here ... */ if (a < 0) { a = -a; } b = sqrt (a); printf("%d",b); return 0; } yes code will always work. But will not give correct output everytime. Like for - 4 it is giving square root as 2. But it is not a correct result srestha answered Dec 27, 2016 srestha comment Share Follow See all 16 Comments 16 16 Comments reply Arjun commented Dec 27, 2016 reply Follow flag I guess the question assumes root of absolute value of a. Try with the largest negative integer value for a. 1 1 replyShare dd commented Dec 27, 2016 reply Follow flag Yes Sir. fails when a = INT_MIN 0 0 replyShare srestha commented Dec 27, 2016 reply Follow flag means a= -32768 how it fails? I am getting answer as 181.http://ideone.com/tB6uLW 0 0 replyShare Kapil commented Dec 27, 2016 reply Follow flag You are using online compiler :O 0 0 replyShare srestha commented Dec 27, 2016 reply Follow flag what is problem with that? 0 0 replyShare Kapil commented Dec 27, 2016 reply Follow flag You will bet better idea on your own computer's compiler. 64 bit or 32 bit have INT_MIN . 0 0 replyShare srestha commented Dec 27, 2016 reply Follow flag in devcpp running, but a warning "9 C:\Dev-Cpp\sqrt.cpp [Warning] converting to `int' from `double' " So, no error at all 0 0 replyShare dd commented Dec 27, 2016 reply Follow flag that compiler taking int as 4 byte. I have checked by forking your code + same version C Now, INT_MIN if we negate it, in 2's complement system it will result in the same number. As in the case of 4 bit, 2's complement of $-8$ is $-8$. That negative number will not work in sqrt() 0 0 replyShare srestha commented Dec 27, 2016 reply Follow flag That I told previously , that -ve number not work. but then u disagree. rt? what is the case of INT_MIN here? I cannot understand. 0 0 replyShare dd commented Dec 27, 2016 reply Follow flag The intention of this code is to take square root the absolute value. So, $-4$ should print $2$. and it will print $2$. INT_MIN for $4$ bit integer number will be $-8$ (minimum representable in 2's complement). Similarly for 32 bit integer. 0 0 replyShare srestha commented Dec 27, 2016 reply Follow flag But in code I am not getting where u considers 2's complement? a= -a; this line not anywhere means to say we have to consider 2's complement. $\sqrt{-8}=2\sqrt{2}i$ (means when -ve stays -ve then code will not run rt?) But in code I am not getting this 0 0 replyShare dd commented Dec 27, 2016 reply Follow flag yes, that would be the problem with negative numbers passed to sqrt(). Ans to answer your QS on 2's complement. You can verify this #include <stdio.h> #include <math.h> int main() { int a = 90,i,b; b = -a; //negation // this loop will print the bit contents of b = -a; // that is 2's complement representation of a for(i = (sizeof(int)<<3) -1;i>=0;i--) (b&(1<<i)) ? printf("1"):printf("0"); printf("\n"); return 0; } 1 1 replyShare srestha commented Dec 27, 2016 reply Follow flag Ok, it is not in range as http://ideone.com/YEaAih i.e.32768 0 0 replyShare Arjun commented Dec 27, 2016 reply Follow flag ^What has 32768 to do with int? 0 0 replyShare srestha commented Dec 27, 2016 reply Follow flag Sir, I mean int range in between -32768 to 32767 But but output here showing 32768. that is why it fails in INT_MIN rt? 0 0 replyShare Arjun commented Dec 27, 2016 reply Follow flag int range in C is never that - that was in stone age. C standard does not fix the size of int, but most compilers take it as 4 bytes. So, range is $-2^{31} - (2^{31} - 1)$ if system uses 2's complement representation (this is also not mandated by C) and since there is one extra number possible for negatives, it fails for INT_MIN. 2 2 replyShare Please log in or register to add a comment.