0

I am counting the primitive operations a piece of code and I am not sure if converting units counts as another operation. For example, converting the units to int:

int avg = (int) (high - low);

Without counting the cast, there are 2 operations in this code. The assignment of avg and the arithmetic. Am I supposed to count the cast too?

Chris
  • 437
  • 1
  • 4
  • 18
  • 1
    You might be missing the point of Big-O complexity problems. The goal is to expression the running time as a function of some input size, possibly the size of the collection on which the code is operating. – Tim Biegeleisen Oct 08 '18 at 05:04
  • 3
    As a complete aside: the difference between two numbers, eg. `high` and `low`, is not their average (`avg`). – dave Oct 08 '18 at 05:05
  • I need to count primitive operations...Does casting a variable count as one? – Chris Oct 08 '18 at 05:05
  • I made up the example btw – Chris Oct 08 '18 at 05:05
  • Can you show us the entire method, and point out what is the input and output? – Tim Biegeleisen Oct 08 '18 at 05:07
  • 1
    do you think the casting will happen without operation? I am not sure whether it is constant time or not, but it actually it is an operation to be counted. – The Scientific Method Oct 08 '18 at 05:09
  • Without counting the cast, there are *4* operations in this code: load high, load low, subtract, (cast), store. But there are always this number of operations, as there is no way to vary the size of input, as you are always operating on 2 things. So it has constant time complexity, i.e. O(1). – Andy Turner Oct 08 '18 at 05:42

1 Answers1

1

I believe that you're trying to get a mathematical formula of the number of primitive operations needed given an input to a method. Similar to what is done in the "Primitive Operations" section in https://www.cpp.edu/~ftang/courses/CS240/lectures/analysis.htm#primitive_operations

The answer is maybe. I would consider it a primitive operation because whether or not casting can happen first needs to be checked. For example, in java: Does Java casting introduce overhead? Why?