division

remquo Results Not Making Sense

北战南征 提交于 2019-11-28 12:45:37
问题 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

Division and modulus using single divl instruction (i386, amd64)

混江龙づ霸主 提交于 2019-11-28 12:15:44
I was trying to come up with inline assembly for gcc to get both division and modulus using single divl instruction. Unfortunately, I am not that good at assembly. Could someone please help me on this? Thank you. Yes -- a divl will produce the quotient in eax and the remainder in edx. Using Intel syntax, for example: mov eax, 17 mov ebx, 3 xor edx, edx div ebx ; eax = 5 ; edx = 2 You're looking for something like this: __asm__("divl %2\n" : "=d" (remainder), "=a" (quotient) : "g" (modulus), "d" (high), "a" (low)); Although I agree with the other commenters that usually GCC will do this for you

How to divide 2 int in c?

十年热恋 提交于 2019-11-28 11:39:40
wanna divide 2 numbers and get the result like this: 5 / 2 = 2.50 But it only outputs 2. I don't now what i'm doing wrong. Here my code: int a; int b; int c; printf("First num\n"); scanf("%d", &a); printf("Second num\n"); scanf("%d", &b); c = a / b; printf("%d", c); Haris You need a double variable to store the result. int stores only integers. Additionally, you have to typecast the other variables also before performing the division. Do something like this double c; . . . c = (double)a / (double)b; printf("%f", c); NOTE: You do not need the & in printf() statements. To avoid the typecast in

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 >>

Make division by zero equal to zero

妖精的绣舞 提交于 2019-11-28 08:57:37
How can I ignore ZeroDivisionError and make n / 0 == 0 ? Check if the denominator is zero before dividing. This avoids the overhead of catching the exception, which may be more efficient if you expect to be dividing by zero a lot. def weird_division(n, d): return n / d if d else 0 You can use a try / except block for this. def foo(x,y): try: return x/y except ZeroDivisionError: return 0 >>> foo(5,0) 0 >>> foo(6,2) 3.0 I think try except (as in Cyber's answer) is usually the best way (and more pythonic: better to ask forgiveness than to ask permission!), but here's another: def safe_div(x,y):

Assembly mod algorithm on processor with no division operator

隐身守侯 提交于 2019-11-28 08:26:27
I need to implement a simple macro that finds the modulo of two numbers on a processor that doesn't have a division operator (think ARM). I could use division by repeated subtraction, but I don't know if this was the most efficient or easiest to work with. Any suggestions? Code would be even more helpful. This particular class has us using a subset of SPARC, so most operations look like this: add r1, r2, rdest . This particular assignment calls for checking that a mod b == 0 or that the remainder of the division is zero. So any hints or suggestions toward an efficient implementation would be

python: getting around division by zero

我只是一个虾纸丫 提交于 2019-11-28 08:19:19
I have a big data set of floating point numbers. I iterate through them and evaluate np.log(x) for each of them. I get RuntimeWarning: divide by zero encountered in log I would like to get around this and return 0 if this error occurs. I am thinking of defining a new function: def safe_ln(x): #returns: ln(x) but replaces -inf with 0 l = np.log(x) #if l = -inf: l = 0 return l Basically,I need a way of testing that the output is -inf but I don't know how to proceed. Thank you for your help! Since the log for x=0 is minus infinite, I'd simply check if the input value is zero and return whatever

Why does Clang do this optimization trick only from Sandy Bridge onward?

柔情痞子 提交于 2019-11-28 07:41:00
问题 I noticed that Clang does an interesting division optimization trick for the following snippet int64_t s2(int64_t a, int64_t b) { return a/b; } Below is the assembly output if specifying march as Sandy Bridge or above mov rax, rdi mov rcx, rdi or rcx, rsi shr rcx, 32 je .LBB1_1 cqo idiv rsi ret .LBB1_1: xor edx, edx div esi ret Here are the Godbolt links for the signed version and the unsigned version From what I understand it checks whether the high bits of the two operands are zero, and

C integer division and floor

≯℡__Kan透↙ 提交于 2019-11-28 07:01:11
In C, is there a difference between integer division a/b and floor(a/b) where both a and b are integers? More specifically what happens during both processes? Pete Becker a/b does integer division. If either a or b is negative, the result depends on the compiler (rounding can go toward zero or toward negative infinity in pre-C99; in C99+, the rounding goes toward 0). The result has type int . floor(a/b) does the same division, converts the result to double, discards the (nonexistent) fractional part, and returns the result as a double. ouah floor returns a double while a / b where both a and b

Find multiples of a number in PHP

天大地大妈咪最大 提交于 2019-11-28 06:49:21
I want to find all muliples of a number in PHP. I'm using something like this if($count != 20 ) to work out if $count is not equal to 20. But I also need this script to check if $count is not equal to 20, 40, 60, 80, 100, 120, 140, 160 etc. Any ideas? I think i need to use the modulus symbol ( % ), but I don't know. if ($count % 20 != 0) if ($count % 20 != 0) { // $count is not a multiple of 20 } marianboda If you don't want zero to be excluded: if ($count % 20 != 0 || $count == 0) You can do it like so: if($count % 20 != 0) 来源: https://stackoverflow.com/questions/2809864/find-multiples-of-a