0

I have to make a multiplication function without the * or / operators. I have already made a method like this.

for(int i=0; i < number1; i++){
    result += number2;
}

System.Out.println(result);

Now, here is my problem: It was fine until my lecturer change the topic, where the multiplication method must be can multiply decimal value. I had no idea how I can make multiplication method which can work on decimal value with just + and - operator.

LittleBobbyTables - Au Revoir
  • 32,008
  • 25
  • 109
  • 114
rizal
  • 101
  • 1
  • 8
  • try handling decimal and integer part seperately and add their result, will that work ? – upog Sep 15 '13 at 13:04
  • i use that trick, but confused when try to return the (,) to their respected position. i'm still learning to code java and i don't know how to code like that hehe.. i'll try googling.. thanks. – rizal Sep 15 '13 at 13:22

4 Answers4

1

yeah you can use log for the multiplication.

log(a*b)=log(a)+log(b)

and then find out the exponential value of log(a)+log(b)

and then you can convert the sign..

for example:

-9*8=-72

log(9*8)=log(9)+log(8)=2.19+2.07=4.27

e^4.27=72

now there is only one -ve no. then it is -72

else it's 72

I'm writing the function for:

void multiply(int num1,int num2)  
{
int counter=0;  
if(num1<0)  
{counter++;num1+=num1+num1;}  
if(num2<0)  
{counter++;num2+=num2+num2;}    
double res=Math.log(num1)+Math.log(num2);  
int result=(int)Math.exp(res);
if(counter%2==0)
System.out.println("the result is:"+result);
else
System.out.println("the result is:-"+result);  
}  

hope this will help you....

BenMorel
  • 34,448
  • 50
  • 182
  • 322
1

You take the decimal numbers and move the decimal point step by step until there is an int left: 0.041 -> 1. step 0.41 -> 2. step 4.1 -> 3. step 41

multiplying 0.041 * 3 could be done by doing the above step 3 times, multiplying 41 * 3 = 123. For the result you take the 123 and undu the steps: 1. 12.3, 2. 1.23, 3. 0.123. There is your result: 0.123 = 0.041 * 3.

Edit: To determine the number of decimals for each number, you might find the answer in this question: How many decimal Places in A Double (Java)

Answers show within others two ways to solve this quite easy: putting the number to a String and checking where in this String the "."-DecimalPoint occurs, or using the BigDecimal type which has a scale()-Method returning the number of decimals.

Community
  • 1
  • 1
Philipp Seeger
  • 140
  • 2
  • 6
0

You shouldn't expect whole perfect code: But here is a hint to achieve this. Try to use recursion technique instead for loops.

public double multiplyMe(double x, double y)
{
   if(y == 0 || x == 0)
     return 0;

   if(y > 0 && x > 0 )
     return (x + multiplyMe(x, y-1)); // multiply positive

   if(y < 0 || x < 0 )
     return - multiplyMe(x, -y); // multiply negative
}

one more way by using log:

10 raise to power ( sum of log10(x) and log10(y) )

Ashwani
  • 3,463
  • 1
  • 24
  • 31
  • did you mean using math.pow on java? i can make negative and positive already because of your code :D – rizal Sep 15 '13 at 13:34
0

This approach might be easier to understand. You have to add a b times, or equivalently, b a times. In addition, you need to handle 4 different cases where a and b can be either positive or negative.

public int multiply(int a, int b){
    int result = 0;

    if (a < 0 && b < 0){
        for (int i = a; i <= -1; i++)
            result-=b;    
    }
    else if (a < 0){
        for (int i = 1; i <= b; i++)
            result+=a;
    }
    else if (b < 0){
        for (int i = 1; i <= a; i++)
            result+=b;
    }
    else {
        for (int i = 1; i <= b; i++)
            result+=a;    
    }
    return result;
}

public static void main(String[] args){
    System.out.println(multiply(3,-13)); // -39
}
leocl1
  • 161
  • 4