Math Help :-(
Hi, I really need some help rearranging a formula to display the difference of a total.
Basically, what the code will do is take the necessary amount off an entered Duration so that it does not exceed a credit amount in 'pence'.
I seem to have made one (much simpler) of it work but this one is stomping me.
Heres the simple one -
Difference = (Duration * hRate) - CreditAvailable;
Duration2 = Duration1 - (Difference/hRate);
Duration1 = (int)Duration2;
And heres the one I'm trying to solve -
Difference= (Duration1 - 120) * lRate + (hRate * 120) - CreditAvailable;
Heres where I'm trying things out
Duration1 = (int)Duration2;
[793 byte] By [
Sacreda] at [2007-10-2 3:15:34]

Anyone?
Basically what the statement is supposed to do is adjust the Call Duration so that a call cant be made for longer than the credit available allows, based on a high rate for the first 2 mins (120 secs) and a low rate thereafter.
I just need some help sorting this equation to make this adjustment like the simpler one
The way I understand your question is: given a certain credit amount
and two rates, high (h) and low(l), what is the maximum duration of
a call. The high rate is applicable for the first 120 seconds. Right?
Let the rates be h and l, let c denote the credit and let d denote the
maximum duration. Then
if (c < 120*h) d= (c/h)
else d= 120+(c-120)/l
kind regards,
Jos
JosAHa at 2007-7-15 21:42:46 >

Thanks, I think I have explained it badly though.
Basically, the purpose of the calculation is for a user making a call of a 'requested' duration to be reassigned a new duration based on the credit they have available.
The first 120secs of each call have a higher tariff rate than those thereafter (and this call is more than 120 secs in this instance) I've been using:
Duration = 400secs and CreditAvailable = 900cents at HighRate = 4.3c and LowRate = 2.3 for the low rate
Code:
CallDifference = ((OldDuration - 120) x lowRate) + (highRate * 120)) - CreditAvailable;
New Duration =
As you can see the bit above just calculates the difference between the Cost for the entered duration and the actual credit available to make the call. Which, given the above values for each would be 260
What I want to make the new duration hold is the reduced duration to fall within the credit available limits.
So, The calldifference is 260 because the calculated cost of the call is 260 over the available credit amount (1160-900)
I basically want to substitute the duration in to make the maximum call possible only amount to 900 (or the closest number to it given the rates)
This is so that the user cannot make more call than they have credit.
I've tried various things like -
NewDuration = Duration - (CallDifference/CreditAvailable) * Duration
Which works for the values above but does not hold firm, I need an absolute solution to it, and I'm stomped trying to find it.
If you can give me any more help I'd really appreciate it.
Your problem is not math as such, but understanding your own problem.
If highRateDuration times highRate is greater than or equal to creditAvailable you calculate the availableDuration as the quoteint, like Jos said,
if highRateDuration times highRate is less than creditAvailable you calculate the availableDuration by taking the creditAvailable, subtracting the product of highRateDuration and highRate, dividing by lowRate, and finally adding highRateDuration.
I have the if statement for calculating and deducing the duration if only high rate applies -
else if(Duration < 120 && CreditAvailable < (Duration * hRate)) // WORKING AS OF 24.10
{
NewDuration = (double)Duration;
CallDiff = (Duration * hRate) - CreditAvailable;
NewDuration = Duration - (CallDiff/hRate);
Duration = (int)NewDuration;
CreditUsed = hRate * Duration;
CreditAvailable = CreditAvailable - CreditUsed;
Its doing the same reduction IF the duration entered is MORE than 120 (highrate duration) but LESS than the credit available.
What I have for the IF statement is this -
else if(Duration > 120 && CreditAvailable < (lRate * (Duration - 120) + (120 * hRate))) //NOT WORKING AS OF 24.10!!!!
{
NewDuration = (double)Duration;
CallDiff = ((Duration - 120) * lRate) + (hRate * 120) - CreditAvailable;
NewDuration =............
I will be adding variables for the highrateduration after I sort this out.
Its just that rearranging this that I cant move past.
The first 1 is simple enough but with this one I just need to know how to make the same change.....conventions aside for now.
> Your problem is not math as such, but understanding your own problem.
Your response serves to emphasize this.
You do not need to concern yourself with Duration until the very end!!!
If you are dealing with a call in progress you can compute availableDuration as I outlined, then subtract durationThisCall to get remainingDuration.
But this is what the method does, it inputs a duration, simuating a call.
If there isnt enough credit to cater for this call ATM it goes into negative, obviously I dont want that. So what is the best method of me doing so?
Duration is only existent for the duration of the method, it is not stored for later use, only for the call in hand will it be valid, as I reset it at the end of the method.
What exactly am I doing wrong? Can the problem not be sorted this way?
Given the same meaning for c, h, l and d, here's a little update:
if (c < 120*h) d= c/h
else d= 120+(c-120*h)/l
This simple formula determines the maximum duration d for a given
credit amount c and two rates h and l ... if an actual duration d' < d
is taken by the call, the remaining credit will be:
if (d' < 120) c-= d'*h
else c-= 120*h+(d'-120)*l
kind regards,
Jos
JosAHa at 2007-7-15 21:42:46 >

Thanks, However this does not take into account that C, the credit amount already exists does it? I dont want to give C a new value, I want a new value for D based on the amount in C.a
> Thanks, However this does not take into account that
> C, the credit amount already exists does it?
Yes it does.c-=
means subtract from c.
It may be the answers you are getting are not exactly what you expect because there are really two things we have to compute (at different times).
At the start of a call (or before or during a call) we want to calculate the maximum allowable duration (remaining).
When a call ends we want to update the credit. It is probably easiest to assume the call duration for the call will not cause the credit to go negative - just put a test at the very end - set the credit to zero if it is ends up being negative.
Is this what you want.
else if (Duration >= 120) {
double costForHighRate = 120 * hRate;
double costForLowRate = (Duration - 120) * lRate;
double totalCost = costForHighRate + costForLowRate;
double newDuration = (double) duration;
if (totalCost > CreditAvailable) {
double newCredit = creditAvailable - costForHighRate;
newDuration = (newCredit / lRate) + 120;
}
}
You don't check for duration equal to 120 so I put that in.
What you have it 2 initial costs the high rate and the low rate and given the total duration they give you a total cost.
You only have a problem if this cost is greater than the credit available. The new duration then has to be:
The credit available minus the cost of the high rate. What is left of the credit available can then be divided by the low cost to give the allowed duration for the low cost part of the call. This added to the 120 for high cost gives you a call that cannot cost more than the credit available.
Thanks for the help everyone
Though I've sorted the rearrangement of the formula to get the 'maximum call duration' possible -
NewDuration = (CreditAvailable - hRate*hRateDuration)/lRate + hRateDuration
It is relatively simple but took me like 5 hours to figure out, with LOADS of help, my maths just isn't up to scratch