1

I'm looking for a way to somehow count amount of steps:

public static int Calculate0(int end, int init, int lim, int bon)
{
    return end <= 0
        ? 0

        : Math.Min(2 * lim, bon == 0 ? init : init + (2 * bon - lim / bon) * end);
}

I guess my problem kind of two fold:

  1. I dont understand the the special :? operator in C# and
  2. I don't know where to input some kind of variable into the Calculate0 method.

Been trying to read about the :? operator through Microsoft's guide but I still struggle with understanding what happens inside Calculate0.

My code is currently looking like this. Can this be correct?

using System;

namespace TestProject {
    internal class Program
    {
        private int calc0Steps = 0;

        public static void Main() {

            var calc0 = Program.Calculate0(1, 0, 1, 2);
            Console.WriteLine("Calculate: {0} | Steps: {1}", calc, calc0Steps);

        }

        public static int Calculate0(int end, int init, int lim, int bon)
        {
            return end <= 0
                ? 0

                : calc0Steps; Math.Min(2 * lim, bon == 0 ? init : init + (2 * bon - lim / bon) * end);
        }
    }
}

Update

I'm sorry for being confusing. I'll try to narrow it down: How can I put a counter into Calculate0?

The main scope of my assignment is to do a full test coverage of the method fhcimolin provided and compare this method to Calculate0. A small side task is to count the computational steps. But I had no idea how I implemented a counter in Calculate0 in the first place.

I'm having another version of Calculate0 looking like fhcimolin's answer, where I can put in a counter. And I need to count how many computation steps there are in both.

halfer
  • 19,824
  • 17
  • 99
  • 186
jubibanna
  • 1,095
  • 9
  • 21
  • We don't know what you are trying to calculate, so maybe you need to explain that. Include expected and actual outcome for the given values – Hans Kesting Mar 12 '19 at 16:19
  • If you find the conditional operator (?:) confusing, then use plain if/else statements to perfect your calculation – Hans Kesting Mar 12 '19 at 16:20
  • The return statement seems to have two statements? First the ternary `return end <= 0 ? 0 : calc0Steps` and second, the `Math.Min` part? Won't it accuse of unreachable code? – fhcimolin Mar 12 '19 at 16:21
  • 1
    To answer your question 1, the ? is the ternary operator. you may be familar with IIF from VB6, it's similar in function. the statement `bon == 0 ? init : init + (2 * bon - lim / bon) * end` can be read as: If this `bon == 0` is true `?` use this value `init` otherwise `:` use this `init + (2 * bon - lim / bon) * end` – Andrew Mar 12 '19 at 16:24
  • I think the point is to count computational steps manually. Not with a counter. – Jesse de Wit Mar 12 '19 at 20:11
  • Pro-tip: when adding an update to your question, see if it can be melded into the question seamlessly, as if you asked the question like that in the first place (there is a revision history for folks who wish to see earlier versions). If you think that would be too confusing for current participants, put it at the end. Do **not** put it at the start, since that will be even more confusing for the long tail of folks who read your question in the future. Remember that the priority of Stack Overflow is to help future readers, and so they are the folks you should aid most in your writing. – halfer Mar 12 '19 at 20:30
  • Define "computational steps." Also, the optimizer will likely generate the same code for both the ternary operator and the `if...else` version. – 3Dave Mar 12 '19 at 20:33
  • @Halfer: Thanks for the input, and next time I will put it in the end. Makes sense – jubibanna Mar 12 '19 at 20:45
  • @jubibanna If you wish to count each step in the `Calculate0` method, granted that the interpretation of "step" is entering an `if/else`, the only way to do that is to turn all the ternary operators into good old `if/else` blocks. That's because you need two statements: one to actually do your logic, and other to add to the counter. With a ternary, you can't have two statements. – fhcimolin Mar 14 '19 at 11:38

1 Answers1

1

If I got your logic right, you might want your Calculate0 method to look like this:

public static int Calculate0(int end, int init, int lim, int bon)
{
    return end <= 0 ? calc0Steps : Math.Min(2 * lim, bon == 0 ? init : init + (2 * bon - lim / bon) * end);
}

Which would the be equivalent of:

public static int Calculate0(int end, int init, int lim, int bon)
{
    if (end <= 0)
    {
        return calc0Steps;
    }
    else
    {
        int aux;

        if (bon == 0) {
            aux = init;
        }
        else
        {
            aux = init + (2 * bon - lim / bon) * end;
        }

        return Math.Min(2 * lim, aux);
    }
}
fhcimolin
  • 616
  • 1
  • 8
  • 27
  • I've edited my question to be more clear. I thank you for the time you spent and I'm sorry for wasting your time by being unprecise. Amazing how close you are to the actual other method we are testing against. We have an assignment (datascience first year) where we need to compare these two methods and count the computation steps. This is just a small part of the assignment, the biggest part is to do a 100% test coverage! Again thank you – jubibanna Mar 12 '19 at 19:51
  • @jubibanna I accidentally overlooked that aspect of the question. I focused instead in solving your syntax problem. Now you're talking about complexity, and I suggest you get a read on [this](https://stackoverflow.com/questions/17328641/ternary-operator-is-twice-as-slow-as-an-if-else-block/17331230). I don't think the complexity is supposed to be affected (between an if/else block and a ternary operator), but some folks put it to the test, and it's technically slower. – fhcimolin Mar 12 '19 at 20:14