Jump to content

Javascript/php Calulation Discrepancy


ShadowMage
 Share

Recommended Posts

Hi guys, I'm hoping the experts here can help me track down a discrepancy in my calculations. I have a PHP version of some functions and a JavaScript version of some functions. The problem is, the functions return different values. I'm not sure if the PHP is wrong or the JavaScript or neither.Here is the PHP version of the functions:

public function calcFrameHrsCost() {	$Total = 0;		$hours = $this->calcFrameHours();	$tmpPerHour = $this->arrAssumptions['LbrHour']['Value'];		$tmpSubtotal = $hours*$tmpPerHour;		$Total = $tmpSubtotal;		return $Total;} //End function calcFrameHrsCostpublic function calcFrameHours() {	$Hours = 0;		$bound = count($this->arrParts);	for ($x=0; $x<$bound; $x++) { 		foreach ($this->arrParts[$x] as $PartType => $Part) {			$blnInit = 1;						if ((strtolower($PartType) != 'ibeam') && (strtolower($PartType) != 'brmetal')) {				foreach ($Part as $Pieces) {					$tmpHrsPerFt = 0;					if (isset($this->arrAssemblies[$this->$PartType]['HrsPerFt'])) {						$tmpHrsPerFt = $this->arrAssemblies[$this->$PartType]['HrsPerFt'];					}										if ($blnInit != 0) {						$lastPart = $Pieces["Part"];						$blnInit = 0;					}					$currPart = $Pieces["Part"];										if ($currPart == $lastPart) {						$tmpFootage = ($Pieces["Pieces"]*$Pieces["PcLength"])/12;						$Hours += ($tmpFootage*$tmpHrsPerFt);					} else {						//If part number changes break the loop, otherwise hours might be						//calculated multiple times for the same assembly						break;					}					$lastPart = $currPart;				}			}		}	} //End for ($x=0; $x<$bound; $x++)		return $Hours;} //End function calcFrameHours

And here's the JavaScript version:

function calcFrameHrsCost() {	var Total = 0;		var hours = calcFrameHours();	var tmpPerHour = Number(Assumptions['LbrHour']['Value']);		var tmpSubtotal = hours*tmpPerHour;		Total = tmpSubtotal;		return Total;} //End function calcFrameHrsCostfunction calcFrameHours() {	var Hours = 0;	var tmpHrsPerFt = 0;	var tmpFootage = 0;	var tmpPartInput = "";	var tmpAssembly = "";	var blnInit = true;	var lastPart = "";	var currPart = "";		var bound = arrParts.length;	var partCnt = 0;	for (x=0; x<bound; x++) {		for (partType in arrParts[x]) {			blnInit = true;						if ((partType.toLowerCase() != 'ibeam') && (partType.toLowerCase() != 'brmetal')) {				if (document.getElementById(partType)) {					tmpPartInput = document.getElementById(partType);					tmpAssembly = tmpPartInput.options[tmpPartInput.selectedIndex].value;				}								partCnt = arrParts[x][partType].length;				for (y=0; y<partCnt; y++) {					if (blnInit) {						lastPart = arrParts[x][partType][y].Part;						blnInit = false;					}					currPart = arrParts[x][partType][y].Part;										tmpHrsPerFt = 0;					if (Assemblies[tmpAssembly]) {						tmpHrsPerFt = Number(Assemblies[tmpAssembly]['HrsPerFt']);					}										if (currPart == lastPart) {						tmpFootage = (arrParts[x][partType][y].PcLength*arrParts[x][partType][y].Pieces)/12;						Hours += tmpFootage*tmpHrsPerFt;					} else {						//If part number changes break the loop, otherwise hours might be calculated						//multiple times for the same assembly						break;					}					lastPart = currPart;				}			} //End if ((partType.toLowerCase() != 'ibeam') || (partType.toLowerCase() != 'brmetal'))		} //End for (partType in arrParts[x])	} //End for ($x=0; $x<$bound; $x++)		return Hours;} //End function calcFrameHours

First off, are they exactly the same as far as what they do?The calcFrameHours functions seem to return the same value, but the cost functions return different values. These functions calculate hours and cost for skylights using width, height, quantity and other such variables. The PHP loads information from the database while the JavaScript recalculates when a user changes information. The problem is the JavaScript calculates a different cost than the PHP. The difference depends on the size and quantities of the skylights, sometimes its 10 cents different, sometimes as much as several dollars.So the second question is how can I track down what is causing this discrepancy?Thanks in advance. Your help is greatly appreciated.

Edited by jkloth
Link to comment
Share on other sites

Print the values being used in the calculations to make sure they're the same. I can't tell what's in arrParts, for example. So, print all values that you use in the calculations. If the values are the same, then the algorithm probably has a bug. If the values are different then that's your problem. Specifically check to make sure that floating point numbers are going to round to the same value. If they aren't, then you need to round them yourself. It's not clear to me how either function gets its data, since neither of them have any parameters, so print out all values and go from there.

Link to comment
Share on other sites

Got it solved! I was forgetting to round values to the nearest 1/16" in another function in PHP (the one that assembles arrParts) and rounding them in JavaScript. This, of course, was throwing things off just a little bit.I really don't know what I was hoping for but I think I've learned that printing out the values is the best (only?) way to debug stuff like this. Thanks jsg!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...