最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin7PV0评论

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
Post a comment

comment list (0)

  1. No comments so far