Algorithm troubles - can't work this one out yet

I’ve worked this out before using an old version of FileMaker, and of course regardless of filemaker or any other programming language, the algorithm should still hold true. I’ve looked at my old filemaker code, but I can’t understand how I got the results, but it did work.

In any case, I’m starting again.

So, I have a variable: totalImpulseNS (as double)

[code]totalImpulseNS = 206.08

// Evaluate Total Impulse CLASSIFICATION

Select Case totalImpulseNS
Case 1.25 To 2.50
lblTotalImpulseCLASS.Text = “A”
Case 2.50 To 5.00
lblTotalImpulseCLASS.Text = “B”
Case 5.00 To 10.00
lblTotalImpulseCLASS.Text = “C”
Case 10.00 To 20.00
lblTotalImpulseCLASS.Text = “D”
Case 20.00 To 40.00
lblTotalImpulseCLASS.Text = “E”
Case 40.00 To 80.00
lblTotalImpulseCLASS.Text = “F”
Case 80.00 To 160.00
lblTotalImpulseCLASS.Text = “G”

Case 160.00 To 320.00
  lblTotalImpulseCLASS.Text = "H"

Case 320.00 To 640.00
  lblTotalImpulseCLASS.Text = "I"
Case 640.00 Yo 1280.00
Case Else
  lblTotalImpulseCLASS.Text = "Unknown"
End Select[/code]

It’s easy enough to see the results, in that “lblTotalImpulseCLASS” is given a classification of “H”

That’s fine, and all working correctly at this point.

Now I need to evaluate a percentage. If you look at classification “H” it has a range of 160 to 320. My original value is 206 and therefore meets the classification requirement. But this classification “H” can easily be given to any number that falls between 160 to 320. Therefore a percentage is required to fully comprehend it and show a properly evaluated result.

In this case, I do know that the “True” classification is H29% from the old filemaker version.

I can’t seem to re-work this out. It should be simple but slips my grasp at the moment.

Hi,

subtract the lower limit of the class to your value and to the upper limit of the class, the percentage is what remains of your value over what remains of the upper limit (by 100):

(totalImpulseNS-160)/(320-160)*100=28.8

HTH,
Julen

As an aside, if the value is exactly one of the range limits, say 10.0, it will be 100% of the lower range. I’m just pointing that out in case it’s not what you want.

or how about with no SELECT statement at all?

		Dim low As Double=1.25
		Dim high As Double
		Dim i As Integer
		Dim letter="Unknown"
		Dim pct As Double=0
		For i=1 To 9
				high=2*low
				If totalImpulseNS>=low And totalImpulseNS<high Then 
						letter=Mid("ABCDEFGHI",i,1)
						pct=(totalImpulseNS-low)/(high-low)*100
						Exit For
				End If
				low=high
		Next i
		lblTotalImpulseCLASS.Text = letter

I’ve used the code suggested by Julen. It works perfectly and makes a lot more sense that my original filemaker code.

@ Kem
Thanks Kem. Yes I am aware of that. At first I thought it could be an issue which could be fixed by having the next class start at 10.0001 etc. but because of the order of case statements, the potential issue resolves itself. ie. 10 would be 100% of class C, whereas anything over 10 would push it into class D - which is exactly as it should be.

@ Dave
Thanks Dave. In some way it would be good to have the code more concise along the lines you have suggested. However, for simplicity there were a few classifications that I omitted: “Micro, 1/4 A and 1/2 A” and therefore Mid would not work in this instance. In any case, I’m happy to have the code spelt out in full.

These classifications are ‘industry standards’ and therefore fixed and can be hard-coded.

This is a snippet of my final code:

[code]totalImpulseNS = 206.08

// Evaluate Total Impulse CLASSIFICATION
Dim lowTI, highTI as Double

Select Case totalImpulseNS
  
Case 0 To .3125
  lowTI=0
  highTI=.3125
  lblTotalImpulseCLASS.Text = "Micro"

'---- etc…

Case 160 To 320
  lowTI=160
  highTI=320
  lblTotalImpulseCLASS.Text = "H"

End Select

totalImpulsePERCENT=(totalImpulseNS-lowTI)/(highTI-lowTI)*100
lblTotalImpulsePERCENT.Text=str(Format((totalImpulsePERCENT),"###")) + "%"

// Draw the indicator (indicator is 100px wide)
totalImpulseIndicator.Width =  totalImpulsePERCENT

[/code]

The percentage as whole is fine as it’s only for “visual purposes”.

In the end, this was perhaps more of an issue with basic math than the algorithm. All working well now.

Thanks to all.

Cheers.

Shouldn’t Julen’s post be the answer then?

I recently notice an increase in the bad etiquette of people marking their own post as answer.

Well Markus, considering that you were a non contributor to the original post, you somehow managed to work out that Julen’s suggestion was the answer. :stuck_out_tongue: teee heee.

Fair enough though, you have a good point. I’m guilty of doing this on more than one occasion. Not through arrogance and disrespect, but through ignorance. I did however refer to Julen being the one providing the solution in the very first sentence of my last post: “I’ve used the code suggested by Julen”. Although I did forget to thank Julen specifically, so thanks Julen. :slight_smile:

From my point of view, it’s good to have the final “used” solution as well as the attributing code that helped solve it. I’m not sure how you go about doing this easily.

But that’s not the worst thing about this thread. The worst thing is the non-specific title: “Algorithm troubles - can’t work this one out yet” which is meaningless, stupid and/or could mean anything.

I probably should change the title to: “Calculating a percentage between a set range of numbers” or similar, that may at least be helpful to others in future having a similar question.

Cheers :slight_smile: