Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calculating the High and Low of it all
#1
So, I have a massive data base of decimal value with 5 digits after the decimal. I have routine which is trying to find the highest and the lowest of these values. Here is the algorythm that I am using but for some reason it's giving me the Highest value as the Lowest and the Lowest as the Highest. 

        HL = DataBaseValue
        If HL < 1 And HL < Low Then Low = HL
        Low = (_Round(Low * 100000)) / 100000
        If HL < 1 And HL > High Then High = HL 

        High = (_Round(High * 100000)) / 100000

The rounding is to avoid scientific notation and be sure result will be 5 digit decimal value.

I can't see why this algorythm would give the High as Low and the Low as High.
Reply
#2
If HL < 1 ??? you only want numbers less than 1?
b = b + ...
Reply
#3
Try this, instead.

Demo

Code: (Select All)
DataBaseValue$ = "7,3,9,4,2,6,2,8"
FOR i = 0 TO 7
    DataBaseValue = VAL(MID$(DataBaseValue$, i * 2 + 1, 1))
    FOR j = 0 TO 7
        DataBaseValue = VAL(MID$(DataBaseValue$, j * 2 + 1, 1))
        IF i <> j THEN
            IF DataBaseValue > HL THEN SWAP HL, DataBaseValue ELSE low = DataBaseValue
            'Low = (_ROUND(Low * 100000)) / 100000
            'HL = (_ROUND(HL * 100000)) / 100000
        END IF
    NEXT
NEXT
PRINT "Low ="; low, "High ="; HL

Pete
Reply
#4
The example I posted uses a double loop and swap to rank the database numbers from high to low. It's basically a non-optimized sorting routine. You can put back in the rounding, as needed. Don't let the MID$() stuff confuse you. You don't need it for your program. I put it in the demo as a way to spit out numbers. Just substitute DataBaseValue for DataBaseValue = VAL(MID$()... and you're good to use.

Pete
Reply
#5
simply start with really low high and really high low , rnd is always < 1 so for example only
Code: (Select All)
high = -100000
low = 10
For i = 1 To 100
    HL = Rnd
    If HL < low Then low = HL
    low = (_Round(low * 100000)) / 100000
    If HL > high Then high = HL
    high = (_Round(high * 100000)) / 100000
Next
Print low, high
b = b + ...
Reply
#6
That's more to the point, as Dim didn't ask for them to be ranked, just pick out the lowest and the highest.

If you don't want to seed it, just do...

Code: (Select All)
FOR i = 1 TO 100
    HL = RND
    IF HL < low OR i = 1 THEN low = HL
    low = (_ROUND(low * 100000)) / 100000
    IF HL > high OR i = 1 THEN high = HL
    high = (_ROUND(high * 100000)) / 100000
NEXT
PRINT low, high
Reply
#7
Thanks guys, appreciate the help, I'll give them all a try.
Reply
#8
If you pick Mark's over mine, no worries. I have no ego invested in this, and besides, my brothers Rocko and Vinnie know where you live.

Pete Big Grin
Reply
#9
low = (_Round(low * 100000)) / 100000

BTW this little trick does not work all the time, in fact it fails to return a controlled max number of decimal digits disgustingly often.
Code: (Select All)
Dim low As Double
For low = 0 To .01 Step .00001
    low = (_Round(low * 100000)) / 100000
    Print low
Next
b = b + ...
Reply
#10
Sometimes it's better to be SINGLE...

Code: (Select All)
$CONSOLE:ONLY
DIM low AS SINGLE
FOR low = 0 TO .01 STEP .00001
    low = (_ROUND(low * 100000)) / 100000
    PRINT low
NEXT

Pete
Reply




Users browsing this thread: 5 Guest(s)