integer-division

C integer division and floor

旧时模样 提交于 2019-11-27 01:40:32
问题 In C, is there a difference between integer division a/b and floor(a/b) where both a and b are integers? More specifically what happens during both processes? 回答1: a/b does integer division. If either a or b is negative, the result depends on the compiler (rounding can go toward zero or toward negative infinity in pre-C99; in C99+, the rounding goes toward 0). The result has type int . floor(a/b) does the same division, converts the result to double, discards the (nonexistent) fractional part

Unsigned 128-bit division on 64-bit machine

社会主义新天地 提交于 2019-11-26 21:03:37
问题 I have a 128-bit number stored as 2 64-bit numbers ("Hi" and "Lo"). I need only to divide it by a 32-bit number. How could I do it, using the native 64-bit operations from CPU? (Please, note that I DO NOT need an arbitrary precision library. Just need to know how to make this simple division using native operations. Thank you). 回答1: If you are storing the value (128-bits) using the largest possible native representation your architecture can handle (64-bits) you will have problems handling

Floor division with negative number

天涯浪子 提交于 2019-11-26 17:33:54
问题 The expression 6 // 4 yields 1 , where floor division produces the whole number after dividing a number. But with a negative number, why does -6 // 4 return -2 ? 回答1: The // operator explicitly floors the result. Quoting the Binary arithmetic operations documentation: the result is that of mathematical division with the ‘floor’ function applied to the result. Flooring is not the same thing as rounding to 0; flooring always moves to the lower integer value . See the math.floor() function:

Why is -1/2 evaluated to 0 in C++, but -1 in Python?

隐身守侯 提交于 2019-11-26 17:18:03
问题 C++ : cout << -1/2 evaluates to 0 Python : -1/2 evaluates to -1 . Why is this the case? 回答1: Integer division in C++ rounds toward 0, and in Python, it rounds toward -infinity. People dealing with these things in the abstract tend to feel that rouding toward negative infinity makes more sense (that means it's compatible with the modulo function as defined in mathematics, rather than % having a somewhat funny meaning). The tradition in programming languages is to round toward 0--this wasn't

How to get fractions in an integer division? [duplicate]

天涯浪子 提交于 2019-11-26 16:38:44
This question already has an answer here: How to make the division of 2 ints produce a float instead of another int? 9 answers How do you divide two integers and get a double or float answer in C? You need to cast one or the other to a float or double . int x = 1; int y = 3; // Before x / y; // (0!) // After ((double)x) / y; // (0.33333...) x / ((double)y); // (0.33333...) Of course, make sure that you are store the result of the division in a double or float ! It doesn't do you any good if you store the result in another int . Regarding @Chad's comment (" [tailsPerField setIntValue:tailsPer]

Rounding integer division (instead of truncating)

你说的曾经没有我的故事 提交于 2019-11-26 16:01:41
I was curious to know how I can round a number to the nearest whole number. For instance, if I had: int a = 59 / 4; which would be 14.75 if calculated in floating point; how can I store the result as 15 in "a"? int a = 59.0f / 4.0f + 0.5f; This only works when assigning to an int as it discards anything after the '.' Edit: This solution will only work in the simplest of cases. A more robust solution would be: unsigned int round_closest(unsigned int dividend, unsigned int divisor) { return (dividend + (divisor / 2)) / divisor; } The standard idiom for integer rounding up is: int a = (59 + (4 -

Is “long x = 1/2” equal to 1 or 0, and why? [duplicate]

心已入冬 提交于 2019-11-26 14:48:57
问题 This question already has an answer here: Integer division: How do you produce a double? 10 answers if I have something like: long x = 1/2; shouldn't this be rounded up to 1? When I print it on the screen it say 0. 回答1: It's doing integer division, which truncates everything to the right of the decimal point. 回答2: Integer division has its roots in number theory. When you do 1/2 you are asking how many times does 2 equal 1? The answer is never, so the equation becomes 0*2 + 1 = 1, where 0 is

Displaying numbers with DOS

只愿长相守 提交于 2019-11-26 14:47:53
I was tasked to write a program that displays the linear address of my program's PSP. I wrote the following: ORG 256 mov dx,Msg mov ah,09h ;DOS.WriteStringToStandardOutput int 21h mov ax,ds mov dx,16 mul dx ; -> Linear address is now in DX:AX ??? mov ax,4C00h ;DOS.TerminateWithExitCode int 21h ; ------------------------------ Msg: db 'PSP is at linear address $' I searched the DOS api (using Ralph Brown's interrupt list ) and didn't find a single function to output a number! Did I miss it, and what can I do? I want to display the number in DX:AX in decimal. Sep Roland It's true that DOS doesn

Check if a number is divisible by 3

☆樱花仙子☆ 提交于 2019-11-26 12:11:05
问题 I need to find whether a number is divisible by 3 without using % , / or * . The hint given was to use atoi() function. Any idea how to do it? 回答1: Subtract 3 until you either a) hit 0 - number was divisible by 3 b) get a number less than 0 - number wasn't divisible -- edited version to fix noted problems while n > 0: n -= 3 while n < 0: n += 3 return n == 0 回答2: The current answers all focus on decimal digits, when applying the "add all digits and see if that divides by 3". That trick

Why does integer division code give the wrong answer?

二次信任 提交于 2019-11-26 08:54:12
I have a very simple division in Java (it's a product quantity / production per hour), however whenever I make this division I get strange errors: float res = quantity / standard; I have tried the above division with several values and I always get errors, however the one that I've tried everywhere else and gotten right was this: Everywhere in the world: 13.6 = 6800 / 500; Java: 13.0 = 6800 / 500; I've researched BigDecimal and BigInteger, however I haven't found a way to create this division with them, is there any other way to do this division in Java without having precision errors?? Any