The bug only shows when you try to calculate the modulus of negative values.
#!/usr/bin/perl $A = 106; printf("Division\n"); printf("106 / 10 = 10\t<- expected\n"); printf("%d / 10 = %01d\t<- correct\n", $A, $A / 10); printf("\n"); printf("Modulus\n"); printf("106 %% 10 = 6\t<- expected\n"); printf("%d %% 10 = %d\t<- correct\n", $A, $A % 10); printf("\n\nNegative values\n"); $A = -106; printf("Division\n"); printf("%d / 10 = 10\t<- expected\n", $A); printf("%d / 10 = %01d\t<- correct\n", $A, $A / 10); printf("\n"); printf("Modulus\n"); printf("%d %% 10 = -6\t<- expected\n", $A); printf("%d %% 10 = %d\t<- WHAT THE!!!\n", $A, $A % 10);
This is the output.
Division 106 / 10 = 10 <- expected 106 / 10 = 10 <- correct Modulus 106 % 10 = 6 <- expected 106 % 10 = 6 <- correct Negative values Divsion -106 / 10 = 10 <- expected -106 / 10 = -10 <- correct Modulus -106 % 10 = -6 <- expected -106 % 10 = 4 <- WHAT THE!!!
Here is the same code in C and the correct output that it gives.
ReplyDelete#include
int main(int argc, char **argv)
{
int A;
A = 106;
printf("Division\n");
printf("106 / 10 = 10\t<- expected\n");
printf("%d / 10 = %01d\t<- correct\n", A, A / 10);
printf("\n");
printf("Modulus\n");
printf("106 %% 10 = 6\t<- expected\n");
printf("%d %% 10 = %d\t<- correct\n", A, A % 10);
A = -106;
printf("\n\nNegative values\n");
printf("Division\n");
printf("%d / 10 = 10\t<- expected\n", A);
printf("%d / 10 = %01d\t<- correct\n", A, A / 10);
printf("\n");
printf("Modulus\n");
printf("%d %% 10 = -6\t<- expected\n", A);
printf("%d %% 10 = %d\t<- correct\n", A, A % 10);
}
Division
106 / 10 = 10 <- expected
106 / 10 = 10 <- correct
Modulus
106 % 10 = 6 <- expected
106 % 10 = 6 <- correct
Negative values
Division
-106 / 10 = 10 <- expected
-106 / 10 = -10 <- correct
Modulus
-106 % 10 = -6 <- expected
-106 % 10 = -6 <- correct