Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add abs, round, floor, ceil operators #6496

Merged
merged 5 commits into from
Apr 11, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/style-spec/expression/definitions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,32 @@ CompoundExpression.register(expressions, {
varargs(NumberType),
(ctx, args) => Math.max(...args.map(arg => arg.evaluate(ctx)))
],
'abs': [
NumberType,
[NumberType],
(ctx, args) => Math.abs(args[0].evaluate(ctx))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer argument destructuring as in the other definitions, i.e. (ctx, [n]) => ....

],
'round': [
NumberType,
[NumberType],
(ctx, args) => {
const v = args[0].evaluate(ctx);
// Javascript's Math.round() rounds towards +Infinity for halfway
// values, even when they're negative. It's more common to round
// away from 0 (e.g., this is what python and C++ do)
return v < 0 ? -Math.round(-v) : Math.round(v);
}
],
'floor': [
NumberType,
[NumberType],
(ctx, args) => Math.floor(args[0].evaluate(ctx))
],
'ceil': [
NumberType,
[NumberType],
(ctx, args) => Math.ceil(args[0].evaluate(ctx))
],
'filter-==': [
BooleanType,
[StringType, ValueType],
Expand Down
36 changes: 36 additions & 0 deletions src/style-spec/reference/v8.json
Original file line number Diff line number Diff line change
Expand Up @@ -2490,6 +2490,42 @@
}
}
},
"round": {
"doc": "Rounds the input to the nearest integer. Halflway values are rounded away from zero. E.g., `[\"round\", -1.5]` evaluates to -2.",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Halflway → Halfway

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: It’s somewhat unusual to begin a sentence with “E.g.” “For example” is more easily understood.

"group": "Math",
"sdk-support": {
"basic functionality": {
"js": "0.45.0"
}
}
},
"abs": {
"doc": "Returns the absolute value of the input.",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when implementing I noticed abs on -1.1 returns 1, could we include in the documentation that abs is an integer type function/not floating point? (from a java background this was a bit confusing for me).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

capturing from chat that above assumption was invalid

"group": "Math",
"sdk-support": {
"basic functionality": {
"js": "0.45.0"
}
}
},
"ceil": {
"doc": "Returns the smallest integer that is greater than or equal to the input.",
"group": "Math",
"sdk-support": {
"basic functionality": {
"js": "0.45.0"
}
}
},
"floor": {
"doc": "Returns the largest integer that is less than or equal to the input.",
"group": "Math",
"sdk-support": {
"basic functionality": {
"js": "0.45.0"
}
}
},
"==": {
"doc": "Returns `true` if the input values are equal, `false` otherwise. Equality is strictly typed: values of different types are always considered not equal.",
"group": "Decision",
Expand Down
17 changes: 17 additions & 0 deletions test/integration/expression-tests/abs/basic/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"expression": ["abs", ["get", "x"]],
"inputs": [
[{}, {"properties": {"x": -2}}],
[{}, {"properties": {"x": 2}}]
],
"expected": {
"compiled": {
"result": "success",
"isFeatureConstant": false,
"isZoomConstant": true,
"type": "number"
},
"outputs": [2, 2],
"serialized": ["abs", ["number", ["get", "x"]]]
}
}
23 changes: 23 additions & 0 deletions test/integration/expression-tests/ceil/basic/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"expression": ["ceil", ["get", "x"]],
"inputs": [
[{}, {"properties": {"x": -2.6}}],
[{}, {"properties": {"x": -2.5}}],
[{}, {"properties": {"x": -2.4}}],
[{}, {"properties": {"x": -2}}],
[{}, {"properties": {"x": 2.6}}],
[{}, {"properties": {"x": 2.5}}],
[{}, {"properties": {"x": 2.4}}],
[{}, {"properties": {"x": 2}}]
],
"expected": {
"compiled": {
"result": "success",
"isFeatureConstant": false,
"isZoomConstant": true,
"type": "number"
},
"outputs": [ -2, -2, -2, -2, 3, 3, 3, 2 ],
"serialized": ["ceil", ["number", ["get", "x"]]]
}
}
23 changes: 23 additions & 0 deletions test/integration/expression-tests/floor/basic/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"expression": ["floor", ["get", "x"]],
"inputs": [
[{}, {"properties": {"x": -2.6}}],
[{}, {"properties": {"x": -2.5}}],
[{}, {"properties": {"x": -2.4}}],
[{}, {"properties": {"x": -2}}],
[{}, {"properties": {"x": 2.6}}],
[{}, {"properties": {"x": 2.5}}],
[{}, {"properties": {"x": 2.4}}],
[{}, {"properties": {"x": 2}}]
],
"expected": {
"compiled": {
"result": "success",
"isFeatureConstant": false,
"isZoomConstant": true,
"type": "number"
},
"outputs": [ -3, -3, -3, -2, 2, 2, 2, 2 ],
"serialized": ["floor", ["number", ["get", "x"]]]
}
}
23 changes: 23 additions & 0 deletions test/integration/expression-tests/round/basic/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"expression": ["round", ["get", "x"]],
"inputs": [
[{}, {"properties": {"x": -2.6}}],
[{}, {"properties": {"x": -2.5}}],
[{}, {"properties": {"x": -2.4}}],
[{}, {"properties": {"x": -2}}],
[{}, {"properties": {"x": 2.6}}],
[{}, {"properties": {"x": 2.5}}],
[{}, {"properties": {"x": 2.4}}],
[{}, {"properties": {"x": 2}}]
],
"expected": {
"compiled": {
"result": "success",
"isFeatureConstant": false,
"isZoomConstant": true,
"type": "number"
},
"outputs": [ -3, -3, -2, -2, 3, 3, 2, 2 ],
"serialized": ["round", ["number", ["get", "x"]]]
}
}
9 changes: 7 additions & 2 deletions test/integration/lib/expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,16 @@ exports.run = function (implementation, options, runExpressionTest) {
.filter(Boolean)
.join('\n');
};

if (compileOk && !evalOk) {
diffOutputs(result.outputs);
const differences = diffOutputs(result.outputs);
diffOutput.text += differences;
diffOutput.html += differences;
}
if (recompileOk && !roundTripOk) {
diffOutputs(result.roundTripOutputs);
const differences = diffOutputs(result.roundTripOutputs);
diffOutput.text += differences;
diffOutput.html += differences;
}

params.difference = diffOutput.html;
Expand Down