integer-division

Divide by 9 without using division or multiplication operator

百般思念 提交于 2019-12-25 18:54:33
问题 This question I have tried to solve it but couldn't get any way. Any pointers would be appreciated. Regular subtraction way of doing division is not the intention here, ingenious way of using shifting operator to get this done is the intention. 回答1: Here's a solution heavily inspired by Hacker's Delight that really uses only bit shifts: def divu9(n): q = n - (n >> 3) q = q + (q >> 6) q = q + (q>>12) + (q>>24); q = q >> 3 r = n - (((q << 2) << 1) + q) return q + ((r + 7) >> 4) #return q + (r >

Divide 64-bit integers as though the dividend is shifted left 64 bits, without having 128-bit types

試著忘記壹切 提交于 2019-12-24 04:43:22
问题 Apologies for the confusing title. I'm not sure how to better describe what I'm trying to accomplish. I'm essentially trying to do the reverse of getting the high half of a 64-bit multiplication in C for platforms where int64_t divHi64(int64_t dividend, int64_t divisor) { return ((__int128)dividend << 64) / (__int128)divisor; } isn't possible due to lacking support for __int128 . 回答1: This can be done without a multi-word division Suppose we want to do ⌊2 64 × x ⁄ y ⌋ then we can transform

divdi3 division used for long long by gcc on x86

落爺英雄遲暮 提交于 2019-12-23 18:21:24
问题 When gcc sees multiplication or division of integer types that isn't supported in hardware, it generates call to special library function. http://gcc.gnu.org/onlinedocs/gccint/Integer-library-routines.html#Integer-library-routines According link above, long __divdi3 (long a, long b) used for division of long. However, here http://gcc.gnu.org/onlinedocs/gcc-3.3/gccint/Library-Calls.html divdi explained as "call for division of one signed double-word". When first source has cleary mapping of di

Why in Ruby 0.0/0, 3.0/0 and 3/0 behave differently?

删除回忆录丶 提交于 2019-12-23 12:36:55
问题 If I divide by 0, I get either a ZeroDivisionError, Infinity or NaN depending on what is divided. ruby-1.9.2-p180 :018 > 0.0 / 0 => NaN ruby-1.9.2-p180 :020 > 3.0 / 0 => Infinity ruby-1.9.2-p180 :021 > 3 / 0 ZeroDivisionError: divided by 0 I understand that 0.0 / 0 is not an Infinity (in math terms), while 3.0 / 0 is but why then isn't 3 / 0 an Infinity? Why dividing an integer throws an exception but dividing a float doesn't? 回答1: In Ruby, not all numbers are created equal (pun intended).

How can I instruct the MSVC compiler to use a 64bit/32bit division instead of the slower 128bit/64bit division?

谁说胖子不能爱 提交于 2019-12-23 12:19:49
问题 How can I tell the MSVC compiler to use the 64bit/32bit division operation to compute the result of the following function for the x86-64 target: #include <stdint.h> uint32_t ScaledDiv(uint32_t a, uint32_t b) { if (a > b) return ((uint64_t)b<<32) / a; //Yes, this must be casted because the result of b<<32 is undefined else return uint32_t(-1); } I would like the code, when the if statement is true, to compile to use the 64bit/32bit division operation, e.g. something like this: ; Assume

MATLAB division… should 29/128 return 0?

会有一股神秘感。 提交于 2019-12-23 09:04:03
问题 I really don't think this is a precision problem, the answer should be about 0.226. Here's the exact code: val = I(i,j) bucketSize pos = val / bucketSize I is just a matrix I'm taking values from. Here is the output from MATLAB: val = 29 bucketSize = 128 pos = 0 What am I missing? 回答1: My guess would be that your matrix I is pixel data loaded from an image file, which will have values that are typically unsigned 8-bit integers. As already mentioned, converting both integer values to a double

How can I avoid a warning about division-by-zero in this template code?

二次信任 提交于 2019-12-23 08:30:14
问题 I have a class for fixed-point arithmetic, of which this is the salient portion: template <typename I, I S> struct fixed { I value; fixed(I i) : value(i * S) {} template <typename J, J T> fixed(const fixed<J, T> &fx) { if (S % T == 0) value = fx.value * (S / T); else if (T % S == 0) value = fx.value / (T / S); else value = S * fx.value / T; } static_assert(S >= 1, "Fixed-point scales must be at least 1."); }; On GCC 4.4.5, the following line of code: fixed<int, 8> f = fixed<int, 2>(1);

Why does 1 // 0.1 == 9.0? [duplicate]

有些话、适合烂在心里 提交于 2019-12-23 06:58:51
问题 This question already has an answer here : Integer division compared to floored quotient: why this surprising result? (1 answer) Closed 3 years ago . In Python 2.7 and 3.x , why does integer division give me a non-correct number when dividing by a number 0 < x < 1 ? Negative numbers -1 < x < 0 even work correctly: >>> 1//.1 9.0 >>> 1//-.1 -10.0 I understand that integer division with a negative (or positive) number rounds toward negative infinity, however I would have thought 1//.1 should

64 bit / 64 bit remainder finding algorithm on a 32 bit processor?

妖精的绣舞 提交于 2019-12-23 03:14:46
问题 I know that similar questions has been asked in the past, but I have implemented after a long process the algorithm to find the quotient correctly using the division by repeated subtraction method. But I am not able to find out the remainder from this approach. Is there any quick and easy way for finding out remainder in 64bit/64bit division on 32bit processor. To be more precise I am trying to implement ulldiv_t __aeabi_uldivmod( unsigned long long n, unsigned long long d) Referenced in this

64 bit / 64 bit remainder finding algorithm on a 32 bit processor?

馋奶兔 提交于 2019-12-23 03:14:22
问题 I know that similar questions has been asked in the past, but I have implemented after a long process the algorithm to find the quotient correctly using the division by repeated subtraction method. But I am not able to find out the remainder from this approach. Is there any quick and easy way for finding out remainder in 64bit/64bit division on 32bit processor. To be more precise I am trying to implement ulldiv_t __aeabi_uldivmod( unsigned long long n, unsigned long long d) Referenced in this