Questions tagged [arithmetic-expressions]

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).

677 questions
10
votes
9 answers

General-purpose language to specify value constraints

I am looking for a general-purpose way of defining textual expressions which allow a value to be validated. For example, I have a value which should only be set to 1, 2, 3, 10, 11, or 12. Its constraint might be defined as: (value >= 1 && value <=…
g t
  • 7,287
  • 7
  • 50
  • 85
10
votes
2 answers

Arithmetic C++ Operators

I was just asked a question in a technical interview that I was a bit confused about. The question was as follows: If int i = -1, int j = -1, and int k = -1, and we run the following line: ++i && ++j && ++k what would be the new values of i, j,…
John Roberts
  • 5,885
  • 21
  • 70
  • 124
9
votes
2 answers

Is there such a thing as short circuit multiplication?

We all know about short circuiting in logical expressions, i.e. when if ( False AND myFunc(a) ) then ... doesn't bother executing myFunc() because there's no way the if condition can be true. I was curious as to whether there is an equivalent for…
9
votes
1 answer

Python Decimal - multiplication by zero

Why does the following code: from decimal import Decimal result = Decimal('0') * Decimal('0.8881783462119193534061639577') print(result) return 0E-28 ? I've traced it to the following code in the module: if not self or not other: ans =…
tonysepia
  • 3,340
  • 3
  • 27
  • 45
9
votes
1 answer

int divided by unsigned int causing rollover

I try to divide int by unsigned int and I get unexpected result: int b; unsigned int c; int res; float res_f; b = -25; c = 5; res = b / c; // res = 858993454 res_f = b / c; // res_f = -5.000000 The same works just fine for '+', '-' and '*', but…
shushu
  • 91
  • 1
  • 2
9
votes
4 answers

Java precedence for multiple + and - operators

This is more of a theoretical question to understand Java's evaluation of arithmetic operations. Since + and - have the same precedence, I don't quite understand how Java evaluates the following expressions (where there are more than one + and -…
ncw
  • 1,635
  • 1
  • 18
  • 26
9
votes
3 answers

bc arithmetic Error

i am trying to solve this bash script which reads an arithmetic expression from user and echoes it to the output screen with round up of 3 decimal places in the end. sample input 5+50*3/20 + (19*2)/7 sample output 17.929 my code is read x echo…
krrish
  • 353
  • 3
  • 15
9
votes
2 answers

How to be warned about potential arithmetic errors due to type conversion?

I am working on a calculation module using C#, and I bumped on this : double v = 4 / 100; I know this is a wrong initialization that returns v = 0.0 instead of v = 0.04 The c# rules says I must ensure at least one of the member is a double, like…
Larry
  • 17,605
  • 9
  • 77
  • 106
9
votes
1 answer

Storing basic arithmetic operators in variables

How can I store a basic arithmetic operator in a variable? I'd like to do something like this in c++: int a = 1; int b = 2; operator op = +; int c = a op b; if (c == 3) // do something Since I'm considering only +, -, * and / I could store the…
MrDatabase
  • 43,245
  • 41
  • 111
  • 153
8
votes
3 answers

convert scientific notation to decimal in bash

I would like to convert a number that is stored in scientific notation into a floating point decimal, so I can then perform some comparisons on the data. This is being done in a bash script - here is a small snippet of the code: while read track_id…
kimmyjo221
  • 685
  • 4
  • 10
  • 17
7
votes
2 answers

How does the formula x & (x - 1) works?

From Hacker's Delight: 2nd Edition: The formula here seems a little bit awkward here. How is some x vector is subtracted from 1 vector (presumbly 0x1111 1111) when x is smaller than 1? (Like: (as given in example) 0x0101 1000 - 0x0000 0000 doesn't…
7
votes
4 answers

What precision are floating-point arithmetic operations done in?

Consider two very simple multiplications below: double result1; long double result2; float var1=3.1; float var2=6.789; double var3=87.45; double var4=234.987; result1=var1*var2; result2=var3*var4; Are multiplications by default done in a higher…
7
votes
2 answers

C style arithmetic with floating point values in Bash

How can I have the right result from this bash script? #!/bin/bash echo $(( 1/2 )) I get 0 as result! So I tried to use these but without success: $ echo $(( 1/2.0 )) bash: 1/2.0 : syntax error: invalid arithmetic operator (error token is ".0 ") $…
Alberto
  • 2,881
  • 7
  • 35
  • 66
6
votes
1 answer

Minimize parenthesis when printing expression

I have a simple arithmetic expression data structure that I want to be able to print. For sake of simplicity here I have made an example with 3 binary operations, addition, multiplication and division. The definition looks like this: module…
6
votes
2 answers

Why is my code not calculating the correct value for the expression string?

In the code I use an expression tree "3 + 2.53 - 1.75" that should return a result of 3.78. However, it ends up adding all the values within the string and outputs 7.28. I ran through the code multiple times on paper trying to see what happens in…
1 2
3
45 46