Sign in to follow this  

Javascript math with negative values

Recommended Posts

Emn1ty    3,253

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.

Share this post

Link to post
Share on other sites
Elliott    235

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

Share this post

Link to post
Share on other sites
Emn1ty    3,253

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

Share this post

Link to post
Share on other sites
Elliott    235

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 this post

Link to post
Share on other sites
Emn1ty    3,253

I found the issue:

var mileMedian = mileRange*.5;
var priceMedian = priceRangeEnd*.5;
//priceRangeEnd should be priceRange

Share this post

Link to post
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

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.