integer-division

What determines the sign of m % n for integers?

戏子无情 提交于 2019-12-01 15:33:08
问题 The modulo in Python is confusing. In Python, % operator is calculating the remainder: >>> 9 % 5 4 However: >>> -9 % 5 1 Why is the result 1 ? and not -4 ? 回答1: Because in python, the sign matches the denominator. >>> 9 % -5 -1 >>> -9 % 5 1 For an explanation of why it was implemented this way, read the blog post by Guido. 回答2: -10 % 5 is 0, ie, -10 is evenly divided by 5. You ask why -9 % 5 is not -4, and the answer is that both 1 and -4 can be correct answers, it depends on what -9 divided

ColdFusion too big to be an integer

≯℡__Kan透↙ 提交于 2019-12-01 03:54:49
I am trying to convert a large number going in to Megabytes. I don't want decimals numeric function formatMB(required numeric num) output="false" { return arguments.num \ 1024 \ 1024; } It then throws an error How do I get around this? You can't change the size of a Long, which is what CF uses for integers. So you'll need to BigInteger instead: numeric function formatMB(required numeric num) { var numberAsBigInteger = createObject("java", "java.math.BigInteger").init(javacast("string", num)); var mbAsBytes = 1024 ^ 2; var mbAsBytesAsBigInteger = createObject("java", "java.math.BigInteger")

Haskell Novice Trouble with Splitting a List in Half

放肆的年华 提交于 2019-12-01 03:52:53
问题 Here's my attempt to write a function that splits a list of even length into two equal halves. halve :: [a] -> ([a], [a]) halve x | even len = (take half x, drop half x) | otherwise = error "Cannnot halve a list of odd length" where len = length x half = len / 2 I am getting the following error: No instance for (Fractional Int) arising from a use of ‘/’ In the expression: len / 2 In an equation for ‘half’: half = len / 2 In an equation for ‘halve’: I don't understand the error but I have a

Is div function useful (stdlib.h)? [duplicate]

懵懂的女人 提交于 2019-12-01 02:29:38
This question already has an answer here: What is the purpose of the div() library function? 5 answers There is a function called div in C,C++ (stdlib.h) div_t div(int numer, int denom); typedef struct _div_t { int quot; int rem; } div_t; But C,C++ have / and % operators. My question is: " When there are / and % operators, Is div function useful ?" psYchotic The div() function returns a structure which contains the quotient and remainder of the division of the first parameter (the numerator) by the second (the denominator). There are four variants: div_t div(int, int) ldiv_t ldiv(long, long)

how to calculate (a times b) divided by c only using 32-bit integer types even if a times b would not fit such a type

安稳与你 提交于 2019-11-30 22:29:49
Consider the following as a reference implementation: /* calculates (a * b) / c */ uint32_t muldiv(uint32_t a, uint32_t b, uint32_t c) { uint64_t x = a; x = x * b; x = x / c; return x; } I am interested in an implementation (in C or pseudocode) that does not require a 64-bit integer type. I started sketching an implementation that outlines like this: /* calculates (a * b) / c */ uint32_t muldiv(uint32_t a, uint32_t b, uint32_t c) { uint32_t d1, d2, d1d2; d1 = (1 << 10); d2 = (1 << 10); d1d2 = (1 << 20); /* d1 * d2 */ return ((a / d1) * (b /d2)) / (c / d1d2); } But the difficulty is to pick

Modulus division when first number is smaller than second number

喜欢而已 提交于 2019-11-30 18:14:40
I apologize if this is a simple question but I'm having trouble grasping the concept of modulus division when the first number is smaller than the second number. For example when 1 % 4 my book says the remainder is 1. I don't understand how 1 is the remainder of 1 % 4. 1 / 4 is 0.25. Am I thinking about modulus division incorrectly? First, in Java, % is the remainder (not modulo) operator, which has slightly different semantics. That said, you need to think in terms of integer-only division, as if there were no fractional values. Think of it as storing items that cannot be divided: you can

How to perform division in Go

强颜欢笑 提交于 2019-11-30 07:47:50
I am trying to perform a simple division in Go. fmt.Println(3/10) This prints 0 instead of 0.3. This is kind of weird. Could someone please share what is the reason behind this? i want to perform different arithmetic operations in Go. Thanks The expression 3 / 10 is an untyped constant expression. The specification says this about constant expressions if the operands of a binary operation are different kinds of untyped constants, the operation and, for non-boolean operations, the result use the kind that appears later in this list: integer, rune, floating-point, complex. Because 3 and 10 are

Why is modulus operator slow?

半城伤御伤魂 提交于 2019-11-30 07:12:58
Paraphrasing from in "Programming Pearls" book (about c language on older machines, since book is from the late 90's): Integer arithmetic operations ( + , - , * ) can take around 10 nano seconds whereas the % operator takes up to 100 nano seconds. Why there is that much difference? How does a modulus operator work internally? Is it same as division ( / ) in terms of time? The modulus/modulo operation is usually understood as the integer equivalent of the remainder operation - a side effect or counterpart to division. Except for some degenerate cases (where the divisor is a power of the

How to check if given number is divisible of 15 in fastest way?

☆樱花仙子☆ 提交于 2019-11-30 04:51:24
Division in processor takes much time, so I want to ask how to check in fastest way if number is divisible of some other number, in my case I need to check if number is divisible by 15. Also I've been looking through web and found fun ways to check if number is divisible of some number, but I'm looking for fast option. NOTE: as division takes much time I'm looking for answer without / and % . ST3 Multiplication takes less time then division, so you can try this: inline bool divisible15(unsigned int x) { //286331153 = (2^32 - 1) / 15 //4008636143 = (2^32) - 286331153 return x * 4008636143u <=

Modulus division when first number is smaller than second number

僤鯓⒐⒋嵵緔 提交于 2019-11-30 01:14:04
问题 I apologize if this is a simple question but I'm having trouble grasping the concept of modulus division when the first number is smaller than the second number. For example when 1 % 4 my book says the remainder is 1. I don't understand how 1 is the remainder of 1 % 4. 1 / 4 is 0.25. Am I thinking about modulus division incorrectly? 回答1: First, in Java, % is the remainder (not modulo) operator, which has slightly different semantics. That said, you need to think in terms of integer-only