Jump to content



Photo

Javascript math with negative values

javascript math negative numbers

  • Please log in to reply
4 replies to this topic

#1 Emn1ty

Emn1ty

    Web Programmer

  • Joined: 09-April 06
  • Location: Irvine, CA
  • OS: Windows 8.1, OSX Mavericks
  • Phone: Driod Razr

Posted 03 November 2011 - 16:20

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 correctly

var lengthRange = endLengthRange - startLengthRange;


//this will return 101 and not 199.



#2 Elliott

Elliott

    Neowinian Senior

  • Joined: 26-September 02
  • Location: Virginia, USA
  • OS: OS X Mountain Lion
  • Phone: iPhone 5 (iOS 6)

Posted 03 November 2011 - 16:25

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

#3 OP Emn1ty

Emn1ty

    Web Programmer

  • Joined: 09-April 06
  • Location: Irvine, CA
  • OS: Windows 8.1, OSX Mavericks
  • Phone: Driod Razr

Posted 03 November 2011 - 16:34

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);  //18

randomFromTo(10,20);  //10

randomFromTo(10,20);  //15

randomFromTo(10,20);  //20

var 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 />');



#4 Elliott

Elliott

    Neowinian Senior

  • Joined: 26-September 02
  • Location: Virginia, USA
  • OS: OS X Mountain Lion
  • Phone: iPhone 5 (iOS 6)

Posted 03 November 2011 - 16:51

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 />');

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

#5 OP Emn1ty

Emn1ty

    Web Programmer

  • Joined: 09-April 06
  • Location: Irvine, CA
  • OS: Windows 8.1, OSX Mavericks
  • Phone: Driod Razr

Posted 03 November 2011 - 17:01

I found the issue:

var mileMedian = mileRange*.5;

   var priceMedian = priceRangeEnd*.5;

//priceRangeEnd should be priceRange





Click here to login or here to register to remove this ad, it's free!