Solved

Problems with using pow

Posted on 2006-11-10
4
337 Views
Last Modified: 2006-11-18
I have a small problem with using pow.

public class Main {
   
    static public int bin2int(int[] array) {
        int value=0;        
        System.out.println("al: "+array.length);
        for(int i = 0, i2 = array.length-1; i < array.length; i++, --i2) {
            value += Math.pow(array[i]*2,i2); ///////////// here the problem is
            System.out.println(array[i]+"*2^"+i2+"="+value);
        }
        System.out.println("dec val: "+value);
        return value;
    }
   
    public static void main(String[] args) {
        // TODO code application logic here
        int buffer[] = {0,0,0};
        bin2int(buffer);
    }
}

gives me:

al: 3
0*2^2=0
0*2^1=0
0*2^0=1
dec val: 1

It should not give me 1 when using pow(0*2,0). How to deal with this?
0
Comment
Question by:m-jansen
  • 2
  • 2
4 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 125 total points
Comment Utility
>>It should not give me 1 when using pow(0*2,0). How to deal with this?

Yes, x^0=1
0
 

Author Comment

by:m-jansen
Comment Utility
sorry I got the solution... now
value += array[i]*Math.pow(2,i2);
0
 

Author Comment

by:m-jansen
Comment Utility
I'll accept your answer anyway
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
:-)
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
The viewer will learn how to implement Singleton Design Pattern in Java.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now