division

Unexpected result in long/int division

为君一笑 提交于 2019-11-26 14:35:27
问题 I have values like this: long millis = 11400000; int consta = 86400000; double res = millis/consta; The question is: why res equals 0.0 (instead of ca. 0.131944 )? It's stored in double so there should be no rounding right? 回答1: When you are using a binary operator, both arguments should be of a same type and the result will be in their type too. When you want to divide (int)/(long) it turns into (long)/(long) and the result is (long) . you shouldmake it (double)/(long) or (int)/(double) to

C# is rounding down divisions by itself

丶灬走出姿态 提交于 2019-11-26 13:49:38
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? 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 be used. 200/3 is integer division, resulting in an integer. try 200.0/3.0 You can specify format string with

Why is division more expensive than multiplication?

浪尽此生 提交于 2019-11-26 13:49:10
问题 I am not really trying to optimize anything, but I remember hearing this from programmers all the time, that I took it as a truth. After all they are supposed to know this stuff. But I wonder why is division actually slower than multiplication? Isn't division just a glorified subtraction, and multiplication is a glorified addition? So mathematically I don't see why going one way or the other has computationally very different costs. Can anyone please clarify the reason/cause of this so I know

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

人走茶凉 提交于 2019-11-26 12:31:17
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 Jon Skeet None of the operands in the arithmetic is a float - so it's all being done with integer arithmetic and then converted to a float. If you change the type of an appropriate operand to a float, it'll work

What's the fastest way to divide an integer by 3?

自作多情 提交于 2019-11-26 12:26:16
问题 int x = n / 3; // <-- make this faster // for instance int a = n * 3; // <-- normal integer multiplication int b = (n << 1) + n; // <-- potentially faster multiplication 回答1: This is the fastest as the compiler will optimize it if it can depending on the output processor. int a; int b; a = some value; b = a / 3; 回答2: The guy who said "leave it to the compiler" was right, but I don't have the "reputation" to mod him up or comment. I asked gcc to compile int test(int a) { return a / 3; } for an

Check if a number is divisible by 3

☆樱花仙子☆ 提交于 2019-11-26 12:11:05
问题 I need to find whether a number is divisible by 3 without using % , / or * . The hint given was to use atoi() function. Any idea how to do it? 回答1: Subtract 3 until you either a) hit 0 - number was divisible by 3 b) get a number less than 0 - number wasn't divisible -- edited version to fix noted problems while n > 0: n -= 3 while n < 0: n += 3 return n == 0 回答2: The current answers all focus on decimal digits, when applying the "add all digits and see if that divides by 3". That trick

C++ Best way to get integer division and remainder

て烟熏妆下的殇ゞ 提交于 2019-11-26 12:07:58
问题 I am just wondering, if I want to divide a by b, and am interested both in the result c and the remainder (e.g. say I have number of seconds and want to split that into minutes and seconds), what is the best way to go about it? Would it be int c = (int)a / b; int d = a % b; or int c = (int)a / b; int d = a - b * c; or double tmp = a / b; int c = (int)tmp; int d = (int)(0.5+(tmp-c)*b); or maybe there is a magical function that gives one both at once? 回答1: On x86 the remainder is a by-product

How to find the smallest number with just 0 and 1 which is divided by a given number?

心已入冬 提交于 2019-11-26 12:06:01
问题 Every positive integer divide some number whose representation (base 10) contains only zeroes and ones. One can prove that: Consider the numbers 1, 11, 111, 1111, etc. up to 111... 1, where the last number has n+1 digits. Call these numbers m 1 , m 2 , ... , m n+1 . Each has a remainder when divided by n, and two of these remainders must be the same. Because there are n+1 of them but only n values a remainder can take. This is an application of the famous and useful “pigeonhole principle”;

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

左心房为你撑大大i 提交于 2019-11-26 11:32:43
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? 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 to tell the compiler your intent clearly (i.e. i*2 rather than i << 1) and let it decide what the fastest

ArithmeticException thrown during BigDecimal.divide

大城市里の小女人 提交于 2019-11-26 11:16:04
问题 I thought java.math.BigDecimal is supposed to be The Answer™ to the need of performing infinite precision arithmetic with decimal numbers. Consider the following snippet: import java.math.BigDecimal; //... final BigDecimal one = BigDecimal.ONE; final BigDecimal three = BigDecimal.valueOf(3); final BigDecimal third = one.divide(three); assert third.multiply(three).equals(one); // this should pass, right? I expect the assert to pass, but in fact the execution doesn\'t even get there: one.divide