How to make my program switch commands at certain intervals/bounds in c++

  • Context: C/C++ 
  • Thread starter Thread starter odinx
  • Start date Start date
  • Tags Tags
    C++ Program Switch
Click For Summary

Discussion Overview

The discussion revolves around implementing a command-switching mechanism in a C++ program based on time intervals. Participants explore how to alternate between two formulas depending on the value of time relative to a user-defined parameter T, specifically switching at intervals of T/2.

Discussion Character

  • Technical explanation
  • Mathematical reasoning

Main Points Raised

  • One participant describes the need for the program to apply formula 1 when time is in the range [0, (T/2)] and formula 2 when time is in the range [(T/2), T], with the intervals continuing to expand based on user input.
  • Another participant suggests using the expression (((time*2)/T)&1) for integer values of time and T to determine the current interval.
  • A later reply clarifies that time and T are not integers, prompting a suggestion to use fmod((time * 2.0) / T, 2.0) to handle floating-point values and check the remainder to decide which formula to apply.
  • It is noted that including math.h is necessary to use the fmod() function.

Areas of Agreement / Disagreement

Participants present different approaches based on the data types of time and T, indicating a lack of consensus on the best method for handling non-integer values.

Contextual Notes

The discussion does not resolve the specific implementation details or confirm the effectiveness of the proposed solutions for all scenarios.

odinx
Messages
9
Reaction score
0
I have a new problem where I have 2 formulas, and I need to tell my program that at certain intervals/parameters/bounds it needs to do one of the formulas and at other intervals it needs to do another one.

To be more descriptive When ever my time=0 to time=(T/2) it should do formula 1, and whenever it is time=(T/2) to time=T it does formula 2. So looking at it another way my
time= [0, (T/2)] it does formula 1 and at time=[(T/2), T] it does formula 2.

Meaning that it switches positions/formulas at every (T/2).

I just don't know what command I would do to make this happen in c++ at first I thought of using the bool command, but the thing is the length of time the program is run is determined by the user, so the longer the time the more intervals.

Let me explain what I mean, after the intervals time=[(T/2), T] it will keep going to make a new interval of time=[T, (3T/2)], thus switching to formula 1 again, and the intervals will keep getting bigger because of the time used.

So any ideas? Also I'm sorry if my explanation was horrid.
 
Technology news on Phys.org
If time and T are integers, you could use (((time*2)/T)&1), which will return 0 or 1 depending on the interval.
 
They aren't int values
 
For float or double, you can use fmod((time * 2.0) / T, 2.0) and check if the remainder is < 1. or >= 1.0.
Include math.h in order to use fmod().
 

Similar threads

Replies
14
Views
4K
  • · Replies 31 ·
2
Replies
31
Views
3K
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 15 ·
Replies
15
Views
8K
Replies
86
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K