multiplication

Floating point multiplication in java [duplicate]

…衆ロ難τιáo~ 提交于 2019-12-20 04:50:17
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: How to round a number to n decimal places in Java When multiplying two numbers in java happens this: double a = 9.495 * 100; Expected result: a = 949.5; But the obtained result is: a = 949.4999999999999 When I try to round number 9.495 in two decimal places the result is 9.49 instead of 9.50 Any ideas how to solve this problem? 回答1: If you want accurate floating point computations, do not use the float or double

Product between all combinations of a vector's elements

戏子无情 提交于 2019-12-19 21:25:39
问题 Suppose I have a vector c(1, 2, 3, 4) with no duplicated values. I need a vector c(1 * 2, 1 * 3, 1 * 4, 2 * 3, 2 * 4, 3 * 4) , so the multiplication is done in all possible combinations of this vector's values. Is there a way of doing that? Thanks in advance! 回答1: We can use combn with anonymous function call combn(vec, 2, FUN = function(x) x[1] * x[2]) #[1] 2 3 4 6 8 12 data vec <- 1:4 回答2: This is fun enough. I thought combn(1:4, 2, "*") would be the simplest solution but it actually does

Multiplying two 32 bit numbers without using 64 bit int

寵の児 提交于 2019-12-19 10:15:18
问题 We are doing some 32bit * 32bit multiplication using the following algorithm Let us we want to multiply a (32 bit) with b (32 bit), both signed, a = ah * 2^16 + al [ah - Higher 16 bits, al - Lower 16 bits] b = bh * 2^16 + bl [bh - Higher 16 bits, bl - Lower 16 bits] We are effectively doing Result = (al * bl) + (((ah * bl) + (al * bh)) * 2^16) + ((ah * bh) * 2 ^ 32) ~~~ My question, Is their any better way to do this? 回答1: In any mainstream compiler, the emulation of 64-bit ints on a 32-bit

How to accelerate slow matrix multiplication in SymPy?

跟風遠走 提交于 2019-12-19 09:52:56
问题 I was writing a tool to solve specific recurrence equations with SymPy, and found that one of the steps that involved a matrix multiplication was taking an extraordinarily long time. For instance, if I try the following in the iPython console, In [1]: from sympy import * In [2]: A = Matrix(500, 500, lambda i,j: 2 + abs(i-j) if i-j in [-1, 0, 1] else 0) In [3]: A[0:7, 0:7] Out[3]: Matrix([ [2, 3, 0, 0, 0, 0, 0], [3, 2, 3, 0, 0, 0, 0], [0, 3, 2, 3, 0, 0, 0], [0, 0, 3, 2, 3, 0, 0], [0, 0, 0, 3,

Python long multiplication

空扰寡人 提交于 2019-12-19 08:22:50
问题 I'm in need of an algorithm faster than the current normal Python long multiplication. I tried to find a decent Karatsuba implementation, but I can't. def main(): a=long(raw_input()) if(a<0): a=a*-1 a=((a*(a+1)/2)-1) print(-a) else: a=(a*(a+1))/2 print(a) main() As you see, it's nothing complicated, just a few multiplications. But it has to handle numbers with up to 100000 digits in under 2.5 sec. I'd like some snippet of a function or just a link to some implementation of a faster

Python long multiplication

不问归期 提交于 2019-12-19 08:21:10
问题 I'm in need of an algorithm faster than the current normal Python long multiplication. I tried to find a decent Karatsuba implementation, but I can't. def main(): a=long(raw_input()) if(a<0): a=a*-1 a=((a*(a+1)/2)-1) print(-a) else: a=(a*(a+1))/2 print(a) main() As you see, it's nothing complicated, just a few multiplications. But it has to handle numbers with up to 100000 digits in under 2.5 sec. I'd like some snippet of a function or just a link to some implementation of a faster

How to implement multiplication without using multiplication operator in .NET [closed]

南笙酒味 提交于 2019-12-19 03:59:59
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 7 years ago . I want to implement multiplication of two integer numbers without using multiplication operator, in .NET public uint MultiplyNumbers(uint x, uint y) { } Any idea! 回答1: I'm assuming this is homework... otherwise

Russian Peasant Multiplication

余生颓废 提交于 2019-12-18 13:39:29
问题 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); 回答1: 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.

Fork Join Matrix Multiplication in Java

邮差的信 提交于 2019-12-18 07:14:25
问题 I’m doing some performance research on the fork/join framework in Java 7. To improve the test results I want to use different recursive algorithms during the tests. One of them is multiplying matrixes. I downloaded the following example from Doug Lea's website (): public class MatrixMultiply { static final int DEFAULT_GRANULARITY = 16; /** The quadrant size at which to stop recursing down * and instead directly multiply the matrices. * Must be a power of two. Minimum value is 2. **/ static

How to multiply all values in an array?

不羁岁月 提交于 2019-12-18 06:08:25
问题 I have an assignment where I need to find the product of all of the numbers in an array, I'm not sure how to do this. int[] numbers = new int[SIZE]; Console.WriteLine("Type in 10 numbers"); Console.WriteLine("To stop, type in 0"); for (int input = 0; input < SIZE; input++) { userInput = Console.ReadLine(); numberInputed = int.Parse(userInput); if (numberInputed == ZERO) { numberInputed = ONE; break; } else { numbers[input] = numberInputed; } } This is where I'm trying to find the product of