Shop OBEX P1 Docs P2 Docs Learn Events
Determine which variable in a group contains the largest value! — Parallax Forums

Determine which variable in a group contains the largest value!

In a group of 10 variables that are incrementing randomly or some not at all, after a short time (say 1 minute), I want to determine which of the variables contains the highest value.
Aaron

Comments

  • JonnyMacJonnyMac Posts: 8,988
    edited 2024-01-12 18:13

    I'm coding as I'm having lunch, but this should work. Your values have to be an array of longs.

    pub find_max(p_vals) : idx | maxval, check, x
    
      idx := -1
      maxval := long[p_vals]
    
      repeat x from 1 to 9
        check := long[p_vals][x]
        if (check > maxval)
          maxval := check
          idx := x
    

    Pass a pointer to the array and you'll get back the index of the highest value -- or -1 if all values are the same.

    Edit: Changed the starting index of the repeat loop to 1 as there is no need to compare the first variable with itself.

  • I see this and I think there should be a clever arbitration method of just looking at the Most Significant Bits to determine the largest value. Thinking of something like CANBUS or 1-Wire arbitration methods. ... I miss the old "Golf Challenges" I used to post.

  • AGCBAGCB Posts: 327

    Sorry. I don't understand what "Pass a pointer to the array" means.
    Aaron

  • JonnyMacJonnyMac Posts: 8,988
    edited 2024-01-12 18:02

    "Pass a pointer" means to pass the address of object/variable. In Spin, the @ operator does that. Let's say you have this array:

    var
    
      long  myValues[10]
    
    

    In this case you would call that function with:

      i := find_max(@myValues)
    
  • JonnyMacJonnyMac Posts: 8,988
    edited 2024-01-12 23:00

    Now... down the road you will learn that Spin treats RAM like a giant array, and variables of the same type will be placed into RAM in the order they're declared. What that means is that you could have this:

    var
    
      long  variable01
      long  variable02
      long  variable03
      long  variable04
      long  variable05
      long  variable06
      long  variable07
      long  variable08
      long  variable09
      long  variable10
    
    

    You can treat this group like an array with:

      i := find_max(@variable01)
    

    The code will iterate through variable01 through variable10 because they will be contiguous in RAM based on the position of their declaration.

    If you want to know what the maximum value found was...

      theMaxVal := long[@variable01][i]
    

    ...where i was returned from the find_max() function.

  • AGCBAGCB Posts: 327

    I've been getting close even before your reply. I've been reading the Propeller manual for 3 hours. Finding a lot things I knew before but forgot by not using them!
    Aaron

  • AGCBAGCB Posts: 327

    Thanks Jon
    Got it working great! Corrected a few of my mistakes and learned again from you.
    Aaron

  • Tracy AllenTracy Allen Posts: 6,658
    edited 2024-01-23 00:10

    This comment from Kuroneko added another brain-teaser to speed up the spin, thread "How to get the Maximum or Minimum value of an array?"
    https://forums.parallax.com/discussion/comment/1061613/#Comment_1061613

  • evanhevanh Posts: 15,392

    I don't understand what Kuroneko wrote at all. Shouldn't it just be this:

    pub maxval2(addr, n)
    
      result := long[addr++]
      repeat n - 1
        result #>= long[addr++]
    
  • No, because that only increments the address by 1 each time. The whacky version indexes into the long-sized local variables, so the increment by 1 increments the effective address by 4. Don't worry, I had to do a double take on that, too.

  • evanhevanh Posts: 15,392

    Oh, ha. I still can't read how his code works. How about this:

    pub maxval2(addr, n)
    
      result := long[addr]
      repeat n - 1
        addr += 4
        result #>= long[addr]
    
  • As an alternative to the original question:

      max_val := register[0] #> register[1] #> register[2] #> register[3] #> register[4] #> register[5] #> register[6] #> register[7] #> register[8] #> register[9]
      idx := lookdownz(max_val: register[0], register[1], register[2], register[3], register[4], register[5], register[6], register[7], register[8], register[9])
    
Sign In or Register to comment.