division

Division in Java always results in zero (0)? [duplicate]

*爱你&永不变心* 提交于 2019-11-26 10:36:26
This question already has an answer here: Double value returns 0 [duplicate] 3 answers The function below gets two values from sharedpreferences, weight and height, and I use these to calculate the BMI, When I print the content of the values I get the values i have entered in the sharedprefs ( which is good) but then when i run a division operation on them, I always get 0 as a result.. Where is the error? public int computeBMI(){ SharedPreferences customSharedPreference = getSharedPreferences( "myCustomSharedPrefs", Activity.MODE_PRIVATE); String Height = customSharedPreference.getString(

Simple division in Java - is this a bug or a feature?

我与影子孤独终老i 提交于 2019-11-26 10:35:59
I'm trying this simple calculation in a Java application: System.out.println("b=" + (1 - 7 / 10)); Obviously I expect the output to be b=0.3 , but I actually get b=1 instead. What?! Why does this happen? If I write: System.out.println("b=" + (1 - 0.7)); I get the right result, which is b=0.3 . What's going wrong here? You're using integer division. Try 7.0/10 instead. You've used integers in the expression 7/10, and integer 7 divided by integer 10 is zero. What you're expecting is floating point division. Any of the following would evaluate the way you expected: 7.0 / 10 7 / 10.0 7.0 / 10.0 7

What is the purpose of the div() library function?

▼魔方 西西 提交于 2019-11-26 10:02:32
问题 When C has the / operator to divide two numbers, what is the purpose of having the div() library function? Is there any scenario where / can\'t be used but div() can? 回答1: From the C99 Rationale document: (7.20.6.2 The div, ldiv, and lldiv functions) Because C89 had implementation-defined semantics for division of signed integers when negative operands were involved, div and ldiv, and lldiv in C99, were invented to provide well-specified semantics for signed integer division and remainder

Check if a number is divisible by 3 [closed]

泪湿孤枕 提交于 2019-11-26 09:33:52
问题 Write code to determine if a number is divisible by 3. The input to the function is a single bit, 0 or 1, and the output should be 1 if the number received so far is the binary representation of a number divisible by 3, otherwise zero. Examples: input \"0\": (0) output 1 inputs \"1,0,0\": (4) output 0 inputs \"1,1,0,0\": (6) output 1 This is based on an interview question. I ask for a drawing of logic gates but since this is stackoverflow I\'ll accept any coding language. Bonus points for a

Python3 integer division [duplicate]

你说的曾经没有我的故事 提交于 2019-11-26 09:31:30
问题 This question already has an answer here: Python integer division yields float 4 answers In Python3 vs Python2.6, I\'ve noticed that I can divide two integers and get a float. How do you get the Python2.6 behaviour back? Is there a different method to get int/int = int? 回答1: Try this: a = 1 b = 2 int_div = a // b 来源: https://stackoverflow.com/questions/19507808/python3-integer-division

Math divison in Swift

让人想犯罪 __ 提交于 2019-11-26 09:14:39
问题 I\'m trying to make a math app with different equations and formulas but I\'m trying to circle sector but i just wanted to try to divide the input value by 360 but when I do that it only says 0 unless the value is over 360. I have tried using String, Double and Float with no luck I don\'t know what I\'m doing is wrong but down here is the code. I\'m thankful for help but I have been sitting a while and searched online for an answer with no result I might have been searching with the wrong

Which is better option to use for dividing an integer number by 2?

老子叫甜甜 提交于 2019-11-26 08:58:09
问题 Which of the following techniques is the best option for dividing an integer by 2 and why? Technique 1: x = x >> 1; Technique 2: x = x / 2; Here x is an integer. 回答1: Use the operation that best describes what you are trying to do. If you are treating the number as a sequence of bits, use bitshift. If you are treating it as a numerical value, use division. Note that they are not exactly equivalent. They can give different results for negative integers. For example: -5 / 2 = -2 -5 >> 1 = -3

Why does integer division code give the wrong answer?

二次信任 提交于 2019-11-26 08:54:12
I have a very simple division in Java (it's a product quantity / production per hour), however whenever I make this division I get strange errors: float res = quantity / standard; I have tried the above division with several values and I always get errors, however the one that I've tried everywhere else and gotten right was this: Everywhere in the world: 13.6 = 6800 / 500; Java: 13.0 = 6800 / 500; I've researched BigDecimal and BigInteger, however I haven't found a way to create this division with them, is there any other way to do this division in Java without having precision errors?? Any

Why does dividing a float by an integer return 0.0?

て烟熏妆下的殇ゞ 提交于 2019-11-26 08:26:52
问题 So if I have a range of numbers \'0 - 1024\' and I want to bring them into \'0 - 255\', the maths would dictate to divide the input by the maximum the input will be (1024 in this case) which will give me a number between 0.0 - 1.0. then multiply that by the destination range, (255). Which is what I want to do! But for some reason in Java (using Processing) It will always return a value of 0. The code would be as simple as this float scale; scale = (n/1024) * 255; But I just get 0.0. I\'ve

What is the reason for having '//' in Python? [duplicate]

别等时光非礼了梦想. 提交于 2019-11-26 06:12:12
问题 This question already has an answer here: What is the difference between '/' and '//' when used for division? 14 answers I saw this in someone\'s code: y = img_index // num_images where img_index is a running index and num_images is 3. When I mess around with // in IPython, it seems to act just like a division sign (i.e. one forward slash). I was just wondering if there is any reason for having double forward slashes? 回答1: In Python 3, they made the / operator do a floating-point division,