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
4
votes
1 answer
What is difference between rem and mod function in sml?
I'm working on sml project in which I have to implement these two function rem & mod for custom datatype.
I know the definition of remainder rem.
dividend = divisor * quotient + remainder
What is the definition of mod?
Please explain me the…

LearningGiraffe
- 145
- 3
- 11
4
votes
3 answers
Inhibit implicit conversion while using arithmetic operators
How can we tell the C++ compiler that it should avoid implicit casting while using arithmetic operators such as +and /, i.e.,
size_t st_1, st_2;
int i_1, i_2;
auto st = st_1 + st_2; // should compile
auto i = i_1 + i_2; // should…

abraham_hilbert
- 2,221
- 1
- 13
- 30
4
votes
2 answers
Arithmetic expansion in array indices - is the dollar sign needed?
When I use arithmetic expansion in an array index in bash, like this:
declare -a FILES
declare -i INDEX=0
for FILE in ./*
do
FILES[((INDEX++))]="$FILE"
done
Do I need a dollar sign in front of ((...))?
So does it have to…

erbth
- 43
- 5
4
votes
4 answers
Why does bash brace expansion work in some arithmetic expressions but not others?
I am working on a very simple bash script and I'm having a problem with understating why deprecated $[] is working flawlessly, while $(()) seems to break the whole thing.
The code I'm referring to is:
for i in {1..10};
do
printf %4d…

Jakub
- 187
- 1
- 1
- 9
4
votes
3 answers
Arithmetic comparison in Prolog
Let's say we have two variables X and Y. X is 53 and Y is 52. What I want to do is compare them by adding 1 to the Y, so that it would be 53 - therefore X would be equal to Y + 1.
I am trying to do it by simply using equal operator and addition for…

WheelPot
- 307
- 1
- 3
- 15
4
votes
5 answers
Dividing rows by their sum in R
I have the following example set of data:
Example<-data.frame(A=10*1:9,B=10*10:18)
rownames(Example)<-paste("Sample",1:9)
> Example
A B
Sample 1 10 100
Sample 2 20 110
Sample 3 30 120
Sample 4 40 130
Sample 5 50 140
Sample 6 60…

Adam
- 313
- 1
- 3
- 12
4
votes
3 answers
How does `int sum = n + - + - + - + n` compile where `n` is an `int`?
This afternoon, I really don't know what I was doing with Operators and C. Eventually, I wrote some code which I was thinking wouldn't compile, But I don't know how it worked.
The code is:
#include
int main()
{
int n=2;
int…

Pankaj Prakash
- 2,300
- 30
- 31
4
votes
1 answer
Is Swift the only (mainstream) language with overflow checking arithmetic?
I started studying Swift language today. I've leaned up to basic and advanced operators.
To me, the fact that all the default arithmetic operations in Swift are checked against overflow/underflow is a little surprising.
Is there any other mainstream…

9dan
- 4,222
- 2
- 29
- 44
4
votes
1 answer
Parsec-Haskell, formatting parse errors
I am developing a simple calculator that takes one line of input, parses it using parsec and then processes it.
I want to make the parsec parse-error messages smaller. They include position info which is not necessary for a one line input. I've…
user2388535
4
votes
5 answers
BASH: Percentage change - how to calculate it? How to get absolute value without bc?
I need to count a percentage change between two values.
The code I have here:
echo $time1
echo $time2
pc=$(( (($time2 - $time1)/$time1 * 100) ))
echo $pc
Brings such output in the console (with set -xe option)
+ echo 1800
1800
+ echo 1000
1000
+…

hc0re
- 1,806
- 2
- 26
- 61
4
votes
2 answers
Prolog dynamic arithmetic expression
I'm new to Prolog and would like to define a simple predicate which calculates the result depending on which function I choose to use in the arithmetic expression.
So, this was my idea:
operation(X,Y, Op, Result):-
Result is X Op Y.
Now, I was…

XerXes
- 337
- 1
- 16
4
votes
1 answer
Prolog all possible expressions of given x
I have a prolog program with given grammar:
sum --> [+], mult, sum | mult | num.
mult --> [*], num, xer.
xer --> [x] | [^], [x], num.
num --> [2] | [3] ... etc
I have an abstract tree representation of my expressions. For example:…

knordbo
- 552
- 3
- 7
4
votes
5 answers
Writing infinite list to skip every factor of p?
How can I efficiently represent the list [0..] \\ [t+0*p, t+1*p ..]?
I have defined:
Prelude> let factors p t = [t+0*p, t+1*p ..]
I want to efficiently represent an infinite list that is the difference of [0..] and factors p t, but using \\ from…

Rob
- 5,223
- 5
- 41
- 62
4
votes
4 answers
C++ test for divisibility with double
why doesn´t ((num / i) % 1 == 0) work in C++ when num is a double? and how would I instead write this code, that checks for factorials by checking if it leaves a remainder (etc 0.3333).
int getFactorials (double num)
{
int total = 0; //…

Tom Lilletveit
- 1,872
- 3
- 31
- 57
4
votes
1 answer
Arithmetic operations and the compiler optimizations
I am contemplating a fixed-point arithmetic library, and in order to decide on how much optimization should be done by the library itself (through expression templates) I started questioning how much will already be done by the optimizer. Take the…

enobayram
- 4,650
- 23
- 36