division

What's wrong with this division? [closed]

百般思念 提交于 2019-11-27 16:23:16
I think there's a lot for me to learn about data types. Why this happens double result = ((3/8)*100).ToString(); it gives zero .. should be 37,5 ... :( 3/8 performs an integer division and the result is 0 double result = ((3.0/8)*100); should do it. By the way, if you do ((3.0/8)*100).ToString() you get a String and not a double. double result = ((3.0/8.0)*100); Should do it. You were performing integer division, not floating point division. You need to convince the compiler to perform floating point division: double result = (((double)3/8)*100); Otherwise it performs integer division and 3/8

Java int division confusing me

我的梦境 提交于 2019-11-27 15:32:39
I am doing very simple int division and I am getting odd results. This code prints 2 as expected: public static void main(String[] args) { int i = 200; int hundNum = i / 100; System.out.println(hundNum); } This code prints 1 as not expected: public static void main(String[] args) { int i = 0200; int hundNum = i / 100; System.out.println(hundNum); } What is going on here? (Windows XP Pro, Java 1.6 running in Eclipse 3.4.1) The value 0200 is an octal (base 8) constant. It is equal to 128 (decimal). From Section 3.10.1 of the Java Language Specification : An octal numeral consists of an ASCII

Perform integer division using multiplication [duplicate]

让人想犯罪 __ 提交于 2019-11-27 14:44:42
This question already has an answer here: Why does GCC use multiplication by a strange number in implementing integer division? 4 answers Looking at x86 assembly produced by a compiler, I noticed that (unsigned) integer divisions are sometimes implemented as integer multiplications. These optimizations seem to follow the form value / n => (value * ((0xFFFFFFFF / n) + 1)) / 0x100000000 For example, performing a division by 9: 12345678 / 9 = (12345678 * 0x1C71C71D) / 0x100000000 A division by 3 would use multiplication with 0x55555555 + 1 , and so on. Exploiting the fact that the mul instruction

Efficiently implementing floored / euclidean integer division

最后都变了- 提交于 2019-11-27 14:34:05
问题 Floored division is when the result is always floored down (towards −∞), not towards 0: Is it possible to efficiently implement floored or euclidean integer division in C/C++? (the obvious solution is to check the dividend's sign) 回答1: I'm revisiting this question five years later, as this is relevant for me too. I did some performance measurements on two pure-C versions and two inline-assembly versions for x86-64, and the results may be interesting. The tested variants of floored division

VB.NET vs C# integer division [duplicate]

自闭症网瘾萝莉.ら 提交于 2019-11-27 14:28:00
问题 This question already has an answer here: Why do the division (/) operators behave differently in VB.NET and C#? 5 answers Anyone care to explain why these two pieces of code exhibit different results? VB.NET v4.0 Dim p As Integer = 16 Dim i As Integer = 10 Dim y As Integer = p / i //Result: 2 C# v4.0 int p = 16; int i = 10; int y = p / i; //Result: 1 回答1: When you look at the IL-code that those two snippets produce, you'll realize that VB.NET first converts the integer values to doubles,

Should I bit-shift to divide by 2 in Java? [duplicate]

佐手、 提交于 2019-11-27 14:26:16
问题 This question already has answers here : Closed 9 years ago . Possible Duplicates: Is shifting bits faster than multiplying and dividing in Java? .NET? Quick Java Optimization Question Many years ago in college, I learned that bit-shifting right by one accomplishes the same thing as dividing by two, but is generally significantly faster. I'm not sure how Java has come along in that regards since the 9-10 years ago I learned about that. Does the Java compiler automatically converts a divide-by

Fast multiplication/division by 2 for floats and doubles (C/C++)

安稳与你 提交于 2019-11-27 13:56:14
In the software I'm writing, I'm doing millions of multiplication or division by 2 (or powers of 2) of my values. I would really like these values to be int so that I could access the bitshift operators int a = 1; int b = a<<24 However, I cannot, and I have to stick with doubles. My question is : as there is a standard representation of doubles (sign, exponent, mantissa), is there a way to play with the exponent to get fast multiplications/divisions by a power of 2 ? I can even assume that the number of bits is going to be fixed (the software will work on machines that will always have 64 bits

When is the difference between quotRem and divMod useful?

孤街醉人 提交于 2019-11-27 11:19:29
From the haskell report: The quot, rem, div, and mod class methods satisfy these laws if y is non-zero: (x `quot` y)*y + (x `rem` y) == x (x `div` y)*y + (x `mod` y) == x quot is integer division truncated toward zero, while the result of div is truncated toward negative infinity. For example: Prelude> (-12) `quot` 5 -2 Prelude> (-12) `div` 5 -3 What are some examples of where the difference between how the result is truncated matters? ShreevatsaR Many languages have a "mod" or "%" operator that gives the remainder after division with truncation towards 0; for example C, C++, and Java, and

Unexpected result in long/int division

萝らか妹 提交于 2019-11-27 09:14:14
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? 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 get a double result. Since double is greater that int and long, int and long will be turned into double in

How to calculate division remainder in SPARC Assembly?

末鹿安然 提交于 2019-11-27 08:16:01
问题 Here is the pseudo code which computes division of two positive integers. HR register saves remainder, and LR saves dividend. (and eventually saves root) However I think this algorithm has some problem. Because this algorithm sometimes don't recover subtraction.(Division is a continuation of subtraction.) For example 6 / 3 (0110 / 011) This algorithm subtract -3 one more time. (This situation never occur when we calculate this division by hand) So I think this algorithm has some problem. Don