diff --git a/api/controllers/arithmeticController.js b/api/controllers/arithmeticController.js index b67886a8c4..0fccf513d7 100644 --- a/api/controllers/arithmeticController.js +++ b/api/controllers/arithmeticController.js @@ -32,14 +32,14 @@ exports.calculate = function(req, res) { // Validate operands if (! req.query.operand1 || - ! req.query.operand1.match(/^(-)?[0-9\.]+$/) || - req.query.operand1.replace(/[-0-9]/g, '').length > 1) { + ! req.query.operand1.match(/^(-)?[0-9\.]+(e(-)?[0-9]+)?$/) || + req.query.operand1.replace(/[-0-9e]/g, '').length > 1) { throw new Error("Invalid operand1: " + req.query.operand1); } if (! req.query.operand2 || - ! req.query.operand2.match(/^(-)?[0-9\.]+$/) || - req.query.operand2.replace(/[-0-9]/g, '').length > 1) { + ! req.query.operand2.match(/^(-)?[0-9\.]+(e(-)?[0-9]+)?$/) || + req.query.operand2.replace(/[-0-9e]/g, '').length > 1) { throw new Error("Invalid operand2: " + req.query.operand2); } diff --git a/test/arithmetic.js b/test/arithmetic.js index eee242fcf1..05f812055f 100644 --- a/test/arithmetic.js +++ b/test/arithmetic.js @@ -91,6 +91,14 @@ describe('Arithmetic', function() { done(); }); }); + it('adds with negative exponent', function(done) { + request.get('/arithmetic?operation=add&operand1=1.2e-5&operand2=-1.2e-5') + .expect(200) + .end(function(err, res) { + expect(res.body).to.eql({ result: 0 }); + done(); + }); + }); }); describe('Subtraction', function() { @@ -177,6 +185,14 @@ describe('Arithmetic', function() { done(); }); }); + it('multiplies supporting exponential notation', function(done) { + request.get('/arithmetic?operation=multiply&operand1=4.2e1&operand2=1e0') + .expect(200) + .end(function(err, res) { + expect(res.body).to.eql({ result: 42 }); + done(); + }); + }); }); describe('Division', function() {