math functions

jeffnooch

Member
Licensed User
Longtime User
are there there any functions or libraries that do the equivalent of math.min and math.max?
Thanks
 

mc73

Well-Known Member
Licensed User
Longtime User
if it's about two variables, you may use max(nr1,nr2) and min(nr1,nr2).
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Below is the code I made to extract the min and max of an array of numbers: It is tested to work.

B4X:
Dim z , y As Double
Dim x() As Double
x=Array As Double(102,61,87.5,111.6,54,43,236.3)
Dim MyList As List
MyList.Initialize
MyList.AddAll(x)
MyList.Sort(True)
y= MyList.Get(0)   'get the smallest
z=MyList.Get(MyList.Size-1)    'get the largest
Msgbox("Min: " & y & CRLF & "Max: " & z,"Min and max")
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Below is the code that gives you the min and max of an array of numbers. Also, it gives you the indices of the min and max number in the original list. I tested it:
B4X:
Dim z , y As Double
Dim x() As Double
x=Array As Double(102,61,87.5,111.6,54,43,236.3)
Dim MyOrigList,MySortList As List
MyOrigList.Initialize
MyOrigList.AddAll(x)
MySortList.Initialize
MySortList.AddAll(x)
MySortList.Sort(True)
y= MySortList.Get(0)   'get the smallest
z=MySortList.Get(MySortList.Size-1)    'get the largest
Msgbox("Min: " & y & CRLF & "Max: " & z,"Min and max")
Dim idxMin, idxMax As Int
idxMin=MyOrigList.IndexOf(y)
idxMax=MyOrigList.IndexOf(z)
Msgbox("idx of Min: " & idxMin & CRLF & "idx of Max: " & idxMax,"Index of Min and max")
 
Upvote 0

positrom2

Active Member
Licensed User
Longtime User
Thanks, Mahares,
I will test your code tomorrow on my array(256) to see if that sorting is faster than going over each member (for i=1 to 256, if ...).
Regards, positrom2
 
Upvote 0

positrom2

Active Member
Licensed User
Longtime User
The array to be sorted is two-dimensional, i.e. I have array(0...255,2), and I wish to find max and min of array(0...255,k), for k either 1 or 0. Is it possible to pass array(...,k) to the sort function?
Thanks, positrom2
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Could you please write down a small sample of what the array looks like and show how you want the numbers sorted in some detail. I am sure someone in the forum can figure it out.
 
Upvote 0

positrom2

Active Member
Licensed User
Longtime User
Sort function for 2-dimensional array?

I have the follwing:
A two-dimensional array.
Dim array(256,2)
where array(256,0) and array(256,1) represent two different data sets.

I want to find max and min values and the indices of each data set. Of course, I know the brutal force method testing individually each array component for a fixed second index (array(i,0) and array(i,1)). But for a 1-dimensional array the sort function seems to be more elegant as pointed out in previous posts (if it's faster I still have to find out). The question is how to pass each of the data sets (array(1...256,0) and array(1...256,0) separately to the sort function, conventiently...

In a different computer language I am used to pass just parts of arrays. For example I would use max(array[*,0]) to get the maximum of the first data set. The star refers to the whole first dimension (also, e.g., array[4:66,0] would be possible). Is there a similar way in B4A? Or do I have to first copy each data set (aray(0...255,0) and array(0...255,1)) to a 1-dimensional array before using the sort function?
Thanks, positrom2
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
Since I may suppose that you have a routine to populate your two arrays, why not asking for the max/min while inside the populating loop?

For e.g.,

B4X:
dim minVal(2),maxVal(2)
for k=0 to 255
     for l=0 to 1
          array(k,l)=someNumber
          if k=0 then
               minVal(l)=someNumber:maxVal(l)=someNumber
          end if
          minVal(l)=min(minVal(l),someNumber)
          maxVal(l)=max(maxVal(l),someNumber)
     next
next
 
Upvote 0

positrom2

Active Member
Licensed User
Longtime User
Yes, good idea. Populating is being done by using somenumber=buffer.readline from BT in a for-next loop (textreader), trying to retrieve the data as quick as possible in order not to bother the sending side more than necessary... I was having lot of trouble with the data transmission, and loading the readline loop with more instructions seemed to make it worse. At the time being the transmission became quite stable (probably due to evolution on both sides of the transmission "line") and I will give your suggestion a try.
Thanks, positrom2
 
Upvote 0
Top