-1

I want shorten the if condition. How can I simplify this? Where step = 1 to 240 in min

I want 1st 3 min ON= 1, second 10 min OFF = 0, It will repeated until 248 min I wrote "if condition" which is consist of many condition. How to rewrite the code with simple expression

   #include "udf.h"
   DEFINE_PROFILE(ON_3min_OFF_10min_4Hr,thread,position) 
   {
   face_t f;
   real step,hf_3min,hf_10min;
   hf_3min = 1;     /*ON */
   hf_10min = 0;       /*OFF */
   step=N_TIME;
   begin_f_loop(f,thread)
   {
   
      if ((step<=3) || ((step>13) && (step<=16)) || ((step>26) && (step<=29)) || ((step>39) && (step<=42)) || ((step>52) && (step<=55)) || ((step>65) && (step<=68)) || ((step>78) && (step<=81)) || ((step>91) && (step<=94)) || ((step>104) && (step<=107)) || ((step>117) && (step<=120)) || ((step>130) && (step<=133)) || ((step>143) && (step<=146)) || ((step>156) && (step<=159)) || ((step>169) && (step<=172)) || ((step>182) && (step<=185)) || ((step>195) && (step<=198)) || ((step>208) && (step<=211)) || ((step>222) && (step<=225)) || ((step>235) && (step<=238)))
        {
          F_PROFILE(f,thread,position)=hf_3min;
        }
    else
        {
          F_PROFILE(f,thread,position)=hf_10min;
        }
   }
  end_f_loop(f,thread)
}
Dijkgraaf
  • 11,049
  • 17
  • 42
  • 54
Siddhu
  • 48
  • 6
  • 7
    Please choose C or C++, as the answers may very well be different. – Fred Larson Jul 16 '20 at 15:15
  • For C++ a condition that long I'd maybe make a static unordered_set and just check if the value was in the list. You'd have more numbers in the set, but the code would look a lot cleaner. – Chase R Lewis Jul 16 '20 at 15:15
  • 1
    @ChaseRLewis: My thought was more like a `std::vector` of `std::pair`, with each entry being one of the ranges. Similar idea. – Fred Larson Jul 16 '20 at 15:17
  • 1
    I would use a 2D-Array and iterate over it, because it is always of this type: `(step >n1)&&(step <= n2)` – JCWasmx86 Jul 16 '20 at 15:18
  • 3
    The first thing that would be important, is to explain what this condition tests. Is the ranges always 3 long? Are they always 10 apart (base on `(step <= 211)) || ((step > 222)` it is not)? – t.niese Jul 16 '20 at 15:20
  • 1
    The limits for `step` are `0` and `255`? what about a 256-bit "value" where you use `hf` when the bit is unset, `hf2` when it's set? Then it's just one access to the particular 'bit' *(\*) you can encode 256 bits in 32 unsigned chars* – pmg Jul 16 '20 at 15:22
  • yeah I didn't see they were always modulo 13 @user3121023 solution is probably most optimal. However 0 is allowed in the original condition I think the condition is if(step <= 3 || (step % 13 >= 1 && step % 13 <= 3)). 0 being allowed in the original might be a 'bug' though in which case it could be made even simpler. – Chase R Lewis Jul 16 '20 at 15:25
  • 3
    This question is badly stated. You should describe what this code should do. If we know that we could solve it in many different more crafty ways. Also read about [Magic Numbers](https://stackoverflow.com/a/47902/1387438). – Marek R Jul 16 '20 at 15:37
  • 1
    Surely if you told us (and yourself) where these numbers are coming from, the answer would be much more obvious - as these are clearly have a pattern. – Eugene Sh. Jul 16 '20 at 16:32

1 Answers1

4

From the bounds that you are checking, it seems your code is equivalent to:

if ((step - 1) % 13 < 3) {
  F_PROFILE(f, thread, position) = hf;
} else {
  F_PROFILE(f, thread, position) = hf2;
}

You need to be careful about the missing lower bound on the first condition, and the change of the bounds at the last 2 conditions, in case that's intentional.

cigien
  • 57,834
  • 11
  • 73
  • 112
  • At the `((step > 222) && (step <= 225))` and following it is shifted by, compared to the previous ranges. – t.niese Jul 16 '20 at 15:25
  • 1
    @t.niese Yes, you're right. My guess is that's actually a typo :p Possibly OP calculated the conditions by hand. – cigien Jul 16 '20 at 15:26
  • 3
    Another good reason not to write out massive expressions like this – Asteroids With Wings Jul 16 '20 at 15:26
  • 1
    I guess so too. That's why I asked the OP to explain the meaning of that expression ;) – t.niese Jul 16 '20 at 15:27
  • I don't think this is 'correct'. If I put 13 in there I'll get 1 <= 3 which is true when the original condition had a step > 13 not step >= 13. Only 0 is included in the original range, but like you said it's weird :/ so maybe 0 shouldn't be included and that's a bug. – Chase R Lewis Jul 16 '20 at 15:32
  • @ChaseRLewis You're right, corrected. Also added a line warning about the weirdness :) – cigien Jul 16 '20 at 15:35
  • @cigien Thank you, i will try the code which you written. – Siddhu Jul 16 '20 at 15:42
  • I would write `F_PROFILE(f, thread, position) = (step - 1) % 13 < 3 ? hf : hf2;` – Slava Jul 16 '20 at 16:20
  • @Slava Well, the asker hasn't really given us enough information, thay may be talking about this: http://jullio.pe.kr/fluent6.1/help/html/udf/node116.htm – Bob__ Jul 16 '20 at 16:57
  • @Bob__ that's irrelevant, I assumed it is correct code. The point is I try to avoid code duplication where it is possible. – Slava Jul 16 '20 at 18:28