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
337
votes
24 answers

How do I use floating-point arithmetic in bash?

I am trying to divide two image widths in a Bash script, but bash gives me 0 as the result: RESULT=$(($IMG_WIDTH/$IMG2_WIDTH)) I did study the Bash guide and I know I should use bc, in all examples in internet they use bc. In echo I tried to put…
Medya Gh
  • 4,563
  • 5
  • 23
  • 35
248
votes
9 answers

Is there an exponent operator in C#?

For example, does an operator exist to handle this? float Result, Number1, Number2; Number1 = 2; Number2 = 2; Result = Number1 (operator) Number2; In the past the ^ operator has served as an exponential operator in other languages, but in C# it…
Charlie
  • 2,491
  • 2
  • 14
  • 4
225
votes
6 answers

How to use mod operator in bash?

I'm trying a line like this: for i in {1..600}; do wget http://example.com/search/link $i % 5; done; What I'm trying to get as output is: wget http://example.com/search/link0 wget http://example.com/search/link1 wget…
Eric
  • 2,900
  • 2
  • 19
  • 20
106
votes
14 answers

Is it possible to simplify (x == 0 || x == 1) into a single operation?

So I was trying to write the nth number in the Fibonacci sequence in as compact a function as possible: public uint fibn ( uint N ) { return (N == 0 || N == 1) ? 1 : fibn(N-1) + fibn(N-2); } But I'm wondering if I can make this even more…
user6048670
  • 2,861
  • 4
  • 16
  • 20
74
votes
8 answers

What does the compiler do here: int a = b * (c * d * + e)?

I had a strange bug in my program, and after a few hours of debugging, I found the following very stupid line: int a = b * (c * d * + e) If you don't see it: Between d and e I wrote * +, where just a +was intended. Why does this compile and what…
Michael
  • 7,407
  • 8
  • 41
  • 84
67
votes
6 answers

Why does NaN^0 == 1

Prompted by a spot of earlier code golfing why would: >NaN^0 [1] 1 It makes perfect sense for NA^0 to be 1 because NA is missing data, and any number raised to 0 will give 1, including -Inf and Inf. However NaN is supposed to represent…
Simon O'Hanlon
  • 58,647
  • 14
  • 142
  • 184
48
votes
3 answers

How do I add two integers together with Twisted?

I have two integers in my program; let's call them "a" and "b". I would like to add them together and get another integer as a result. These are regular Python int objects. I'm wondering; how do I add them together with Twisted? Is there a…
Glyph
  • 31,152
  • 11
  • 87
  • 129
48
votes
1 answer

How to multiply/divide/add/subtract numbers of different types?

I'm working through the second edition of the Rust handbook, and decided to try and make the classic Celsius-to-Fahrenheit converter: fn c_to_f(c: f32) -> f32 { return ( c * ( 9/5 ) ) + 32; } Compiling this with cargo build will yield the…
Kenny Worden
  • 4,335
  • 11
  • 35
  • 62
40
votes
7 answers

Prolog =:= operator

There are some special operators in Prolog, one of them is is, however, recently I came across the =:= operator and have no idea how it works. Can someone explain what this operator does, and also where can I find a predefined list of such special…
nubela
  • 1
  • 24
  • 75
  • 123
35
votes
2 answers

Is Python's == an equivalence relation on the floats?

In native Python, without using NumPy (for which numpy.nan != numpy.nan) there is no NaN, so am I right in thinking that Python's floating point == is reflexive? Then since it is symmetric (a == b implies b == a) and transitive (if a==b and b==c…
xnx
  • 24,509
  • 11
  • 70
  • 109
32
votes
2 answers

bash: $[] vs. $(())

I have just stumbled upon the bash syntax: foo=42 bar=$[foo+1] # evaluates an arithmetic expression When I Googled for this, I found http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_03_04.html#sect_03_04_05: 3.4.6. Arithmetic…
Chen Levy
  • 15,438
  • 17
  • 74
  • 92
30
votes
3 answers

How do promotion rules work when the signedness on either side of a binary operator differ?

Consider the following programs: // http://ideone.com/4I0dT #include #include int main() { int max = std::numeric_limits::max(); unsigned int one = 1; unsigned int result = max + one; std::cout <<…
Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
28
votes
2 answers

Ambiguous reference to member '=='

That must be a basic mistake, but I can't see what is wrong in this code: .... object is some NSManagedObject .... let eltType = ((object.valueForKey("type")! as! Int) == 0) ? .Zero : .NotZero At compile time, I get this message: Ambiguous…
Michel
  • 10,303
  • 17
  • 82
  • 179
25
votes
3 answers

Subtraction between signed and unsigned followed by division

The following results make me really confused: int i1 = 20-80u; // -60 int i2 = 20-80; // -60 int i3 =(20-80u)/2; // 2147483618 int i4 =(20-80)/2; // -30 int i5 =i1/2; // -30 i3 seems to be computed as (20u-80u)/2, instead of…
Leo Lai
  • 863
  • 8
  • 17
23
votes
4 answers

In R why is factorial(100) displayed differently to prod(1:100)?

In R I am finding some odd behaviour that I can't explain and I am hoping someone here can. I believe that the value of 100! is this big number. A few lines from the console showing expected behaviour... >factorial( 10 ) [1] 3628800 >prod( 1:10…
Simon O'Hanlon
  • 58,647
  • 14
  • 142
  • 184
1
2 3
45 46