I'm writing a music generator in C++, and I'm currently working on BPM. To get the amount of time to wait between notes, I'm using 60 / bpm, but this evaluates to zero. I have checked to make sure that bpm is declared, and it is. Trying 60 / bpm for some reason gives 2. Why is this?
Because 60 / 120 is 0 considering the constants are integral. (Inferring 120 from x / 60 = 2.) You will need to use 60.0 / x for example to get a floating point number as a result.
Related
I am working on a game where I need an algorithm to vary a value in a loop. I have implemented the algorithm but I guess its not working as I want it to work. Here's what I want and what I have already implemented :
Given :
a commodity whose price I want to circulate (from min to max to min again and continuously in a loop)
I am using cocos2d-x (C++) where I have a scheduler which runs a function at a given interval say SCHEDULE_INTERVAL
MIN_PRICE and MAX_PRICE of the commodity
currentPrice
Time duration which it will take to complete one cycle (min-max-min)
Current Implementation :
SCHEDULE_INTERVAL = 0.3 (sec) (so the function is running every 0.3 secs)
counter = 0;
timeDuration = time to complete one cycle
function
{
counter++;
_amplitude = (maxPrice - minPrice)/2;
_midValue = (maxPrice + minPrice)/2;
currentPrice = _midValue + _amplitude * sin (2*PI*counter/timeDuration)
}
why i am using sine wave : because at the peaks i want to make the transitions slow.
Problem : for some reasons its not behaving the way I want it to behave
I want to continuously change the currentPrice form minPrice-maxPrice-minPrice in timeDuration and the loop running at SCHEDULE_INTERVAL
please suggest any solutions.
Thanks :)
EDIT :
what's not working in the above implementation is that the values are not changing according to the 'timeDuration' variable
If the pseudocode you posted accurately mirrors the expressions you use in real code, you probably want to change the argument of sin to this:
2 * PI * (counter * SCHEDULE_INTERVAL) / timeDuration
counter is the number of executions, while timeDuration is (I presume) the desired length in seconds.
In other words, your units don't match - it's always worthwhile to perform a dimensional analysis when formulae don't work.
I work with SoapUI project and I have one question. In following example I've got 505 requests in 5 seconds with thread count =5. I would like to understand how count has been calculated in this example.
For example, if I want 1000 request in 1 minute what setting should I set in variance strategy?
Regards, Evgeniy
variance strategy as the name implies, it varies the number of threads overtime.Within the specified interval the threads will increase and decrease as per the variance value, thus simulating a realistic real time load on target web-service.
How variance is calculated : its not calculated using the mathematical variance formula. its just a multiplication. (if threads = 10 and variance = 0.5 then 10 * 0.5 = 5. The threads will be incremented and decremented by 5)
For example:
Threads = 20
variance = 0.8
Strategy = variance
interval = 60
limit = 60 seconds
the above will vary the thread by 16 (because 20 * 0.8 = 16), that is the thread count will increase to 36 and decrease to 4 and end with the original 20 within the 60 seconds.
if your requirement is to start with 500 threads and hit 1000 set your variance to 2 and so on.
refrence link:
chek the third bullet - simulating different type of load - soapUI site
Book for reference:
Web Service Testing with SoapUi by Charitha kankanamge
Here is a implementation that I wrote to add hrs, min and sec in C++
Is there a way to implement this by converting all this to seconds and do a simpler implementation?
For some reason it wouldn't let me add the code in this post :(
Thank you
Converting hours, minutes, and seconds to just seconds is rather trivial:
total_seconds = hours*60*60 + minutes*60 + seconds;
Is there a way to implement this by converting all this to seconds and do a simpler implementation?
Whichever way you go, you'll need to change the time between seconds and hour/minute/second quite often, so there's not necessarily a huge benefit to using one or the other for storage. Put another way, seconds-since-midnight is probably nicer, but maybe not worth a rewrite. For things like + and -, if you're not storing just seconds then you can calculate it easily (see Mark's post), do your arithmetic then have a function to convert from seconds back to hour/min/sec to store the result into your data members:
hours = seconds_since_midnight / 60 / 60;
minutes = seconds_since_midnight / 60 % 60;
seconds = seconds_since_midnight % 60;
Say you're adding 120 seconds to 23:59:00, you'll get a value past midnight, so you can use "total_seconds % 24 * 60 * 60" to get just the 60 seconds past the following midnight, equivalent to wrapping from "24:01:00" to 00:01:00.
(If you want to actually use this, and not just doing this to teach yourself, then consider using the time() function and associated conversion routines like localtime())
Is there an equivalent to: Microseconds() we can find in the Carbon framework?
** Microseconds()
*
* Summary:
* Determines the number of microseconds that have elapsed since
* system startup time.
*
* Discussion:
* Return a value representing the number of microseconds since some
* point in time, usually since the system was booted. One
* microsecond is 1 * 10^-6 seconds, and so there are one million (
* 1,000,000 ) microseconds per second. For reference, in one
* microsecond light can travel about 850 feet in a vacuum.
*
* Microseconds() doesn't necessarily advance while the computer is
* asleep, so it should not be used for long duration timings.
*
* Parameters:
*
* microTickCount:
* The number of microseconds elapsed since system startup.
*
* Availability:
* Mac OS X: in version 10.0 and later in CoreServices.framework
* CarbonLib: in CarbonLib 1.0 and later
* Non-Carbon CFM: in InterfaceLib 7.1 and later*
Microseconds is a Core Service, and it is always accessible from Carbon. (Carbon sits on top of Core Services).
It originates from classic Mac OS as well.
I'm messing with core.time.Durations - in particular, I'm trying to properly get number of full minutes in "2 days and 1 hour" Duration. As it have cleared, get!"minutes" returns number of minutes without hours, days and weeks (e.g. 0 in this case), so it's inappropriate for me, because I expect answer of 2940.
I've looked into sources and found core.time.getUnitsFromHNSecs function, which does exactly what I need, but it's private to core.time and uses private field _hnsecs of Duration objects.
Of course, it's possible to do
long minutes_in_duration(Duration d) {
return (d.get!"minutes"() + d.get!"hours"() * 60 +
d.get!"days"() * 24 * 60 + d.get!("weeks") * 7 * 24 * 60);
}
but this is clumsy as hell. Is there better way to do the same thing without scattering away Duration's guts?
More thorough reading through source revealed overlooked .total!"unit" property, which does exactly what it should.