division

efficient way to divide a very large number stored in 2 registers by a constant

只愿长相守 提交于 2019-12-01 23:01:38
Let's say I want to calculate the following: A/Z Where A is of length 128 bit and Z is 64 bit long. A is stored in 2 64 bit registers since the registers of the system can store up to 64 bits. What would be an efficient way to calculate the result? P.S: I've solved similar multiplication problems by using CSD representations. However, this would require calculating 1/Z first. The right way to solve such a problem, is by returning to the basics: divide the most significant register by the denominator calculate the quotient Q and the rest R define a new temporary register preferrably with the

Why byte and short division results in int in Java?

☆樱花仙子☆ 提交于 2019-12-01 21:50:15
In Java, if we divide byte s, short s or int s, we always get an int . If one of the operands is long , we'll get long . My question is - why does byte or short division not result in byte or short ? Why always int ? Apparently I'm not looking for the "because JLS says so" answer, I am asking about the technical rationale for this design decision in the Java language. Consider this code sample: byte byteA = 127; byte byteB = -128; short shortA = 32767; short shortB = -32768; int intA = 2147483647; int intB = - -2147483648; long longA = 9223372036854775807L; long longB = -9223372036854775808L;

Why 2^31 is not divisible by n?

ぐ巨炮叔叔 提交于 2019-12-01 17:21:55
http://docs.oracle.com/javase/6/docs/api/java/util/Random.html#nextInt%28int%29 says: The algorithm is slightly tricky. It rejects values that would result in an uneven distribution (due to the fact that 2^31 is not divisible by n). The probability of a value being rejected depends on n. The worst case is n=2^30+1, for which the probability of a reject is 1/2, and the expected number of iterations before the loop terminates is 2. The algorithm: int bits, val; do { bits = next(31); val = bits % n; } while (bits - val + (n-1) < 0); The code tests the case where n > 2^30 and bits > n . Then the

Why does map return an additional element when using ranges in Haskell?

别等时光非礼了梦想. 提交于 2019-12-01 17:09:54
I've just started learning Haskell and found a strange thing. Let we have a list: ghci> [0,2..5] [0,2,4] It has 3 elements. When I use map with this list I get 3 element as output, for example: ghci> map (+ 1) [0,2..5] [1,3,5] ghci> map (* 2) [0,2..5] [0,4,8] ghci> map (`div` 2) [0,2..5] [0,1,2] But when I use fractional division I get 4 elements in output list: ghci> map (/ 2) [0,2..5] [0.0,1.0,2.0,3.0] ghci> length (map (/ 2) [0,2..5]) 4 Could you please explain why map may return more elements then it was? Thank you! It's due to the implementation of Enum for Float and Double : > [0,2..5] :

Why does map return an additional element when using ranges in Haskell?

半腔热情 提交于 2019-12-01 15:57:45
问题 I've just started learning Haskell and found a strange thing. Let we have a list: ghci> [0,2..5] [0,2,4] It has 3 elements. When I use map with this list I get 3 element as output, for example: ghci> map (+ 1) [0,2..5] [1,3,5] ghci> map (* 2) [0,2..5] [0,4,8] ghci> map (`div` 2) [0,2..5] [0,1,2] But when I use fractional division I get 4 elements in output list: ghci> map (/ 2) [0,2..5] [0.0,1.0,2.0,3.0] ghci> length (map (/ 2) [0,2..5]) 4 Could you please explain why map may return more

How can I get the result with decimal number in division? C++, Pi

佐手、 提交于 2019-12-01 14:37:51
My school give me an assignment to calculate pi. The result should be : Question 4 Accuracy set at : 1000 term pi 1 4 100 3.13159 200 3.13659 300 3.13826 400 ... ... ... The result in my program : term pi 1 4 100 3 200 3 300 3 400 ... ... ... I guess that when I do (4 / denominator), the result will lose the decimal number although I have changed some declarations of data type from int to double. (Some websites tell me to do this.) Maybe I do it wrongly. How can I deal with this problem? The following is my program. #include <iostream> using namespace std; class Four { private: int

How can I get the result with decimal number in division? C++, Pi

ぐ巨炮叔叔 提交于 2019-12-01 12:42:32
问题 My school give me an assignment to calculate pi. The result should be : Question 4 Accuracy set at : 1000 term pi 1 4 100 3.13159 200 3.13659 300 3.13826 400 ... ... ... The result in my program : term pi 1 4 100 3 200 3 300 3 400 ... ... ... I guess that when I do (4 / denominator), the result will lose the decimal number although I have changed some declarations of data type from int to double. (Some websites tell me to do this.) Maybe I do it wrongly. How can I deal with this problem? The

How different programming languages handle division by 0?

▼魔方 西西 提交于 2019-12-01 06:29:28
Perhaps this is the wrong sort of question to ask here but I am curious. I know that many languages will simply explode and fail when asked to divide by 0, but are there any programming languages that can intelligently handle this impossible sum - and if so, what do they do? Do they keep processing, treating 350/0 as 350, or stop execution, or what? From Wikipedia : The infinities of the extended real number line can be represented in IEEE floating point datatypes, just like ordinary floating point values like 1, 1.5 etc. They are not error values in any way, though they are often (but not

How different programming languages handle division by 0?

走远了吗. 提交于 2019-12-01 04:38:38
问题 Perhaps this is the wrong sort of question to ask here but I am curious. I know that many languages will simply explode and fail when asked to divide by 0, but are there any programming languages that can intelligently handle this impossible sum - and if so, what do they do? Do they keep processing, treating 350/0 as 350, or stop execution, or what? 回答1: From Wikipedia: The infinities of the extended real number line can be represented in IEEE floating point datatypes, just like ordinary

(a * b) / c MulDiv and dealing with overflow from intermediate multiplication

旧街凉风 提交于 2019-12-01 03:48:38
I need to do the following arithmetic: long a,b,c; long result = a*b/c; While the result is guaranteed to fit in long , the multiplication is not, so it can overflow. I tried to do it step by step (first multiply and then divide) while dealing with the overflow by splitting the intermediate result of a*b into an int array in size of max 4 ( much like the BigInteger is using its int[] mag variable). Here I got stuck with the division. I cannot get my head around the bitwise shifts required to do a precise division. All I need is the quotient (don't need the remainder). The hypothetical method