integer-division

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

Assembly Language - How to Do Modulo?

牧云@^-^@ 提交于 2019-11-26 08:08:49
问题 Is there something like a modulo operator or instruction in x86 assembly? 回答1: If your modulus / divisor is a known constant, and you care about performance, see this and this. A multiplicative inverse is even possible for loop-invariant values that aren't known until runtime, e.g. see https://libdivide.com/ (But without JIT code-gen, that's less efficient than hard-coding just the steps necessary for one constant.) Never use div for known powers of 2: it's much slower than and for remainder,

Dividing two integers to produce a float result [duplicate]

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-26 07:38:18
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Why can't I return a double from two ints being divided My C++ program is truncating the output of my integer devision even when I try and place the output into a float. How can I prevent this whilst keeping those to variables (a & b) as integers? user@box:~/c/precision$ cat precision.cpp #include <iostream> #include <iomanip> using namespace std; int main() { int a = 10, b = 3; float ans = (a/b); cout<<fixed<

How to get fractions in an integer division? [duplicate]

时间秒杀一切 提交于 2019-11-26 06:02:57
问题 This question already has an answer here: How to make the division of 2 ints produce a float instead of another int? 9 answers How do you divide two integers and get a double or float answer in C? 回答1: You need to cast one or the other to a float or double . int x = 1; int y = 3; // Before x / y; // (0!) // After ((double)x) / y; // (0.33333...) x / ((double)y); // (0.33333...) Of course, make sure that you are store the result of the division in a double or float ! It doesn't do you any good

Integer division with remainder in JavaScript?

懵懂的女人 提交于 2019-11-26 05:39:18
In JavaScript, how do I get: the whole number of times a given integer goes into another? the remainder? Mark Elliot For some number y and some divisor x compute the quotient ( quotient ) and remainder ( remainder ) as: var quotient = Math.floor(y/x); var remainder = y % x; user113716 I'm no expert in bitwise operators, but here's another way to get the whole number: var num = ~~(a / b); This will work properly for negative numbers as well, while Math.floor() will round in the wrong direction. This seems correct as well: var num = (a / b) >> 0; I did some speed tests on Firefox. -100/3 // -33

Rounding integer division (instead of truncating)

自作多情 提交于 2019-11-26 03:53:46
问题 I was curious to know how I can round a number to the nearest whole number. For instance, if I had: int a = 59 / 4; which would be 14.75 if calculated in floating point; how can I store the result as 15 in \"a\"? 回答1: int a = 59.0f / 4.0f + 0.5f; This only works when assigning to an int as it discards anything after the '.' Edit: This solution will only work in the simplest of cases. A more robust solution would be: unsigned int round_closest(unsigned int dividend, unsigned int divisor) {

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

Integer division with remainder in JavaScript?

邮差的信 提交于 2019-11-26 01:55:40
问题 In JavaScript, how do I get: the whole number of times a given integer goes into another? the remainder? 回答1: For some number y and some divisor x compute the quotient ( quotient ) and remainder ( remainder ) as: var quotient = Math.floor(y/x); var remainder = y % x; 回答2: I'm no expert in bitwise operators, but here's another way to get the whole number: var num = ~~(a / b); This will work properly for negative numbers as well, while Math.floor() will round in the wrong direction. This seems

Divide by 10 using bit shifts?

余生颓废 提交于 2019-11-26 00:48:10
问题 Is it possible to divide an unsigned integer by 10 by using pure bit shifts, addition, subtraction and maybe multiply? Using a processor with very limited resources and slow divide. 回答1: Editor's note: this is not actually what compilers do, and gives the wrong answer for large positive integers ending with 9, starting with div10(1073741829) = 107374183 not 107374182. It is exact for smaller inputs, though, which may be sufficient for some uses. Compilers (including MSVC) do use fixed-point

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