multiplication

Russian Peasant Multiplication

▼魔方 西西 提交于 2019-11-30 10:54:14
Here is my short implementation of Russian Peasant Multiplication . How can it be improved? Restrictions : only works when a>0,b>0 for(p=0;p+=(a&1)*b,a!=1;a>>=1,b<<=1); Svante It can be improved by adding whitespace, proper indentation, and a proper function body: int peasant_mult (int a, int b) { for (p = 0; p += (a & 1) * b, a != 1; a /= 2, b *= 2); return p;} See? Now it's clear how the three parts of the for declaration are used. Remember, programs are written mainly for human eyes. Unreadable code is always bad code. And now, for my personal amusement, a tail recursive version: (defun

Multiply 2D Matrix with vector to span third dimension - MATLAB

隐身守侯 提交于 2019-11-30 09:43:17
问题 As I am trying to multiply a m x n Matrix with a p-dimensional vector, I am stumbling across some difficulties. Trying to avoid for loops, here is what I am looking to achieve enter code here M = [1 2 3; p = [1;2;3] 4 5 6; 7 8 9] I want to obtain a 3x3x3 matrix, where the slices in third dimension are simply the entries of M multiplied by the respective entry in p . Help is much appreciated 回答1: You can use bsxfun with permute for a vectorized (no-loop) approach like so - out = bsxfun(@times

Signed & unsigned integer multiplication

两盒软妹~` 提交于 2019-11-30 05:43:36
问题 In fixed point math I use a lot of 16bit signals and perform multiplication with 32bit intermediate results. For example: int16_t a = 16384; //-1.0q14 or 1.0*2^14 int16_t b = -24576; // -1.4q14 or 1.4*2^14 int16_t c; // result will be q14 c = (int16_t)(((int32_t)a * (int32_t)b)>>14); Lets say a is a q14 number then c with have the same scaling as b. This is fine and works for unsigned as well as signed arithmetic. The question is: What happens if I were to mix types? For example if I know the

How to multiply two quaternions with minimal instructions?

天涯浪子 提交于 2019-11-30 05:13:44
After some thought, I came up with the following code for multiplying two quaternions using SSE: #include <pmmintrin.h> /* SSE3 intrinsics */ /* multiplication of two quaternions (x, y, z, w) x (a, b, c, d) */ __m128 _mm_cross4_ps(__m128 xyzw, __m128 abcd) { /* The product of two quaternions is: */ /* (X,Y,Z,W) = (xd+yc-zb+wa, -xc+yd+za+wb, xb-ya+zd+wc, -xa-yb-zc+wd) */ __m128 wzyx = _mm_shuffle_ps(xyzw, xyzw, _MM_SHUFFLE(0,1,2,3)); __m128 baba = _mm_shuffle_ps(abcd, abcd, _MM_SHUFFLE(0,1,0,1)); __m128 dcdc = _mm_shuffle_ps(abcd, abcd, _MM_SHUFFLE(2,3,2,3)); /* variable names below are for

how to do two complement multiplication and division of integers?

别说谁变了你拦得住时间么 提交于 2019-11-30 03:45:06
I have read this post on binary multiplication using two complement. but it is not very clear to me. Even I have difficulty understanding the wiki article on this. I want to know how to go about calculating multiplications of negative numbers: eg: -1 with -7 should give 7. A 4-bit, 2's complement of -1 is : 1111 A 4-bit, 2's complement of -7 is : 1001 some step-wise way of calculating the multiplication will be helpful. No article I came across talks about division. How to approach this? step 1: sign extend both integers to twice as many bits. This is safe to do, though may not always be

Error CS0051 (Inconsistent accessibility: parameter type 'Job' is less accessible than method 'AddJobs.TotalPay(Job)')

巧了我就是萌 提交于 2019-11-30 02:37:44
问题 I compiled and ran the source code below successfully by omitting the totalFee field. How do I write totalFee into this program so that it will accurately calculate the total fee for each job (rate * time)? Below, you'll see I tried using a method; which generated the error CS0051 (Inconsistent accessibility: parameter type 'Job' is less accessible than method 'AddJobs.TotalPay(Job)'). This source code is in response to the following assignment: "Design a Job class for Harold’s Home Services.

Is MOD operation more CPU intensive than multiplication?

隐身守侯 提交于 2019-11-30 01:16:56
问题 Why is MOD operation more expensive than multiplication by a bit more than a factor of 2 ? Please be more specific about how CPU performs division operation and returns the result for MOD operation. In the following example the threads each run for a second. The test was performed on a SPARC processor. // multiplication void someThread() { int a = 10234; while (true) { opers++; a = a * a; a++; } // opers ~ 26 * 10^6 in a sec. } // MOD void someThread() { int a = 10234; while (true) { opers++;

Is integer multiplication really done at the same speed as addition on a modern CPU?

六眼飞鱼酱① 提交于 2019-11-29 20:53:59
I hear this statement quite often, that multiplication on modern hardware is so optimized that it actually is at the same speed as addition. Is that true? I never can get any authoritative confirmation. My own research only adds questions. The speed tests usually show data that confuses me. Here is an example: #include <stdio.h> #include <sys/time.h> unsigned int time1000() { timeval val; gettimeofday(&val, 0); val.tv_sec &= 0xffff; return val.tv_sec * 1000 + val.tv_usec / 1000; } int main() { unsigned int sum = 1, T = time1000(); for (int i = 1; i < 100000000; i++) { sum += i + (i+1); sum++;

Multiplication of three numbers in c give a wrong results?

这一生的挚爱 提交于 2019-11-29 18:30:20
I can't belive what's happen in my program double den = 180*3600*10000 ; in debugging a got this value -2109934592.0000000 any help please ??? you can try this simple code #include<stdio.h> #include<math.h> int main ( int argc , char *argv ) { double denominator = 10000*180*3600 ; printf("%f \n", denominator ) ; return 0 ; } With the full code in the question we can now see it's an integer overflow. 10000 * 180 * 3600 = 6,480,000,000. This is greater than 2,147,483,648 which is the max value of a 32-bit signed int. The results of the multiplication overflows to -2,109,934,592 and is then

Multiply two 100-Digit Numbers inside Excel Using Matrix

末鹿安然 提交于 2019-11-29 18:17:06
I want to multiply two 100-Digit Numbers In Excel using matrix. The issue in Excel is that after 15-digit, it shows only 0. So, the output also need to be in a Matrix. 1st Number: "9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999" 2nd Number: "2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222" Output: