division

C# is rounding down divisions by itself

五迷三道 提交于 2019-11-26 05:55:08
问题 When I make a division in C#, it automaticaly rounds down. See this example: double i; i = 200 / 3; Messagebox.Show(i.ToString()); This shows me a messagebox containing \"66\". 200 / 3 is actually 66.66666~ however. Is there a way I can avoid this rounding down and keep a number like 66.6666667? 回答1: i = 200 / 3 is performing integer division. Try either: i = (double)200 / 3 or i = 200.0 / 3 or i = 200d / 3 Declaring one of the constants as a double will cause the double division operator to

Double value returns 0 [duplicate]

末鹿安然 提交于 2019-11-26 05:39:07
问题 This question already has answers here : Int division: Why is the result of 1/3 == 0? (15 answers) Closed last year . Here\'s an example: Double d = (1/3); System.out.println(d); This returns 0, not 0.33333... as it should. Does anyone know? 回答1: That's because 1 and 3 are treated as integers when you don't specify otherwise, so 1/3 evaluates to the integer 0 which is then cast to the double 0 . To fix it, try (1.0/3) , or maybe 1D/3 to explicitly state that you're dealing with double values.

Division in C++ not working as expected

倖福魔咒の 提交于 2019-11-26 04:26:58
问题 I was working on something else, but everything came out as zero, so I made this minimalistic example, and the output is still 0. #include <iostream> int main(int argc, char** argv) { double f=3/5; std::cout << f; return 0; } What am I missing? 回答1: You are missing the fact that 3 and 5 are integers, so you are getting integer division. To make the compiler perform floating point division, make one of them a real number: double f = 3.0 / 5; 回答2: It doesn't need to be .0 , you can also do 3./5

How to get a float result by dividing two integer values using T-SQL?

余生长醉 提交于 2019-11-26 03:19:14
问题 Using T-SQL and Microsoft SQL Server I would like to specify the number of decimal digits when I do a division between 2 integer numbers like: select 1/3 That currently returns 0 . I would like it to return 0,33 . Something like: select round(1/3, -2) But that doesn\'t work. How can I achieve the desired result? 回答1: The suggestions from stb and xiowl are fine if you're looking for a constant. If you need to use existing fields or parameters which are integers, you can cast them to be floats

Why does (360 / 24) / 60 = 0 … in Java

旧街凉风 提交于 2019-11-26 02:58:58
问题 I am trying to compute (360 / 24) / 60 I keep getting the answer 0.0 when I should get 0.25 In words: I want to divide 360 by 24 and then divide the result by 60 public class Divide { public static void main(String[] args){ float div = ((360 / 24) / 60); System.out.println(div); } } This prints out: 0.0 Why is that? Am I doing something really stupid, or is there a good reason for this 回答1: None of the operands in the arithmetic is a float - so it's all being done with integer arithmetic and

Is multiplication and division using shift operators in C actually faster?

此生再无相见时 提交于 2019-11-26 02:27:27
问题 Multiplication and division can be achieved using bit operators, for example i*2 = i<<1 i*3 = (i<<1) + i; i*10 = (i<<3) + (i<<1) and so on. Is it actually faster to use say (i<<3)+(i<<1) to multiply with 10 than using i*10 directly? Is there any sort of input that can\'t be multiplied or divided in this way? 回答1: Short answer: Not likely. Long answer: Your compiler has an optimizer in it that knows how to multiply as quickly as your target processor architecture is capable. Your best bet is

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

杀马特。学长 韩版系。学妹 提交于 2019-11-26 02:14:45
问题 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

Why does integer division code give the wrong answer?

白昼怎懂夜的黑 提交于 2019-11-26 02:01:34
问题 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

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

让人想犯罪 __ 提交于 2019-11-26 01:54:34
问题 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? 回答1: You're using integer division. Try 7.0/10 instead. 回答2: You've used integers in the expression 7/10, and integer 7 divided by integer 10 is zero. What you're

Negative integer division surprising result

痞子三分冷 提交于 2019-11-26 00:44:42
问题 In my application I encountered the following and was surprised by the results: 8/-7=-2 (both integers). what does this means? 回答1: For the actual values, i.e. 8.0/(-7.0) , the result is roughly -1.143 . Your result using integer division is being rounded down toward the more negative value of -2 . (This is also known as "Floor division") This is why you will get the somewhat perplexing answers of: >>> 8/(-7) -2 >>> 8/7 1 Note: This is "fixed" in Python 3, where the result of 8/(-7) would be