Followers 0

# Javascript math with negative values

## 5 posts in this topic

I am wondering if anyone has a method to deal with negative numbers in math equations for javascript. At the moment I will sometimes get negative numbers resulting from some math done, for example I take the maximum number value in a set of information, multiply it half and add that value back:

`var maxLength = 100;var minLength = 1;var LengthBuffer = maxLength * .5;var endLengthRange = maxLength + LengthBuffer ;var startLengthRange = minLengthBuffer - LengthBuffer;// The above should yield 150 and -49, the issue is in the next part.// Due to needing to find a median between the end of this range and// the start (which here should be 199/2) the following math does not work correctlyvar lengthRange = endLengthRange - startLengthRange;//this will return 101 and not 199.[/CODE]`

##### Share on other sites

In what browser? I tried it in Safari and lengthRange gets set to 199 (like you're expecting).

##### Share on other sites

I've been testing this in Chrome but I get the same answer in FF as well. I'll post the code up that I'm working with.

`function randomFromTo(from, to){	   return Math.round(Math.random() * (to - from + 1) + from);	}randomFromTo(10,20);  //18randomFromTo(10,20);  //10randomFromTo(10,20);  //15randomFromTo(10,20);  //20var carPrices = new Array();var carMiles = new Array();var allCars = new Array();   for (i=0;i<400;i++) {  	allCars[i] = new Array();	allCars[i][0] = "car "+(i+1);	allCars[i][1] = randomFromTo(1000,500000); //miles	  allCars[i][2] = randomFromTo(5000,50000); //dollars  	//document.write(allCars[i][0]+' Miles: '+allCars[i][1]+' Price \$'+allCars[i][2]+'<br />')   }   totalMile = 0;   totalPrice = 0;   for(var i = 0; i < allCars.length - 1; ++i){	 //document.write(totalMile+" + "+allCars[i][1]+" =");   totalMile = totalMile + allCars[i][1];   totalPrice = totalPrice + allCars[i][2];   carMiles [i] = allCars[i][1];   carPrices [i] = allCars[i][2];	  //document.write(totalMile+'<br />');	}   //document.write(totalMile);   var totalCars = allCars.length;   var largestPrice = Math.max.apply(Math, carPrices);   var largestMiles = Math.max.apply(Math, carMiles);   var smallestPrice = Math.min.apply(Math, carPrices);   var smallestMiles = Math.min.apply(Math, carMiles);   var yourCar = new Array();   yourCar[0] = new Array();   yourCar[0][0] = 'car1';   yourCar[0][1] = 20000;   yourCar[0][2] = 25000;   var bufferMiles = largestMiles*.5;   var bufferPrice = largestPrice*.5;  // document.write('Your car is '+yourCar[0][0]+' with '+yourCar[0][1]+' miles and is priced at \$'+yourCar[0][2]+'<br />');   var yourCarMiles = yourCar[0][1];   var yourCarPrice = yourCar[0][2];   var startPriceRange = smallestPrice-bufferPrice;   var startMileRange = smallestMiles-bufferMiles;   var averageMile = totalMile/totalCars;   var averagePrice = totalPrice/totalCars;   var mileRangeEnd = largestMiles+bufferMiles;   var priceRangeEnd = largestPrice+bufferPrice;   var mileRange = mileRangeEnd - startMileRange; // Does not yield correct value   var priceRange = priceRangeEnd - startPriceRange; // Does not yield correct value   var mileMedian = mileRange*.5;   var priceMedian = priceRangeEnd*.5;  //document.write('Average Price: \$' + averagePrice +'<br />');  //document.write('Average Mileage: ' + averageMile +'<br />');  //document.write('number of cars: '+totalCars+'<br />');  //document.write('Highest Mileage: '+largestMiles+'<br />');  //document.write('Highest Price: '+largestPrice+'<br />');  //document.write('Lowest Price: '+smallestPrice+'<br />');  //document.write('Lowest Mileage: '+smallestMiles+'<br />');  document.write(mileRangeEnd+' - '+startMileRange+' = '+mileMedian+'<br />');  document.write('Middle Mile Range: '+priceMedian+'<br />');  document.write('Middle Price Range: '+priceMedian+'<br />');  document.write('Middle Mile Range: '+mileMedian+'<br />');[/CODE]`

##### Share on other sites

Well, I'm stumped. From both your example code and your actual code, I'm always getting the values you're expecting. For example, startPriceRange gets set to -19590. priceRangeEnd gets set to 74442. priceRange gets (properly) set to 94032.

Tested in Safari, Chrome, and Firefox.

Edit: Is this the part that's confusing you?:

`document.write(mileRangeEnd+' - '+startMileRange+' = '+mileMedian+'<br />');[/CODE]`

If I'm understanding this correctly, you should be outputting mileRange in that example, not mileMedian.

##### Share on other sites

I found the issue:

`var mileMedian = mileRange*.5;   var priceMedian = priceRangeEnd*.5;//priceRangeEnd should be priceRange[/CODE]`

## Create an account

Register a new account