Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <meta name="description" content="math.js | basic usage">
  <title>math.js | basic usage</title>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/mathjs/3.0.0/math.min.js"></script>
</head>
<body>
  <script>
    // basic usage of math.js
    //
    // website:  http://mathjs.org
    // docs:     http://mathjs.org/docs
    // examples: http://mathjs.org/examples
    
    var smoothing = .9;
    
    function lowPass(degree, oldVal) {
        // var oldVal = $scope.compassBearing || 0;
        var complexOldVal = math.complex({r:1, phi:oldVal * (math.pi/180)});
        var complexNewVal = math.complex({r:1, phi:degree * (math.pi/180)});    
        var complexSmoothedValue = math.chain(complexNewVal)
                                       .subtract(complexOldVal)
                                       .multiply(smoothing)
                                       .add(complexOldVal)
                                       .done();
        var smoothedValue = math.arg(complexSmoothedValue) * (180/math.pi);
        if (smoothedValue < 0) { return 360 + smoothedValue; }
        return smoothedValue;
    }
    document.write(lowPass(20, 30)+'<br>');
    // 20.99634415156203
    document.write(lowPass(10, 350)+'<br>');
    // 8.029256754215519
    
  </script>
</body>
</html>
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
anonymouspro
0viewers