1

I wrote a program in java and php. In which a loop runs 64 times. And keep adding n to n:

Java Code:

  public static void main(String[] args) {
    double n = 1;
    double p = 1;
    for(int i = 1;i <= 64;i++){
        n = n + n;
        p = p + n;
    }
    System.out.println(p);
}

PHP code:

<?php
    $n = 1;
    $p = 0;
    for($i = 1;$i <= 64;$i++){
        $n = $n + $n;
        $p = $p + $n;
    }
    echo($p);

?>

And the output of both of these is:

3.6893488147419E+19

Now I want to know is it possible to convert this big float to int? if yes, Then how. In both languages.

mega6382
  • 9,211
  • 17
  • 48
  • 69
  • Why use double for n and p in the first place? Maybe you mean long? See http://stackoverflow.com/questions/415924/what-is-the-purpose-of-long-double-byte-char-in-java. Also, if above what a 64 bit long can store, you can use java.math.BigInteger. But again, this is integer addition. – demongolem Apr 21 '14 at 18:26
  • You could use casting in Java to convert it I believe, but the easiest way is to, as @demongolem suggests, use the proper types in the first place. – Marshall Tigerus Apr 21 '14 at 18:29
  • 3.7 * 10^19 is well outside the range of an int. What output would you like? – Elliott Frisch Apr 21 '14 at 18:30
  • 1
    @demongolem if I use the long instead of double it gives me output as `-1` – mega6382 Apr 21 '14 at 18:31
  • @ElliottFrisch Have you ever heard of the trick `Put 1 rice grain on square 1 of a chess board. Double the grains on each successive square to square 64.` This is what I am trying to do. I want it be exact amount that I would get by the calculation on the paper. – mega6382 Apr 21 '14 at 18:35
  • Using `double` will not really get you the right answer. The correct answer is an integer that takes 64 bits (which means `long` isn't good enough because of the sign bit). A `double` only gives you 52 bits of precision, which means that if you use a `double` to do the computation, then no matter how you convert the result to an integer or `BigInteger`, the low-order 12 bits of the answer will not necessarily be accurate. – ajb Apr 21 '14 at 19:07
  • P.S. The right way to solve this is to go back to your high-school math class where they taught you how to sum a geometric progression :) :) :) – ajb Apr 21 '14 at 19:08
  • 2
    @ajb Well I am still in High School. My age is only (16 yrs). ;):) – mega6382 Apr 21 '14 at 19:10

3 Answers3

2

I would use the BigInteger type,

public static void main(String[] args) {
    BigInteger n = BigInteger.ONE;
    BigInteger p = BigInteger.ONE;
    for(int i = 1;i <= 64;i++){
        n = n.add(n);
        p = p.add(n);
    }
    System.out.println(p);
}

Output is

36893488147419103231

Edit Based on your comment, you really wanted something more like The Legend of the Chessboard -

BigInteger n = BigInteger.ONE;
BigInteger p = BigInteger.ZERO;
BigInteger TWO = new BigInteger("2");
for (int i = 1; i <= 64; i++) {
    StringBuilder sb = new StringBuilder();
    sb.append("For square #: " + i);
    sb.append(", Grains on square: " + n);
    p = p.add(n);
    n = n.multiply(TWO);
    sb.append(", Running Total: " + p);
    System.out.println(sb.toString());
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
1

The number is too large to fit in a long. To get the closest integral approximation, convert the double to a BigInteger by way of a BigDecimal:

BigDecimal bd = BigDecimal.valueOf(p);
BigInteger bi = bd.toBigInteger();

However, to get an exact result, perform all the calculations using BigIntegers:

import static java.math.BigInteger.ONE;

BigInteger n = ONE;
BigInteger p = ONE;
for (int i = 1; i <= 64; i++) {
    n = n.add(n);
    p = p.add(n);
}
System.out.println(p);

The difference between the approximate and exact values is:

36893488147419103000
36893488147419103231
David Conrad
  • 15,432
  • 2
  • 42
  • 54
0

Java: Math.round(float), PHP: round(value,precision) However you will precision

amit
  • 337
  • 1
  • 11