Skip to content

Commit 24d6565

Browse files
committed
Provide example for SpEL's exponential power operator (^)
1 parent e34ad6b commit 24d6565

File tree

3 files changed

+46
-24
lines changed

3 files changed

+46
-24
lines changed

Diff for: framework-docs/modules/ROOT/pages/core/expressions/language-ref/operators.adoc

+19-11
Original file line numberDiff line numberDiff line change
@@ -290,30 +290,33 @@ Java::
290290
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
291291
----
292292
// Addition
293-
int two = parser.parseExpression("1 + 1").getValue(Integer.class); // 2
293+
int two = parser.parseExpression("1 + 1").getValue(int.class); // 2
294294
295295
// Subtraction
296-
int four = parser.parseExpression("1 - -3").getValue(Integer.class); // 4
296+
int four = parser.parseExpression("1 - -3").getValue(int.class); // 4
297297
298-
double d = parser.parseExpression("1000.00 - 1e4").getValue(Double.class); // -9000
298+
double d = parser.parseExpression("1000.00 - 1e4").getValue(double.class); // -9000
299299
300300
// Multiplication
301-
int six = parser.parseExpression("-2 * -3").getValue(Integer.class); // 6
301+
int six = parser.parseExpression("-2 * -3").getValue(int.class); // 6
302302
303-
double twentyFour = parser.parseExpression("2.0 * 3e0 * 4").getValue(Double.class); // 24.0
303+
double twentyFour = parser.parseExpression("2.0 * 3e0 * 4").getValue(double.class); // 24.0
304304
305305
// Division
306-
int minusTwo = parser.parseExpression("6 / -3").getValue(Integer.class); // -2
306+
int minusTwo = parser.parseExpression("6 / -3").getValue(int.class); // -2
307307
308-
double one = parser.parseExpression("8.0 / 4e0 / 2").getValue(Double.class); // 1.0
308+
double one = parser.parseExpression("8.0 / 4e0 / 2").getValue(double.class); // 1.0
309309
310310
// Modulus
311-
int three = parser.parseExpression("7 % 4").getValue(Integer.class); // 3
311+
int three = parser.parseExpression("7 % 4").getValue(int.class); // 3
312312
313-
int one = parser.parseExpression("8 / 5 % 2").getValue(Integer.class); // 1
313+
int one = parser.parseExpression("8 / 5 % 2").getValue(int.class); // 1
314+
315+
// Exponential power
316+
int maxInt = parser.parseExpression("(2^31) - 1").getValue(int.class); // Integer.MAX_VALUE
314317
315318
// Operator precedence
316-
int minusTwentyOne = parser.parseExpression("1+2-3*8").getValue(Integer.class); // -21
319+
int minusTwentyOne = parser.parseExpression("1+2-3*8").getValue(int.class); // -21
317320
----
318321
319322
Kotlin::
@@ -343,7 +346,12 @@ Kotlin::
343346
344347
val one = parser.parseExpression("8 / 5 % 2").getValue(Int::class.java) // 1
345348
346-
// Operator precedence
349+
// -- Exponential power --
350+
351+
var maxInt = parser.parseExpression("(2^31) - 1").getValue(Int::class.java) // Integer.MAX_VALUE
352+
353+
// -- Operator precedence --
354+
347355
val minusTwentyOne = parser.parseExpression("1+2-3*8").getValue(Int::class.java) // -21
348356
----
349357
======

Diff for: spring-expression/src/test/java/org/springframework/expression/spel/ParsingTests.java

+9
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,15 @@ void mathOperatorDecrementPostfix() {
392392
parseCheck("7--", "7--");
393393
parseCheck("foo--", "foo--");
394394
}
395+
396+
@Test
397+
void mathOperatorPower() {
398+
parseCheck("3^2", "(3 ^ 2)");
399+
parseCheck("3.0d^2.0d", "(3.0 ^ 2.0)");
400+
parseCheck("3L^2L", "(3 ^ 2)");
401+
parseCheck("(2^32)^2", "((2 ^ 32) ^ 2)");
402+
parseCheck("new java.math.BigDecimal('5') ^ 3", "(new java.math.BigDecimal('5') ^ 3)");
403+
}
395404
}
396405

397406
@Nested

Diff for: spring-expression/src/test/java/org/springframework/expression/spel/SpelDocumentationTests.java

+18-13
Original file line numberDiff line numberDiff line change
@@ -73,17 +73,17 @@ class SpelDocumentationTests extends AbstractExpressionTests {
7373

7474
@Test
7575
void methodInvocation() {
76-
evaluate("'Hello World'.concat('!')","Hello World!",String.class);
76+
evaluate("'Hello World'.concat('!')", "Hello World!", String.class);
7777
}
7878

7979
@Test
8080
void beanPropertyAccess() {
81-
evaluate("new String('Hello World'.bytes)","Hello World",String.class);
81+
evaluate("new String('Hello World'.bytes)", "Hello World", String.class);
8282
}
8383

8484
@Test
8585
void arrayLengthAccess() {
86-
evaluate("'Hello World'.bytes.length",11,Integer.class);
86+
evaluate("'Hello World'.bytes.length", 11, Integer.class);
8787
}
8888

8989
@Test
@@ -307,39 +307,44 @@ void stringOperators() {
307307
@Test
308308
void mathematicalOperators() {
309309
// Addition
310-
int two = parser.parseExpression("1 + 1").getValue(Integer.class); // 2
310+
int two = parser.parseExpression("1 + 1").getValue(int.class); // 2
311311
assertThat(two).isEqualTo(2);
312312

313313
// Subtraction
314-
int four = parser.parseExpression("1 - -3").getValue(Integer.class); // 4
314+
int four = parser.parseExpression("1 - -3").getValue(int.class); // 4
315315
assertThat(four).isEqualTo(4);
316316

317-
double d = parser.parseExpression("1000.00 - 1e4").getValue(Double.class); // -9000
317+
double d = parser.parseExpression("1000.00 - 1e4").getValue(double.class); // -9000
318318
assertThat(d).isCloseTo(-9000.0d, within((double) 0));
319319

320320
// Multiplication
321-
int six = parser.parseExpression("-2 * -3").getValue(Integer.class); // 6
321+
int six = parser.parseExpression("-2 * -3").getValue(int.class); // 6
322322
assertThat(six).isEqualTo(6);
323323

324-
double twentyFour = parser.parseExpression("2.0 * 3e0 * 4").getValue(Double.class); // 24.0
324+
double twentyFour = parser.parseExpression("2.0 * 3e0 * 4").getValue(double.class); // 24.0
325325
assertThat(twentyFour).isCloseTo(24.0d, within((double) 0));
326326

327327
// Division
328-
int minusTwo = parser.parseExpression("6 / -3").getValue(Integer.class); // -2
328+
int minusTwo = parser.parseExpression("6 / -3").getValue(int.class); // -2
329329
assertThat(minusTwo).isEqualTo(-2);
330330

331-
double one = parser.parseExpression("8.0 / 4e0 / 2").getValue(Double.class); // 1.0
331+
double one = parser.parseExpression("8.0 / 4e0 / 2").getValue(double.class); // 1.0
332332
assertThat(one).isCloseTo(1.0d, within((double) 0));
333333

334334
// Modulus
335-
int three = parser.parseExpression("7 % 4").getValue(Integer.class); // 3
335+
int three = parser.parseExpression("7 % 4").getValue(int.class); // 3
336336
assertThat(three).isEqualTo(3);
337337

338-
int oneInt = parser.parseExpression("8 / 5 % 2").getValue(Integer.class); // 1
338+
int oneInt = parser.parseExpression("8 / 5 % 2").getValue(int.class); // 1
339339
assertThat(oneInt).isEqualTo(1);
340340

341+
// Exponential power
342+
int maxInt = parser.parseExpression("(2^31) - 1").getValue(int.class); // Integer.MAX_VALUE
343+
assertThat(maxInt).isEqualTo(Integer.MAX_VALUE);
344+
341345
// Operator precedence
342-
int minusTwentyOne = parser.parseExpression("1+2-3*8").getValue(Integer.class); // -21
346+
347+
int minusTwentyOne = parser.parseExpression("1+2-3*8").getValue(int.class); // -21
343348
assertThat(minusTwentyOne).isEqualTo(-21);
344349
}
345350

0 commit comments

Comments
 (0)