[RESOLVED] Calculate 'Maximum' value
I'm playing around with a 'general purpose' plotting routine. I have about 20 different sets of data, each with it's own maximum possible value. The data is added to every 2 weeks.
For instance, one set could range from 0 to 600 whilst another could be 0 to 20.
What I would like to do is find the maximum value in a given set and then round it up to, say, in the case of the 0 to 600 series, the nearest 50/100. So if the largest value in week n is 575 I'd like to round it to 600. in the case of a series from 0 to 20, if the largest value is 15.5 I'd like to round it up to 20.
The rounded value will be used to set the maximum Y value on the chart.
I can think of a method, involving repeatedly diving by 10 to find the magnitude but was wondering if anyone has a more elegant solution.
The language is fairly unimportant, although a 'basic' type would be helpful. The implementation will be in Mobile Basic for iPad.
1 Attachment(s)
Re: Calculate 'Maximum' value
Thought it may help if you saw a screen shot. Looking at the Platelets plot the largest value is 552 and I would like then 'ymax' to be 600. I also will need to annotate the y axis with appropriate values. The largest Neuts is 11.4 and I would like to sent 'ymax' to 20 on that graph.
Re: Calculate 'Maximum' value
Since I know PHP best, I wrote the following code in PHP. The theory could easily be implemented in any language.
PHP Code:
$number = 65;
if ($number < 100) {
$round = 10;
} elseif ($number > 100 && $number < 1000) {
$round = 100;
} elseif ($number > 1000 && $number < 10000) {
$round = 1000;
}
echo ceil($number/ $round) * $round;
You can see it in action here... Change the Number variable to see different numbers.
http://sandbox.onlinephpfunctions.co...fa06866fac6cf2
Re: Calculate 'Maximum' value
I had a go at converting the php code into VB.NET.
VB.NET Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim number As Integer, round As Integer
number = 100
If number < 100 Then
round = 10
ElseIf number >= 100 & number < 1000 Then
round = 100
ElseIf number >= 1000 & number <= 10000 Then
round = 1000
End If
MessageBox.Show(Math.Ceiling(number / round) * round)
End Sub
End Class
Re: Calculate 'Maximum' value
Addition to the above code. It should be greater than equal to and lessthan equal to... I got errors if the number was 100, 1000, etc
Re: Calculate 'Maximum' value
Quote:
Originally Posted by
dclamp
Addition to the above code. It should be greater than equal to and lessthan equal to... I got errors if the number was 100, 1000, etc
Ah ok thanks! That is what I had originally but deleted it since you didn't have it in your code.
Re: Calculate 'Maximum' value
Quote:
Originally Posted by
Nightwalker83
Ah ok thanks! That is what I had originally but deleted it since you didn't have it in your code.
Yeah I didnt update the code, But updated in the sandbox thing I was playing with. Also, on the last condition you have <= 1000, should be 10000
Re: Calculate 'Maximum' value
Quote:
Originally Posted by
dclamp
Yeah I didnt update the code, But updated in the sandbox thing I was playing with. Also, on the last condition you have <= 1000, should be 10000
Fixed!
Code:
ElseIf number >= 1000 & number <= 10000 Then
Edit:
I originally tried to convert it to VB6 but none of codes for ceil (that I found) worked correctly.
2 Attachment(s)
Re: Calculate 'Maximum' value
How about something like (VB6):
Code:
Private Sub FindScaleMax()
Dim OrderMag As Single
Dim Mag As Single
OrderMag = Int(Log(Largest) / Log(10)) 'Integer part of Log10(Largest).
Mag = 10 ^ OrderMag
ScaleMax = (Int(Largest / Mag) + 1#) * Mag
End Sub
Re: Calculate 'Maximum' value
Perfect. Thanks Dilettante.
Re: [RESOLVED] Calculate 'Maximum' value
I was a little surprised you didn't get an answer earlier.
This is an example of a class of small problems often used for the performance section of programming job interviews in scientific and engineering companies. They send you to the whiteboard to watch you solve a few such problems. Even if the problem isn't completed, and it generally isn't, attempted solutions using logarithms are "pass" and anything based on iteration is a "fail."
Some say this skews in favor of "conventionality" while the defenders says it evaluates problem solving skills.
Re: [RESOLVED] Calculate 'Maximum' value
I might have come up with the Log solution given enough time. The next interesting problem is using the scale max to create tick marks and labels for the y-axis. When I get stuck no doubt I'll be posting here again.