division

Python: Remove division decimal

只愿长相守 提交于 2019-11-30 06:01:35
I have made a program that divides numbers and then returns the number, But the thing is that when it returns the number it has a decimal like this: 2.0 But I want it to give me: 2 so is there anyway I can do this? Thanks in Advance! You can call int() on the end result: >>> int(2.0) 2 When a number as a decimal it is usually a float in Python. If you want to remove the decimal and keep it an integer ( int ). You can call the int() method on it like so... >>> int(2.0) 2 However, int rounds down so... >>> int(2.9) 2 If you want to round to the nearest integer you can use round : >>> round(2.9)

how to do two complement multiplication and division of integers?

别说谁变了你拦得住时间么 提交于 2019-11-30 03:45:06
I have read this post on binary multiplication using two complement. but it is not very clear to me. Even I have difficulty understanding the wiki article on this. I want to know how to go about calculating multiplications of negative numbers: eg: -1 with -7 should give 7. A 4-bit, 2's complement of -1 is : 1111 A 4-bit, 2's complement of -7 is : 1001 some step-wise way of calculating the multiplication will be helpful. No article I came across talks about division. How to approach this? step 1: sign extend both integers to twice as many bits. This is safe to do, though may not always be

Why is such complex code emitted for dividing a signed integer by a power of two?

巧了我就是萌 提交于 2019-11-30 03:06:24
When I compile this code with VC++10: DWORD ran = rand(); return ran / 4096; I get this disassembly: 299: { 300: DWORD ran = rand(); 00403940 call dword ptr [__imp__rand (4050C0h)] 301: return ran / 4096; 00403946 shr eax,0Ch 302: } 00403949 ret which is clean and concise and replaced a division by a power of two with a logical right shift. Yet when I compile this code: int ran = rand(); return ran / 4096; I get this disassembly: 299: { 300: int ran = rand(); 00403940 call dword ptr [__imp__rand (4050C0h)] 301: return ran / 4096; 00403946 cdq 00403947 and edx,0FFFh 0040394D add eax,edx

Is divmod() faster than using the % and // operators?

旧城冷巷雨未停 提交于 2019-11-30 03:01:54
I remember from assembly that integer division instructions yield both the quotient and remainder. So, in python will the built-in divmod() function be better performance-wise than using the % and // operators (suppose of course one needs both the quotient and the remainder)? q, r = divmod(n, d) q, r = (n // d, n % d) To measure is to know (all timings on a Macbook Pro 2.8Ghz i7): >>> import sys, timeit >>> sys.version_info sys.version_info(major=2, minor=7, micro=12, releaselevel='final', serial=0) >>> timeit.timeit('divmod(n, d)', 'n, d = 42, 7') 0.1473848819732666 >>> timeit.timeit('n // d,

Division of integers returns 0

冷暖自知 提交于 2019-11-30 01:18:01
问题 I feel like I'm missing something obvious. I am trying to test out the distribution of random() . Here is the table: create table test ( id int, random_float float, random_int int ); Here is what I want to do: truncate table test; insert into test (id) values (generate_series(1,1000)); update test set random_float = random() * 10 + 1; update test set random_int = trunc(random_float); select random_int, count(random_int) as Count, cast( count(random_int) / max(id) as float) as Percent from

Is MOD operation more CPU intensive than multiplication?

隐身守侯 提交于 2019-11-30 01:16:56
问题 Why is MOD operation more expensive than multiplication by a bit more than a factor of 2 ? Please be more specific about how CPU performs division operation and returns the result for MOD operation. In the following example the threads each run for a second. The test was performed on a SPARC processor. // multiplication void someThread() { int a = 10234; while (true) { opers++; a = a * a; a++; } // opers ~ 26 * 10^6 in a sec. } // MOD void someThread() { int a = 10234; while (true) { opers++;

Faster integer division when denominator is known?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-30 01:04:56
问题 I am working on GPU device which has very high division integer latency, several hundred cycles. I am looking to optimize divisions. All divisions by denominator which is in a set { 1,3,6,10 }, however numerator is a runtime positive value, roughly 32000 or less. due to memory constraints, lookup table may not be a good option. Can you think of alternatives? I have thought of computing float point inverses, and using those to multiply numerator. Thanks PS. thank you people. bit shift hack is

Signed 64 by 32 integer division

人盡茶涼 提交于 2019-11-29 23:49:12
问题 Assuming you have a machine instruction udive that does a special case 64 by 32 unsigned division by taking a (32bit dividend << 32) / 32bit divisor, we can do a full 64 by 32 division using the following: // assume: a / b guaranteed not to overflow a = 64bit dividend, a.h & a.l are hi & lo 32bits respectively b = 32bit divisor q1 = udive(a.h, b) // (a.h << 32) / b r1 = -(q1 * b) // remainder of the above, shortcut since a.h & 0xffffffff == 0 q2 = a.l / b // a.l / b using regular unsigned

Converting KB to MB, GB, TB dynamically

可紊 提交于 2019-11-29 17:46:09
问题 public String size(int size){ String hrSize = ""; int k = size; double m = size/1024; double g = size/1048576; double t = size/1073741824; DecimalFormat dec = new DecimalFormat("0.00"); if (k>0) { hrSize = dec.format(k).concat("KB"); } if (m>0) { hrSize = dec.format(m).concat("MB"); } if (g>0) { hrSize = dec.format(g).concat("GB"); } if (t>0) { hrSize = dec.format(t).concat("TB"); } return hrSize; } This is a method that should return size in GB,MB, KB or TB. Input value is in KB. for example

remquo Results Not Making Sense

不问归期 提交于 2019-11-29 17:36:09
I read here that remquo should return: If successful, returns the floating-point remainder of the division x/y as defined in std::remainder, and a value whose sign is the sign of x/y and whose magnitude is congruent modulo 2 n to the magnitude of the integral quotient of x/y , where n is an implementation-defined integer greater than or equal to 3 . Now clearly I've misunderstood all that techno-speak cause I thought that I was going to get back the fractional result of the division. Instead this operation: int quot; const float rem = remquo(7.0F, 4.0F, &quot); Sets quot to 2 and rem to -1.0 !