integer-division

Why should EDX be 0 before using the DIV instruction?

孤人 提交于 2019-11-25 23:07:51
问题 I noticed when EDX contains some random default value like 00401000, and I then use a DIV instruction like this: mov eax,10 mov ebx,5 div ebx it causes an INTEGER OVERFLOW ERROR. However, if I set edx to 0 and do the same thing it works. I believed that using div would result in the quotient overwriting eax and the remainder overwriting edx . Getting this INTEGER OVERFLOW ERROR really confuses me. 回答1: What to do For 32-bit / 32-bit => 32-bit division: zero- or sign-extend the 32-bit dividend

Division result is always zero [duplicate]

不问归期 提交于 2019-11-25 22:46:36
问题 This question already has an answer here: Dividing 1/n always returns 0.0 [duplicate] 3 answers I got this C code. #include <stdio.h> int main(void) { int n, d, i; double t=0, k; scanf(\"%d %d\", &n, &d); t = (1/100) * d; k = n / 3; printf(\"%.2lf\\t%.2lf\\n\", t, k); return 0; } I want to know why my variable \'t\' is always zero (in the printf function) ? 回答1: because in this expression t = (1/100) * d; 1 and 100 are integer values, integer division truncates, so this It's the same as this

Why does dividing two int not yield the right value when assigned to double?

旧时模样 提交于 2019-11-25 21:56:16
问题 How come that in the following snippet int a = 7; int b = 3; double c = 0; c = a / b; c ends up having the value 2, rather than 2.3333, as one would expect. If a and b are doubles, the answer does turn to 2.333. But surely because c already is a double it should have worked with integers? So how come int/int=double doesn\'t work? 回答1: This is because you are using the integer division version of operator/ , which takes 2 int s and returns an int . In order to use the double version, which

Why does GCC use multiplication by a strange number in implementing integer division?

Deadly 提交于 2019-11-25 21:54:55
问题 I\'ve been reading about div and mul assembly operations, and I decided to see them in action by writing a simple program in C: File division.c #include <stdlib.h> #include <stdio.h> int main() { size_t i = 9; size_t j = i / 5; printf(\"%zu\\n\",j); return 0; } And then generating assembly language code with: gcc -S division.c -O0 -masm=intel But looking at generated division.s file, it doesn\'t contain any div operations! Instead, it does some kind of black magic with bit shifting and magic

Integer division: How do you produce a double?

前提是你 提交于 2019-11-25 21:40:10
问题 For this code block: int num = 5; int denom = 7; double d = num / denom; the value of d is 0.0 . It can be forced to work by casting: double d = ((double) num) / denom; But is there another way to get the correct double result? I don\'t like casting primitives, who knows what may happen. 回答1: double num = 5; That avoids a cast. But you'll find that the cast conversions are well-defined. You don't have to guess, just check the JLS. int to double is a widening conversion. From §5.1.2: Widening

What is the behavior of integer division?

限于喜欢 提交于 2019-11-25 21:38:33
问题 For example, int result; result = 125/100; or result = 43/100; Will result always be the floor of the division? What is the defined behavior? 回答1: Will result always be the floor of the division? What is the defined behavior? Yes, integer quotient of the two operands. 6.5.5 Multiplicative operators 6 When integers are divided, the result of the / operator is the algebraic quotient with any fractional part discarded. 88) If the quotient a/b is representable, the expression (a/b)*b + a%b shall

Int division: Why is the result of 1/3 == 0?

允我心安 提交于 2019-11-25 21:34:18
问题 I was writing this code: public static void main(String[] args) { double g = 1 / 3; System.out.printf(\"%.2f\", g); } The result is 0. Why is this, and how do I solve this problem? 回答1: The two operands (1 and 3) are integers, therefore integer arithmetic (division here) is used. Declaring the result variable as double just causes an implicit conversion to occur after division . Integer division of course returns the true result of division rounded towards zero. The result of 0.333... is thus