最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I compare a number plus or minus another number - Stack Overflow

programmeradmin2浏览0评论

Lets say I have two vars called value1 and value2. These values would be input by the user but lets just pretend they did already and these are the values

    var value1 = 5;
    var value2 = 7; 
    var parison = .785  

I just want to pare the values plus or minus the parison so when the code displays output it only displays the values of 5 + or - .785 and 7 + or minus .785

For some context the following code is a pulling information from a json object that has two separate lists of numbers lets say number1 and number2 and a ton of other info but dont worry about any of that, that all works fine

       $("#createlist").click(function() {
            var value1 = Number($("#value1").val());
            var value2 = Number($("#value2").val());    
            var parison = .785

            $.getJSON("get_divvy_data.php", null, function(data) {
                var total_bikes_available = 0;
                $("#stationtable .stationrow").remove();
                $.each(data.stationBeanList, function(index, station) {

This next part is where I am having a problem with.

                  if( station.number1 is <= value1 + - parison && station.number2 is <= value2 + - parison) {
                       //do something
                   }

I just dont know how to write the if statement and parison effectively.

Lets say I have two vars called value1 and value2. These values would be input by the user but lets just pretend they did already and these are the values

    var value1 = 5;
    var value2 = 7; 
    var parison = .785  

I just want to pare the values plus or minus the parison so when the code displays output it only displays the values of 5 + or - .785 and 7 + or minus .785

For some context the following code is a pulling information from a json object that has two separate lists of numbers lets say number1 and number2 and a ton of other info but dont worry about any of that, that all works fine

       $("#createlist").click(function() {
            var value1 = Number($("#value1").val());
            var value2 = Number($("#value2").val());    
            var parison = .785

            $.getJSON("get_divvy_data.php", null, function(data) {
                var total_bikes_available = 0;
                $("#stationtable .stationrow").remove();
                $.each(data.stationBeanList, function(index, station) {

This next part is where I am having a problem with.

                  if( station.number1 is <= value1 + - parison && station.number2 is <= value2 + - parison) {
                       //do something
                   }

I just dont know how to write the if statement and parison effectively.

Share Improve this question edited Apr 9, 2014 at 22:59 nope asked Apr 9, 2014 at 21:46 nopenope 1771 gold badge4 silver badges16 bronze badges 1
  • just renamed some variables in last edit – nope Commented Apr 9, 2014 at 21:48
Add a ment  | 

1 Answer 1

Reset to default 9

One possible approach:

if (Math.abs(value1 - station.number1) <= parison
    && Math.abs(value2 - station.number2) <= parison) {
   //...    
}

... but be aware of possible edge cases caused by float-math imperfection. For example:

var value     = 0.9;
var reference = 0.7;
var delta     = 0.2;
console.log(value - reference <= delta); // false
发布评论

评论列表(0)

  1. 暂无评论