
October 6th, 2012, 08:31 AM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 2
Time spent in forums: 15 m 9 sec
Reputation Power: 0
|
|
|
PHP to Java
Hello, I am trying to convert the following php code to java:
PHP Code:
<?php
$e_tax = 70;
$e_peasants = 0;
$e_freeland = 0;
$e_land = 10000;
$e_bldpop = 10000;
for($i = 1; $i <= 10000; $i++){
$taxrate = $e_tax / 100;
if ($taxrate > 0.40)
$taxpenalty = ($taxrate - 0.40) / 2;
elseif ($taxrate < 0.20)
$taxpenalty = ($taxrate - 0.20) / 2;
else $taxpenalty = 0;
$popbase = round((($e_land * 2) + ($e_freeland * 5) + ($e_bldpop * 60)) / (0.95 + $taxrate + $taxpenalty));
$peasants = 0;
$peasmult = 1;
if ($e_peasants != $popbase)
$peasants = ($popbase - $e_peasants) / 20;
if ($peasants > 0)
$peasmult = (4 / (($e_tax + 15) / 20)) - (7 / 9);
if ($peasants < 0)
$peasmult = 1 / ((4 / (($e_tax + 15) / 20)) - (7 / 9));
$peasants = round($peasants * $peasmult * $peasmult);
$e_peasants += $peasants;
print "$e_peasants<br>";
}
?>
I converted it to the following:
Code:
package game;
public class Main4 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
long e_tax = 70;
long e_peasants = 0;
long e_freeland = 0;
long e_land = 10000;
long e_bldpop = 10000;
for (int i = 1;i<=10000;i++) {
double taxrate = (e_tax / 100);
double taxpenalty;
if (taxrate > 0.40){
taxpenalty = (taxrate - 0.40) / 2;
}
else if (taxrate < 0.20){
taxpenalty = (taxrate - 0.20) / 2;
}
else {taxpenalty = 0;}
long popbase = (int) Math.round(((e_land * 2)
+ (e_freeland * 5) + (e_bldpop * 60))
/ (0.95 + taxrate + taxpenalty));
long peasants = 1;
double peasmult = 1;
if (e_peasants != popbase) {
peasants = (popbase - e_peasants) / 20;
}
if (peasants > 0) {
double a = ((e_tax + 15) / 20);
peasmult = (4 / a) - (7 / 9);
}
if (peasants < 0) {
double a = ((e_tax + 15) / 20);
peasmult = 1 / ((4 / a) - (7 / 9));
}
peasants = Math.round(peasants * peasmult * peasmult);
e_peasants = e_peasants + peasants;
System.out.println(e_peasants);
}
}
}
But the output is different, can someone help me out with this, so that the java will be the same output as the php(without modifying the php)?
Why is it that I got different outputs of both?
|