division

Division with expected result between 1 and 0 always gives 0 [duplicate]

≯℡__Kan透↙ 提交于 2019-12-04 07:00:00
问题 This question already has answers here : Closed 6 years ago . Possible Duplicate: What is the behavior of integer division in C? When I do a division calculation with an answer that's supposed to be between 1 and 0, it always returns 0. Try this: float a = 1; float b = 2; float c = a / b; //variable divide by variable gives 0.5 as it should float d = 1 / 2; //number divide by number gives 0 float e = 4 / 2; NSLog(@"%f %f %f %f %f", a,b,c, d, e); I get this from the console: 2013-01-17 14:24

division double android

。_饼干妹妹 提交于 2019-12-04 04:31:11
问题 I have a problem with my division on android: double test= 100/ 1280; double test2 = 0.2354; System.out.println(test); System.out.println(test2); I have 0.0 0.2354 I'd like to have 0.078125 0.2345 Thanks 回答1: Try this double test= (double) 100/ 1280; 回答2: if you specify one of the numbers in your division with a decimal, i.e. double test = 100.0/1280; It will give you the desired output. The reason you are not getting the correct result, is that when dividing two ints, the resulting type will

SQL Server round after division

纵饮孤独 提交于 2019-12-04 03:57:10
问题 In a stored procedure I have an expression like select @some_val_in_percents = (@total_val / 100) * @some_val If I use the Round function like this: select @some_val_in_percents = Round((@total_val / 100) * @some_val, 0) will the result be rounded when the whole expression is calculated or will (@total_val / 100) be rounded and than multiplied by @some_val ? 回答1: You seem to be calculating the percent value wrongly. Here's what I would expect it to be like: @some_val * 100 / @total_val As for

C++. Dividing 1 by any number gives 0

回眸只為那壹抹淺笑 提交于 2019-12-04 03:33:03
问题 When I try to divide 1/60 or 1/(60*60) it gives 0. Even in debugger window. I am a bit confused what it could be, because 2/3 or 2.5/6 give results. My code: int main() { double k1 = 1/60; cout << k1 << endl; double k2 = 1/(60*60); cout << k2 << endl; return 0; } I appreciate your help. 回答1: Since both your operands are integers, the compiler performs an integer division (that doesn't calculate the decimal part). If at least one of the operands is a floating point type (as in your other

Doctest not recognizing __future__.division

夙愿已清 提交于 2019-12-04 03:28:28
I have the following doctest written x.doctest : This is something: >>> x = 3 + 4 foo bar something else: >>> from __future__ import division >>> y = 15 >>> z = int('24') >>> m = z / y >>> print (m) 1.6 But when I ran python -m doctest x.doctest on python 2.7.11, the doctest didn't recognize from __future__ import division : ********************************************************************** File "x.doctest", line 11, in x.doctest Failed example: print (m) Expected: 1.6 Got: 1 ********************************************************************** 1 items had failures: 1 of 6 in x.doctest **

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

爱⌒轻易说出口 提交于 2019-12-04 01:04:39
问题 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

Python 2.6.5: Divide timedelta with timedelta

Deadly 提交于 2019-12-03 23:19:48
I'm trying to divide one timedelta object with another to calculate a server uptime: >>> import datetime >>> installation_date=datetime.datetime(2010,8,01) >>> down_time=datetime.timedelta(seconds=1400) >>> server_life_period=datetime.datetime.now()-installation_date >>> down_time_percentage=down_time/server_life_period Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unsupported operand type(s) for /: 'datetime.timedelta' and 'datetime.timedelta' I know this has been solved in Python 3.2 , but is there a convenient way to handle it in prior versions of Python,

Assembler 64b division

梦想的初衷 提交于 2019-12-03 20:19:42
I need some easy way to divide 64b unsigned integers in assembler for x86. My number is saved in two 32b registers EDX:EAX and I need to put result back to EDX:EAX. Factor is in 32b integer. Some code, please? If I interpret your question correctly (particularly the part Factor is in 32b integer ), you want to divide a 64-bit dividend by a 32-bit divisor and get a 64-bit quotient. If that interpretation is correct, then it's actually easy to do in 32-bit code. The idea is that you divide both "halves" of the dividend by the divisor and reuse the remainder from the first division for the second

Recursive subdivision on octahedron in OpenGL

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-03 18:18:39
I have been referring to this post Drawing Sphere in OpenGL without using gluSphere()? which has helped me quite a lot but i'm stumped now. I have an octahedron in my scene and I would now like to recursively subdivide the triangles to create a sphere. I found this block of code which is supposed to carry out the subdivision for me but I don't understand it fully void subdivide(GLfloat v1[3], GLfloat v2[3], GLfloat v3[3], int depth) { GLfloat v12[3], v23[3], v31[3]; int i; if (depth == 0) { drawTriangle(v1, v2, v3); return; } for (i = 0; i < 3; i++) { v12[i] = (v1[i]+v2[i])/2.0; v23[i] = (v2[i

x86 Assembly: Division Floating Point Exception dividing by 11

梦想的初衷 提交于 2019-12-03 18:14:27
问题 I'm trying to divide 859091 by 11 to obtain the quotient and the remainder, but I'm getting Floating Point Exception on line: div bx This is my code for SASM : %include "io.inc" section .data dividend dd 859091 divisor dw 11 section .text global CMAIN CMAIN: push ebp mov ebp, esp xor eax, eax xor ebx, ebx xor edx, edx mov ax, word [dividend] mov dx, word [dividend + 2] mov bx, word [divisor] test bx, bx jz exit div bx exit: leave ret 回答1: You're getting divide overflow because the quotient