integer-division

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

霸气de小男生 提交于 2019-11-27 09:42:04
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. It's doing integer division, which truncates everything to the right of the decimal point. 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 the quotient (what you get from 1/2) and 1 is the remainder (what you get from 1%2). It is right to point out that

How to calculate division remainder in SPARC Assembly?

末鹿安然 提交于 2019-11-27 08:16:01
问题 Here is the pseudo code which computes division of two positive integers. HR register saves remainder, and LR saves dividend. (and eventually saves root) However I think this algorithm has some problem. Because this algorithm sometimes don't recover subtraction.(Division is a continuation of subtraction.) For example 6 / 3 (0110 / 011) This algorithm subtract -3 one more time. (This situation never occur when we calculate this division by hand) So I think this algorithm has some problem. Don

Java Division error

旧时模样 提交于 2019-11-27 07:57:25
问题 I have the following variables: int first = 0; int end = 0; Declare in the public class. Within a method: double diff = end / first; double finaldiff = 1 - diff; The end variable on System.out.println is 527 , the first is 480 . Why is the answer for diff coming out as 1 ? It should be 1.097916667 , I thought using a double would enable me to calculate into decimals? 回答1: Dividing two int s will get you an int , which is then implicitly converted to double . Cast one to a double before the

Find the division remainder of a number

喜欢而已 提交于 2019-11-27 07:01:50
How could I go about finding the division remainder of a number in Python? For example: If the number is 26 and divided number is 7, then the division remainder is 5. (since 7+7+7=21 and 26-21=5.) Uku Loskit you are looking for the modulo operator: a % b for example: 26 % 7 Of course, maybe they wanted you to implement it yourself, which wouldn't be too difficult either. The remainder of a division can be discovered using the operator % : >>> 26%7 5 In case you need both the quotient and the modulo, there's the builtin divmod function: >>> seconds= 137 >>> minutes, seconds= divmod(seconds, 60)

How can i convert Integer value to decimal value?

隐身守侯 提交于 2019-11-27 06:55:33
问题 i have an Integer value: Integer value = 56472201; Where the value could be positive or negative. When I divide the value by 1000000, I want this result in the form 56.472201 but instead it gives me just the quotient. How am I able to get both the quotient and remainder values? 回答1: cast it to float and then do it: int i = 56472201; float j = ((float) i)/1000000.0 Edit: Due to precision(needed in your case), use double. Also as pointed by Konrad Rudolph, no need for explicit casting: double j

Check if a number is divisible by 3

 ̄綄美尐妖づ 提交于 2019-11-27 06:48:43
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? 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 MSalters The current answers all focus on decimal digits, when applying the "add all digits and see if that divides by 3". That trick actually works in hex as well; e.g. 0x12 can be divided by 3 because 0x1 + 0x2 = 0x3. And "converting" to hex

Integer division & modulo operation with negative operands in Python

无人久伴 提交于 2019-11-27 03:58:27
问题 Questions arise when I type in these expressions to Python 3.3.0 -10 // 3 # -4 -10 % 3 # 2 10 // -3 # -4 10 % -3 # -2 -10 // -3 # 3 It appears as though it takes the approximate floating point (-3.33)? and rounds down either way in integer division but in the modulo operation it does something totally different. It seems like it returns the remainder +/-1 and only switches the sign depending on where the negative operand is. I am utterly confused, even after looking over other answers on this

128-bit division intrinsic in Visual C++

偶尔善良 提交于 2019-11-27 03:29:06
问题 I'm wondering if there really is no 128-bit division intrinsic function in Visual C++? There is a 64x64=128 bit multiplication intrinsic function called _umul128() , which nicely matches the MUL x64 assembler instruction. Naturally, I assumed there would be a 128/64=64 bit division intrinsic as well (modelling the DIV instruction), but to my amazement neither Visual C++ nor Intel C++ seem to have it, at least it's not listed in intrin.h. Can someone confirm that? I tried grep'ing for the

In Python, what is a good way to round towards zero in integer division?

旧街凉风 提交于 2019-11-27 02:38:44
问题 1/2 gives 0 as it should. However, -1/2 gives -1 , but I want it to round towards 0 (i.e. I want -1/2 to be 0), regardless of whether it's positive or negative. What is the best way to do that? 回答1: Do floating point division then convert to an int. No extra modules needed. >>> int(float(-1)/2) 0 >>> int(float(-3)/2) -1 >>> int(float(1)/2) 0 >>> int(float(3)/2) 1 回答2: Python's default division of integers is return the floor (towards negative infinity) with no ability to change that. You can

How can I use bit shifting to replace integer division?

。_饼干妹妹 提交于 2019-11-27 02:18:06
问题 I understand how to do it for powers of 2 so that's not my question. For example, if I want to find 5% of a number using a bit shift instead of an integer divide, how would i calculate that? So instead of (x * 20 / 19), I could do (x * 100 >> 11). Now this isn't right but it's close and I arrived at it using trial and error. How would I determine the most possible precise shift to use? 回答1: Best approach is to let the compiler do it for you. You simply write a/b in your language of choice,