Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 6th, 2012, 08:31 AM
guldurkhand guldurkhand is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 2 guldurkhand User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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 = (/ (($e_tax 15) / 20)) - (9);
  if (
$peasants 0)
    
$peasmult / ((/ (($e_tax 15) / 20)) - (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?

Reply With Quote
  #2  
Old October 6th, 2012, 11:02 AM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,958 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 3 h 7 m 10 sec
Reputation Power: 345
Can you post the output from both programs to show what is different?

Reply With Quote
  #3  
Old October 7th, 2012, 06:17 AM
guldurkhand guldurkhand is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 2 guldurkhand User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 m 9 sec
Reputation Power: 0
Ah I got the problem, it is a floating point so I have to put instead of tax/100, tax/100.0

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > PHP to Java

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap