How do I raise a number to power in java (conversion of binary to decimal class)?

When I try to compile this code, I get a possiple loss of precision….anyone know what I am doing wrong.

String x = sc.next();

length = x.length();

StringBuffer buffer = new StringBuffer(x);

buffer = buffer.reverse();

while ( length > – ) {

char ch = x.charAt(length);

if (ch == ”) {

decval = decval + Math.pow(,pow);

}

–length;

++pow;

}

✅ Answers

? Favorite Answer

  • Possible loss of precision is usually from casting a double (or float) into an int (or long, short, byte).

    Since you have not shown the rest of your code, I will assume that decval is an int. Since Math.pow returns a double, this is where the possible loss of precision would be occuring.

    How to fix it? Chane decval to be a double not an int.

    Hope that helps.

  • Source(s): Phone Finder http://reversephonenumberlookup.enle.info/?TQ

  • straight forward replace of all references to the huge form (which comprise in the st cout fact) to the huge form sixteen. i don’t think that it will actual convert a binary huge form to a decimal huge form, because of the fact it does not think appropriate to the region value of the ‘a million’ digit. it may be this: if (b[i] == ‘a million’) sum += pow(, sixteen – i); additionally, there is yet another vast assumption. That the climate of the ‘b’ variable are already assigned to the NULL character. There could desire to be a decision like this in the present day after the “char b[];”: memset(b, , sixteen); And to further simplify issues, it rather is greater effective to apply a macro or a relentless variable to define the size of the array and how long it loops, by way of utilising something like this: #define NUMDIGITS or const int numdigits = ; Then, the place you spot the huge form in the code, it may get replaced by way of the two “NUMDIGITS”, or “numdigits”, based upon which one you chosen to apply. And the st cout() fact could desire to be greater like this: cout << “Please enter a binary huge form as much as ” << NUMDIGITS << ” digits…nn”; right here is the code with the adjustments I suggested in place: #comprise <iostream> #comprise <cmath> #comprise <string.h> utilising namespace std; #define NUMDIGITS sixteen int substantial() {    char b[NUMDIGITS];    memset(&b, , NUMDIGITS);    cout << “Please enter a binary huge form as much as ” << NUMDIGITS << ” digits…nn”;    cin >> b;    int sum = , i=;    mutually as (i < NUMDIGITS && b[i] != (char)) { //each and each ingredient of b[] is initialized to (char) – NUL       if (b[i] == ‘a million’) sum += pow(, NUMDIGITS – i);       else if (b[i] == ”) ;       else cout << ” you’re actually not inputing a binary huge form! n”;       i++;    }    cout << sum;    char c; cin >> c;    return ; } //end substantial

    See also  Visual basic!! Comboboxes HELP! Please :o x?
  • You could try the parseLong method instead, for example:

    String x=””;

    long decval=;

    try {

    decval=Long.parseLong(x,);

    } catch (Exception e) {

    System.err.println(e);

    }

    System.out.println(“Decimal value = “+decval);

  • Other Related Questions

    Learning Visual Basic ?

    Answers Favorite AnswerTry using "System.Diagnostics.Process. GetProcessesByName( "iwmp" ).Length > " for your check.

    Microsoft Office word Fast answer needed its urgent?

    Answers Favorite AnswerTry this:http://www.techsupport.com///microsoft-wo...Here is another thread re: the same issue with instructions:http://www.pcreview.co.uk/forums/modification-not-...http://www.techsupport.com///microsoft-wo...http://support.microsoft.com/kb/http://answers.microsoft.com/en-us/office/forum/of...

    Can I download a whole city android google maps?

    Answers Favorite Answer:) Yes!!!Interesting question. I wonder why it has blocked in this way especially considering that Google Earth is very detailed and has good maps of Israel.

    What is meant by ROM ? Explain in simple but elaborate terms.What about mobile ROM’s?

    Answers Favorite AnswerHi Diva below is a link that will give a simple answer.http://wiki.answers.com/Q/What_does_ROM_stand_for_...Hope this helps.Source(s): Experience and wiki answers.ROM is Read only memory. i.e data can write only once.There two types of ROM..ROM.PROMThe difference between ROM and PROM.that is ROM is programmed during manufacturing it means data stored by manufacturing company.PROM is blank memory that a user programmable memory.user can store content on PROM.both ROM and PROM are Read only memory Data can write only once.and its not possible to write so many time.Memories of PROM and ROM are Non-volatile in nature. Its that stored informations can retain even power goes off.

    Leave a Comment