# Power in java

• 04-19-2011, 07:21 PM
someone
Power in java
Hello folks

Each protein sequences can be converted into an integer value by multiplying the ASCII value of its left most character by 1280, that its second left most character by 1281, etc, and then adding up the resulting 4 values (The ASCII value for A = 65, B = 66, C = 67, …, Y = 89.)
For example the integer value for the sequence AFDE is:

I want to convert this calculation to java

(65 x 1283) + (70 x 1282) + (68 x 1281) + (69 x 1280) = 137470533

the numbers

-------------------------------------------------
Code:

```  double a = (Aint * (Math.pow( 128,3))) + (Fint * (Math.pow( 128,2))) + (Dint * (Math.pow( 128,1)))+ (Eint * (Math.pow( 128,0)));                     System.out.print(a);```

-------------------------------

I got 1.37470533E8

why? any one has idea?
• 04-19-2011, 07:25 PM
Dark
Um, thats the right answer is it not? Are you looking for it without the power?
• 04-19-2011, 07:27 PM
someone
The right answer should be look like this 137470533
• 04-19-2011, 07:38 PM
Dark
Right, your problem is not with your math but your formatting. When you use Math.pow your answer will be in Scientific Notation. Look into converting it into an Int.
• 04-19-2011, 07:40 PM
JosAH
Quote:

Originally Posted by someone
The right answer should be look like this 137470533

Math.pow(128, 3) != 1283; and nor is this true for the other values.

kind regards,

Jos
• 04-19-2011, 07:44 PM
Dark
@JosAH doesn't 1.37470533E8 mean the same thing as 137,470,533?
• 04-19-2011, 08:06 PM
JosAH
Quote:

Originally Posted by Dark
@JosAH doesn't 1.37470533E8 mean the same thing as 137,470,533?

Erm, I should put the lights on; I'm getting old, I didn't see it clearly ;-) Or just my deformed mind imagined the number ending in E38 ...

kind regards,

Jos
• 04-19-2011, 08:09 PM
Dark
Its been a while since I've dealt with scientific notation, but I was pretty sure he's getting the expected result. He just wants it displayed in a different form.

He should be able to cast it to an int to make it display in long form should he not?
• 04-19-2011, 08:13 PM
JosAH
Quote:

Originally Posted by Dark
Its been a while since I've dealt with scientific notation, but I was pretty sure he's getting the expected result. He just wants it displayed in a different form.

He should be able to cast it to an int to make it display in long form should he not?

Yep, that number fits in an int so the cast works.

kind regards,

Jos
• 04-19-2011, 08:16 PM
Dark
However, I have no idea whether or not with the formula you will breach 2.3 billion something. If you do, then I suggest casting to long.
• 04-19-2011, 08:19 PM
JosAH
Quote:

Originally Posted by Dark
However, I have no idea whether or not with the formula you will breach 2.3 billion something. If you do, then I suggest casting to long.

You can handle only four characters or so in an int range but a long can only handle nine characters. Each characters takes up seven bits so you can only handle characters in the seven bit ASCII range. b.t.w. bitshifts would be more efficient than Math.pow( ... ) operations ...

kind regards,

Jos
• 04-20-2011, 08:09 AM
someone
it is the same answer but I think the problem is that I should store it as int not double
any one has idea on how to do that
• 04-20-2011, 08:14 AM
Junky
Obviously you didn't read all the replies.
• 04-20-2011, 08:28 AM
JosAH
Quote:

Originally Posted by someone
it is the same answer but I think the problem is that I should store it as int not double
any one has idea on how to do that

Either do all the calculations in the int domain (I prefer this method) or cast your result back to the int domain; a simple (int) cast does the job.

kind regards,

Jos
• 04-20-2011, 09:29 AM
Dark
Quote:

Originally Posted by Junky
Obviously you didn't read all the replies.

^This, most definitely this.