Need help with a somewhat complex math equation in Java/Acroscript

delericho

Legend
Doh! I've made a stupid mistake - one of those that is obvious as soon as you spot it.

Reverse the order of the last three clauses in the if... else if... tree - that is, handle the >100 case first then the >95 and then >90, and that should get it.

Code:
var oldStat = this.getField("St.0").value var newStat = this.getField("NewStat1").value 
var oldVal = 0 
var newVal = 0 
if(oldStat <= 90) { oldVal = oldStat } 
else if(oldStat > 100) { oldVal = 115 + ((oldStat - 100) * 10) }
else if(oldStat > 95) { oldVal = 100 + ((oldStat - 95) * 3) } 
else if(oldStat > 90) { oldVal = 90 + ((oldStat - 90) * 2) } 


if(newStat <= 90) { newVal = newStat } 
else if(newStat > 100) { newVal = 115 + ((newStat - 100) * 10) }
else if(newStat > 95) { newVal = 100 + ((newStat - 95) * 3) } 
else if(newStat > 90) { newVal = 90 + ((newStat - 90) * 2) } 
event.value = (newVal - oldVal)
 

log in or register to remove this ad

tomBitonti

Adventurer
Code:
if(oldStat <= 90) { oldVal = oldStat } 
else if(oldStat > 100) { oldVal = 115 + ((oldStat - 100) * 10) }
else if(oldStat > 95) { oldVal = 100 + ((oldStat - 95) * 3) } 
else if(oldStat > 90) { oldVal = 90 + ((oldStat - 90) * 2) }

May I suggest:

Code:
if (oldStat<0) { oldVal = 0 }
else if (oldStat > 100) { oldVal = 115 + ((oldStat - 100) * 10) }
else if (oldStat > 95) { oldVal = 100 + ((oldStat - 95) * 3) }
else if (oldStat > 90) { oldVal = 100 + ((oldStat - 90) * 2) }
else { oldVal = 0 + (oldStat - 0) * 1 }

I added in the initial case to force a value which is always zero or higher. (And to create a pre-condition for the remaining tests: The stat is known to be zero or higher for all of those tests.)

The case of a stat being at least zero and no greater than ninety was moved to the end. That puts the whole sequence in descending order.

The last case was written in the same form as the other case to unify the cases and to show that the per-point cost was one. That makes it easier to add in new branches, or to change the point cost.

The form will work, but is somewhat troublesome to use if any update is made: The formulas bake in the point cost for the different ranges.

If you don't want to bake in the prior costs, you can use this:

Code:
if (oldStat<0) {
  oldVal = 0
} else {
  if (oldStat <= 90) {
    oldVal = oldStat * 1
  } else {
    oldVal = 90 * 1
    if (oldStat <= 95) {
      oldVal = oldVal + (oldStat - 90) * 2
    } else {
        oldVal = oldVal + (95 - 90) * 2
        if (oldStat <= 100) {
          oldVal = oldVal + (oldStat - 95) * 3
        } else {
          oldVal = oldVal + (100 - 95) * 3
          oldVal = oldVal + (oldStat - 100) * 10
      }
    }
  }
}

And if you are OK to look at the costs as costs cumulative with the prior costs:

Code:
if (oldStat<0) {
  oldVal = 0
} else {
  oldVal = oldStat * 1
  if (oldStat > 90) {
    oldVal = oldVal + (oldStat - 90) * 1
    if (oldStat > 95) {
      oldVal = oldVal + (oldStat - 95) * 1
      if (oldStat > 100) {
        oldVal = oldVal + (oldStat - 100) * 7
      }
    }
  }
}

Notice that the costs add up to 1, 2, 3, and 10, matching the formula initially given.

If the language has a "+=" operator,
Code:
oldVal = oldVal + expr
can be shortened to
Code:
oldVal += expr
.

Thx!

TomB
 

delericho

Legend
Code:
if(oldStat <= 90) { oldVal = oldStat } 
else if(oldStat > 100) { oldVal = 115 + ((oldStat - 100) * 10) }
else if(oldStat > 95) { oldVal = 100 + ((oldStat - 95) * 3) } 
else if(oldStat > 90) { oldVal = 90 + ((oldStat - 90) * 2) }

May I suggest:

Code:
if (oldStat<0) { oldVal = 0 }
else if (oldStat > 100) { oldVal = 115 + ((oldStat - 100) * 10) }
else if (oldStat > 95) { oldVal = 100 + ((oldStat - 95) * 3) }
else if (oldStat > 90) { oldVal = 100 + ((oldStat - 90) * 2) }
else { oldVal = 0 + (oldStat - 0) * 1 }

Yep, that's better.
 

Madmaxneo

Explorer
Yeah I should have noticed the order should have been reversed, thanks for that. It works great now, everything is much appreciated!

Interesting things to know, I especially like the
Code:
oldVal += expr
I wonder if it is any quicker. The biggest problem with using Acrobat for any of this is that it takes time to do every calculation and I have some scripts that cover a lot of fields. One takes close to 5 mins to calculate everything, but that one is taking all the new skill ranks the player developed for the current level and adding them to the old skill ranks, then deleting the new ones from the appropriate fields. But that's another story.

One question on the code you (TomB) recommended, wouldn't this:
Code:
else { oldVal = 0 + (oldStat - 0) * 1 }
be the same as this?
Code:
else { oldVal =oldStat}
The adding and subtracting zeros then multiplying by one seems irrelevant.

Bruce
 

tomBitonti

Adventurer
Yeah I should have noticed the order should have been reversed, thanks for that. It works great now, everything is much appreciated!

Interesting things to know, I especially like the
Code:
oldVal += expr
I wonder if it is any quicker. The biggest problem with using Acrobat for any of this is that it takes time to do every calculation and I have some scripts that cover a lot of fields. One takes close to 5 mins to calculate everything, but that one is taking all the new skill ranks the player developed for the current level and adding them to the old skill ranks, then deleting the new ones from the appropriate fields. But that's another story.

One question on the code you (TomB) recommended, wouldn't this:
Code:
else { oldVal = 0 + (oldStat - 0) * 1 }
be the same as this?
Code:
else { oldVal =oldStat}
The adding and subtracting zeros then multiplying by one seems irrelevant.

Bruce

Yes; exactly the same, except that the "0 + (oldStat - 0) * 1" has a better tie to the formulas which you originally presented.

Not sure about acrobat, but many languages will optimize out the redundant steps.

Parts of a computer program are to express intent, and to convincingly show that the code achieves that intent.

Thx!

TomB
 

Remove ads

AD6_gamerati_skyscraper

Remove ads

Recent & Upcoming Releases

Top