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 =;

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);





✅ 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

  • 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  Is This A Good Idea To Add To My Tumblr?
  • You could try the parseLong method instead, for example:

    String x=””;

    long decval=;

    try {


    } catch (Exception e) {



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

  • Leave a Comment