integer-arithmetic

Prolog: predicate for maximum without accumulator

爱⌒轻易说出口 提交于 2019-12-01 16:42:20
Is it possible to create a predicate max/2 without an accumulator so that max(List, Max) is true if and only if Max is the maximum value of List (a list of integers)? Willem Van Onsem Yes, you can calculate the maximum after the recursive step. Like: max([M],M). % the maximum of a list with one element is that element. max([H|T],M) :- max(T,M1), % first calculate the maximum of the tail. M is max(H,M1). % then calculate the real maximum as the max of % head an the maximum of the tail. This predicate will work on floating points for instance. Nevertheless it is better to use an accumulator

Unsigned arithmetic and integer overflow

拥有回忆 提交于 2019-12-01 01:08:16
I am trying to understand arithmetic overflow. Suppose I have the following, unsigned long long x; unsigned int y, z; x = y*z; y*z can lead to an integer overflow. Does casting one of the operands to an unsigned long long alleviate this issue. What is the expected result of the multiplication of a 64-bit operand with a 32-bit operand? unsigned long long x; unsigned int y, z; x = y*z; The evaluation of the expression y*z is not affected by the context in which it appears. It multiplies two unsigned int values, yielding an unsigned int result. If the mathematical result cannot be represented as

Unsigned arithmetic and integer overflow

拟墨画扇 提交于 2019-11-30 20:26:25
问题 I am trying to understand arithmetic overflow. Suppose I have the following, unsigned long long x; unsigned int y, z; x = y*z; y*z can lead to an integer overflow. Does casting one of the operands to an unsigned long long alleviate this issue. What is the expected result of the multiplication of a 64-bit operand with a 32-bit operand? 回答1: unsigned long long x; unsigned int y, z; x = y*z; The evaluation of the expression y*z is not affected by the context in which it appears. It multiplies

Multiplying two 128-bit ints

喜欢而已 提交于 2019-11-30 20:13:01
I'm trying to multiply two 128-bit integers in C. Here is my algorithm: Split the two 128-bit sequences into S1 and S2. Then split S1 into S11 (front/higher half) and S12 (back/lower half) and split S2 into S21 (front/higher half) and S22 (back/lower half). Multiply S12 by S22... = S1222. Multiply S11 by S21... = S1121 and then bit shift it by multiplying it by 2^128 Combine S1222 and S1121 to be the front and back halves of a new array. Let's call it "Array1". The new array's length is twice as long as S1. Then we have to multiply S12 by S21 and S11 by S22. I have multiplied these two to get

How is it possible that BITWISE AND operation to take more CPU clocks than ARITHMETIC ADDITION operation in a C program?

半城伤御伤魂 提交于 2019-11-29 16:40:53
I wanted to test if bitwise operations really are faster to execute than arithmetic operation. I thought they were. I wrote a small C program to test this hypothesis and to my surprise the addition takes less on average than bitwise AND operation. This is surprising to me and I cannot understand why this is happening. From what I know for addition the carry from the less significant bits should be carried to the next bits because the result depends on the carry too. It does not make sense to me that a logic operator is slower than addition. My cod is below: #include<stdio.h> #include<time.h>

Why int plus uint returns uint?

会有一股神秘感。 提交于 2019-11-29 13:47:07
int plus unsigned int returns an unsigned int. Should it be so? Consider this code: #include <boost/static_assert.hpp> #include <boost/typeof/typeof.hpp> #include <boost/type_traits/is_same.hpp> class test { static const int si = 0; static const unsigned int ui = 0; typedef BOOST_TYPEOF(si + ui) type; BOOST_STATIC_ASSERT( ( boost::is_same<type, int>::value ) ); // fails }; int main() { return 0; } If by "should it be" you mean "does my compiler behave according to the standard": yes . C++2003: Clause 5, paragraph 9: Many binary operators that expect operands of arithmetic or enumeration type

How to do 64 bit multiply on 16 bit machine?

独自空忆成欢 提交于 2019-11-29 12:48:06
I have an embedded 16 bit CPU. On this machine ints are 16 bit wide and it supports longs that are 32 bits wide. I need to do some multiplications that will need to be stored in 64 bits (e.g. multiply a 32 bit number by a 16 bit number). How can I do that with the given constraints? I do not have a math library to do this. Just another metaprogrammer A suggestion in C. Note that this code probably will be easier to implement with inline assembler as carry detection in C doesn't seem that easy // Change the typedefs to what your compiler expects typedef unsigned __int16 uint16 ; typedef

How can I calculate (A*B)%C for A,B,C <= 10^18, in C++?

喜你入骨 提交于 2019-11-29 11:08:29
For example, A=10^17, B=10^17, C=10^18. The product A*B exceeds the limit of long long int. Also, writing ((A%C)*(B%C))%C doesn't help. Assuming you want to stay within 64-bit integer operations, you can use binary long division, which boils down to a bunch of adds and multiply by two operations. This means you also need overflow-proof versions of those operators, but those are relatively simple. Here is some Java code that assumes A and B are already positive and less than M. If not, it's easy to make them so beforehand. // assumes a and b are already less than m public static long addMod

How can I detect integer overflow on 32 bits int?

狂风中的少年 提交于 2019-11-28 23:09:55
I know such topic was asked several times, but my question is about overflow on full 32 bits of int . For example: 11111111111111111111111111111111 + 00000000000000000000000000000001 = 00000000000000000000000000000000 //overflow! I found topic with similar question about this, however the algorithm is not perfect. 11111111111111111111111111111111 + 00000000000000000000000000000000 = 00000000000000000000000000000000 //overflow! Is there any simple and fast way to check this ? Since Java 8 there is a set of methods in the Math class: toIntExact (long), addExact (int,int), subtractExact (int,int)

Perfect square and perfect cube

北慕城南 提交于 2019-11-28 21:53:13
Is there any predefined function in c++ to check whether the number is square of any number and same for the cube.. No, but it's easy to write one: bool is_perfect_square(int n) { if (n < 0) return false; int root(round(sqrt(n))); return n == root * root; } bool is_perfect_cube(int n) { int root(round(cbrt(n))); return n == root * root * root; } sqrt(x) , or in general, pow(x, 1./2) or pow(x, 1./3) For example: int n = 9; int a = (int) sqrt((double) n); if(a * a == n || (a+1) * (a+1) == n) // in case of an off-by-one float error cout << "It's a square!\n"; Edit: or in general: bool is_nth