An arithmetic expression is an expression that results in a numeric value. There are two kinds of numeric values, integers (whole numbers), and real or floating point numbers (numbers containing a decimal point).
Questions tagged [arithmetic-expressions]
677 questions
5
votes
2 answers
Expression recursion level exceeded
Don't know why there is error in the following examples:
$ a=1; (( a > 0 )) && echo y || echo n
y
$ a=x; (( a > 0 )) && echo y || echo n
n
$ a=a; (( a > 0 )) && echo y || echo n
-bash: ((: a: expression recursion level exceeded (error…

user2847598
- 1,247
- 1
- 14
- 25
5
votes
1 answer
Where does the recursive variable expansion in bash/shell numeric contexts come from?
The POSIX spec states with regard to Arithmetic Expansion that
[i]f the shell variable x contains a value that forms a valid integer constant, optionally including a leading plus or minus sign, then the arithmetic expansions "$((x))" and "$(($x))"…

Etan Reisner
- 77,877
- 8
- 106
- 148
5
votes
2 answers
Regular expression for arithmetic expression
I,m trying to write a regex to check if the given string is like
a + b, 2 + a + b, 3 + 6 * 9 + 6 * 5 + a * b, etc...
Only + and * operators.
I tried
if (str.matches("(\\d|\\w \\+|\\*){1,} \\d|\\w"))
Unfortunately it only handles cases like…

MedAl
- 457
- 3
- 19
5
votes
2 answers
Arithmetic with array elements in bash
I'm using bash and trying to add all elements of an array that was created from a file.
while read line; do
array=($line);
sum=0
length=${#array[@]}
for i in ${array[@]:0:$length}; do
sum=$[$sum+${array[i]}] #<--- this…

Vaderico
- 629
- 2
- 8
- 24
5
votes
2 answers
Scientific Notation Conversion - Scheme
I am trying to do simple subtraction arithmetic on two very large numbers.
(- 1.8305286640724363e+023 (floor 1.8305286640724363e+023))
When I do this, I get a result of 0.0. I'm expecting an output of:
(- 1.8305286640724363e+023 (floor…

Sixers17
- 592
- 2
- 5
- 20
5
votes
4 answers
Extra parenthesis changing result of formula in SQL Server 2008
In all other languages (arithmetic engines in general) putting an extra set of parenthesis around operators of same priority does not impact results. But recently in a testing project I noticed that MS SQL server changes the results in those cases.…

Adarsha
- 2,267
- 22
- 29
4
votes
3 answers
Chain arithmetic operators with the R |> pipe
This is basically the same question as Chain arithmetic operators in dplyr with %>% pipe but updated for the new (as R 4.1) pipe operator |>.
How can I chain arithmetic operators with the R native pipe |>? With dplyr/magrittr, you can use backticks…

Tripartio
- 1,955
- 1
- 24
- 29
4
votes
4 answers
Arithmetic operation within string concatenation without parenthesis causes strange result
Consider the following line of code:
The output of that is 3, which is the expected result of the calculation $x-$y. However, the expected output is:
10 - 7 = 3
My question therefore is, what…

Jens Wegar
- 4,147
- 4
- 29
- 34
4
votes
3 answers
Can you implement arithmetic operator as variables in C?
I'm trying to program a reverse polish calculator and I didn't wanted to write switches for every single arithmetic operator inputted. Is it possible to input an arithmetic operator and use it as such inside the language itself?
For example, the…

minh anh b
- 51
- 4
4
votes
2 answers
Can a function be used in $(( func arg ? str1 : str2 ))?
I'd like to use a statement like this:
var=$(( func arg ? str1 : str2 ))
but bash gives this syntax error message:
syntax error in expression (error token is "arg")
I've played with various forms but I can't figure out how to make it accept a…

grok12
- 3,526
- 6
- 26
- 27
4
votes
3 answers
What would be the output of the following code snippet and why?
#include
#include
typedef unsigned int U32;
int main() {
U32 var = -1;
printf("var = %u\n", var);
if(var != -1)
{
printf("\n I'm not -1\n");
}
else
{
printf("I'm -1 and…

Sunny Mehta
- 43
- 4
4
votes
4 answers
Why does the 6502 microcontroller not have a arithmetic right shift?
I'm trying to understand the instruction sets of old microcontrollers, especially the 6502.
The documentation of the instruction set that can be found here lists two shift instructions (beside the rotate instructions):
ASL - arithmetic shift…

uzumaki
- 1,743
- 17
- 32
4
votes
1 answer
Balancing arithmetic expression tree with +, - operators
Given a binary arithmetic expression tree consisting of only addition and subtraction operators, and numbers, how to balance the tree as much as possible? The task is to balance the tree without evaluating the expression, that is the number of nodes…

Jevgeny
- 43
- 3
4
votes
3 answers
LOWORD and HIWORD operations
I've come into contact with some weird notation in a C++ program. I'm dealing with bit shifting and I came across the LOWORD() and HIWORD() functions. I understand that LOWORD is the value of the lower 2 bytes of an integer. I also know that HIWORD…

Sean
- 592
- 4
- 9
- 19
4
votes
3 answers
Which integer operations are unsafe?
My application evaluates some integer expressions specified by the user. I want to detect all potential errors and report them.
All computations are done in int64_t (signed). Formulas may include almost all C++ binary operators (+, -, *, /, %, |,…

Ivan Smirnov
- 4,365
- 19
- 30