integer-division

What determines the sign of m % n for integers?

会有一股神秘感。 提交于 2019-11-29 13:46:48
The modulo in Python is confusing. In Python, % operator is calculating the remainder: >>> 9 % 5 4 However: >>> -9 % 5 1 Why is the result 1 ? and not -4 ? Because in python, the sign matches the denominator. >>> 9 % -5 -1 >>> -9 % 5 1 For an explanation of why it was implemented this way, read the blog post by Guido . -10 % 5 is 0, ie, -10 is evenly divided by 5. You ask why -9 % 5 is not -4, and the answer is that both 1 and -4 can be correct answers, it depends on what -9 divided by 5 is. Of course -9 divided by 5 is 1.8, but this is integer division, in Python 3 represented by //, so I'll

How to perform division in Go

时光总嘲笑我的痴心妄想 提交于 2019-11-29 10:29:48
问题 I am trying to perform a simple division in Go. fmt.Println(3/10) This prints 0 instead of 0.3. This is kind of weird. Could someone please share what is the reason behind this? i want to perform different arithmetic operations in Go. Thanks 回答1: The expression 3 / 10 is an untyped constant expression. The specification says this about constant expressions if the operands of a binary operation are different kinds of untyped constants, the operation and, for non-boolean operations, the result

How to compute 2⁶⁴/n in C?

耗尽温柔 提交于 2019-11-29 09:50:43
How to compute the integer division, 2 64 /n? Assuming: unsigned long is 64-bit We use a 64-bit CPU 1 < n < 2 64 If we do 18446744073709551616ul / n , we get warning: integer constant is too large for its type at compile time. This is because we cannot express 2 64 in a 64-bit CPU. Another way is the following: #define IS_POWER_OF_TWO(x) ((x & (x - 1)) == 0) unsigned long q = 18446744073709551615ul / n; if (IS_POWER_OF_TWO(n)) return q + 1; else return q; Is there any faster (CPU cycle) or cleaner (coding) implementation? phuclv's idea of using -n is clever, but can be made much simpler. As

Why is modulus operator slow?

我怕爱的太早我们不能终老 提交于 2019-11-29 03:02:20
问题 Paraphrasing from in "Programming Pearls" book (about c language on older machines, since book is from the late 90's): Integer arithmetic operations ( + , - , * ) can take around 10 nano seconds whereas the % operator takes up to 100 nano seconds. Why there is that much difference? How does a modulus operator work internally? Is it same as division ( / ) in terms of time? 回答1: The modulus/modulo operation is usually understood as the integer equivalent of the remainder operation - a side

How to check if given number is divisible of 15 in fastest way?

风流意气都作罢 提交于 2019-11-29 02:24:11
问题 Division in processor takes much time, so I want to ask how to check in fastest way if number is divisible of some other number, in my case I need to check if number is divisible by 15. Also I've been looking through web and found fun ways to check if number is divisible of some number, but I'm looking for fast option. NOTE: as division takes much time I'm looking for answer without / and % . 回答1: Multiplication takes less time then division, so you can try this: inline bool divisible15

Java Division error

江枫思渺然 提交于 2019-11-28 13:56:54
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? Dividing two int s will get you an int , which is then implicitly converted to double . Cast one to a double before the divison: double diff = (double)end / first; 来源: https://stackoverflow.com/questions/10376322/java-division

How can i convert Integer value to decimal value?

早过忘川 提交于 2019-11-28 12:22:15
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? 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 = i / 1000000.0; If you divide an int by a double you will be left with a double result as illustrated by

128-bit division intrinsic in Visual C++

拈花ヽ惹草 提交于 2019-11-28 10:04:20
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 function names in the compiler executable files, but couldn't find _umul128 in the first place, so I guess

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

a 夏天 提交于 2019-11-28 09:47:23
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? 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 dawg Python's default division of integers is return the floor (towards negative infinity) with no ability to change that. You can read the BDFL's reason why. To do 'round up' division, you would use: >>> a=1 >>> b=2 >>> (a+(-a%b))//b 1 >>

X86 IDIV sign of remainder depends on sign of dividend for 8/-3 and -8/3?

主宰稳场 提交于 2019-11-28 08:49:13
问题 Can anyone explain for me why the sign of the remainder is different in these cases? Is this an emulator bug or do real CPUs do this, too? 8 / -3 : quotient(AL) = -2 remainder(AH) = 2 -8 / 3 : quotient(AL) = -2 remainder(AH) = -2 回答1: It is supposed to work that way, though it is tricky to find out by reading the documentation: Non-integral results are truncated (chopped) towards 0. Combined with the "division law" X = dq + r (the dividend is the divisor times the quotient plus the remainder)