This question already has answers here:
Modulo operation with negative numbers
(12 answers)
Closed 7 years ago.
I'm learning C basics right now. I have a question which is confusing me little bit.
My question is how the below program's output is - 2 ?
#include<stdio.h>
int main()
{
printf("%d", -5%3);
return 0 ;
}
The % operator is gives you the remainder left after of integer division.
Then -5/3 = -1 with -2 as remainder of division as 3*(-1)=-3 and -5-(-3)=-5+3=-2.
Related
This question already has answers here:
Why are floating point numbers inaccurate?
(5 answers)
Is floating point math broken?
(31 answers)
Closed 4 months ago.
i want to extract the decimal part of a float variable by substracting the whole part, the thing is i get a wrong value
#include<stdio.h>
int main(){
int k ;
float a=12.36,i;
k = (int)a;
i = a - k ;
i*=10;
printf("%f",i);
return 0;
}
well, the output is 3.599997 not 3.6 , is there a way to solve this ?
edit : i know it's because of the binary conversion, i m asking if there is a solution to get the right result, not the cause of it. ty for the replies anw.
edit 2 : sadly it's not a matter of display, i want the stored value to be 3.6 ( in this case) because i need it in other calculations.
This question already has answers here:
How does the bitwise complement operator (~ tilde) work?
(18 answers)
C Unsigned int providing a negative value?
(3 answers)
Closed 2 years ago.
There is an interview question in C as below.
int main()
{
unsigned int a = 9;
a = ~a;
printf("%d\n", a);
}
I though it was supposed to be 6 but it is -10.
~a is assinged back to an unsigned integer then printed out.
It should not be a negative value.
Isn't it? Hoe come?
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 2 years ago.
I have written this piece of code in my computer and the result is 7 instead of 8 (the correct result ... I think).
I don't know why... Can somebody help me?
#include <stdio.h>
int main() {
int num;
num = (68/10.0 - 68/10)*10;
printf("the result %d", num);
return 0;
}
double typically represents exactly about 264 different numbers. 68/10.0 is not one of them,
As a binary64, 68/10.0 is about
6.7999999999999998223643161..., the closest value to 6.8 that is a multiple of a dyadic rational. # AntonH
68/10 is an integer division with a quotient of 6.
(68/10.0 - 68/10)*10 is thus about 7.9999999999999982236431606...
Assigning that to an int is 7 not 8 as the fraction is discarded even though it is very close to 8.
When converting a floating point value consider round to the the closest, rather than truncating.
num = lround((68/10.0 - 68/10)*10);
This question already has answers here:
Modulo operation with negative numbers
(12 answers)
Closed 5 years ago.
I have the following code
#include <stdio.h>
int main(void)
{
printf("%d\n", -8%5);
printf("%d\n", 8%-5);
printf("%d\n", -8%-5);
return 0;
}
The output I get is
-3
3
-3
How is it that -8%5 and 8%-5 have different signs, especially when the results of -8/5 and 8/-5 have the same output ?
Also why does -8%-5 give -3 as the output, when -8/-5 gives it's output as 1 ?
If both operands are nonnegative then the remainder is nonnegative; if not, the sign of the remainder is implementation-defined.
This question already has answers here:
Modulo operation with negative numbers
(12 answers)
Problem using modulo with negative numbers in decryption program
(2 answers)
Closed 4 years ago.
I have couple line of C code testing the modulo operator as follows:
// line 1
printf("%d\n", 5 % (-3)); => output: 2
// line 2
printf("%d\n", -5 % 3); => output: -2
I know that the sign of the modulo depends on the sign of the numerator, but I am curious why not otherwise?
5/(-3) = -1;
(-5)/3 = -1;
If that is agreed then let's calculate the remainder
Remainder = Dividend - ( Divisor * Factor)
5 - (-3 * -1) = 2
-5 - (3 * -1) = -2