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
23
votes
3 answers

Python error in basic subtraction?

Possible Duplicate: Python rounding error with float numbers python maths is wrong I can't get Python to correctly do the subtraction 1 - 0.8 and assign it. It keeps coming up with the incorrect answer, 0.19999999999999996. I explored a bit: sq =…
gotube
  • 671
  • 2
  • 5
  • 22
19
votes
6 answers

Modulus division when first number is smaller than second number

I apologize if this is a simple question but I'm having trouble grasping the concept of modulus division when the first number is smaller than the second number. For example when 1 % 4 my book says the remainder is 1. I don't understand how 1 is the…
Jessica M.
  • 1,451
  • 12
  • 39
  • 54
18
votes
1 answer

let vs expr vs double parenthesis arithmetic in shell

When I was first learning shell scripting, a lot of examples I saw used let for basic arithmetic, but later I found out that some environments don't present let as a built-in, but support use of expr instead (though it's significantly…
Haravikk
  • 3,109
  • 1
  • 33
  • 46
17
votes
1 answer

MATLAB: Is it possible to overload operators on native constructs (cells, structs, etc)?

I'm using cells to manage data in some stuff I'm working on. I'd like to be able to do things like: A = cellfun( @(X)( randn( 5,5 ) ), cell( 5,1 ), 'UniformOutput', 0 ); B = cellfun( @(X)( randn( 5,5 ) ), cell( 5,1 ), 'UniformOutput', 0 ); %# %#…
17
votes
3 answers

C++ Implicit Conversion (Signed + Unsigned)

I understand that, regarding implicit conversions, if we have an unsigned type operand and a signed type operand, and the type of the unsigned operand is the same as (or larger) than the type of the signed operand, the signed operand will be…
2013Asker
  • 2,008
  • 3
  • 25
  • 36
16
votes
4 answers

Does casting `std::floor()` and `std::ceil()` to integer type always give the correct result?

I am being paranoid that one of these functions may give an incorrect result like this: std::floor(2000.0 / 1000.0) --> std::floor(1.999999999999) --> 1 or std::ceil(18 / 3) --> std::ceil(6.000000000001) --> 7 Can something like this happen? If…
hkBattousai
  • 10,583
  • 18
  • 76
  • 124
16
votes
5 answers

Arithmetic vs boolean operations

I've come across this piece of code in some forum: if ( a * b * c * d == 0 ) .... and the owner claims this is a tad faster than if (a == 0 || b == 0 || c == 0 || d == 0) These variables are defined as: int a, b, c, d; And their absolute values are…
user1508893
  • 9,223
  • 14
  • 45
  • 57
15
votes
1 answer

Is it possible to count the number of arithmetic operations in R?

It is possible to record the time that was used to run some code using system.time. Here is a little example: system.time( mean(rnorm(10^6)) ) But I am not only interested in the time but also in the number of arithmetic operations (that is…
ehi
  • 409
  • 8
  • 23
15
votes
1 answer

Optimization of arithmetic expressions - what is this technique called?

A discussion with a friend led to the following realization: >>> import dis >>> i = lambda n: n*24*60*60 >>> dis.dis(i) 1 0 LOAD_FAST 0 (n) 3 LOAD_CONST 1 (24) 6 BINARY_MULTIPLY …
Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
13
votes
2 answers

Basic arithmetic operations on int - Java

I recently noticed an idiosyncrasy of Java regarding basic arithmetic operations in Java. With the following code byte a = 3; byte b = 4; byte c = a * b; I get a "type mismatch" compilation error... Are basic arithmetic operations in Java (+, -, *,…
Jon Gan
  • 867
  • 1
  • 11
  • 22
12
votes
5 answers

Firefox JavaScript arithmetics performance oddity

Please run this test on firefox. http://jsperf.com/static-arithmetic How would you explain the results? This b = a + 5*5; b = a + 6/2; b = a + 7+1; executes much faster than b = a + 25; b = a + 3; b = a + 8; Why?
Frizi
  • 2,900
  • 1
  • 19
  • 25
12
votes
1 answer

Why does the 0x55555556 divide by 3 hack work?

There is a (relatively) well known hack for dividing a 32-bit number by three. Instead of using actual expensive division, the number can be multiplied by the magic number 0x55555556, and the upper 32 bits of the result are what we're looking for.…
user4520
  • 3,401
  • 1
  • 27
  • 50
11
votes
3 answers

Scala division by zero yields different results

I am confused with how Scala handles division by zero. Here is a REPL code snippet. scala> 1/0 java.lang.ArithmeticException: / by zero ... 33 elided scala> 1.toDouble/0.toDouble res1: Double = Infinity scala> 0.0/0.0 res2: Double = NaN scala>…
Ahmadov
  • 1,567
  • 5
  • 31
  • 48
11
votes
2 answers

In R, evaluate expressions within vector of strings

I wish to evaluate a vector of strings containing arithmetic expressions -- "1+2", "5*6", etc. I know that I can parse a single string into an expression and then evaluate it as in eval(parse(text="1+2")). However, I would prefer to evaluate the…
C8H10N4O2
  • 18,312
  • 8
  • 98
  • 134
10
votes
1 answer

PHP does not echo text before + and - sign

With the following code: $a=1; $b=1; echo $a."%".$b." maradéka: "." = ".$a % $b."
"; echo $a."+".$b." összege: "." = ".$a + $b."
"; I get this output: 1%1 maradéka: = 0 2 As you can see, the + syntax is the same as…
JustMatthew
  • 372
  • 3
  • 14
1
2
3
45 46