I have a CLR project that I am building. The program operates as follows:
Step1: User inputs survey heading, then clicks Calculate.
Step2: User inputs observed headings and enters them into text fields
Step3: User clicks buttons to calculate the difference between calculated headings and observed headings
Step4: Difference in headings are put in new text fields
Ok, so basically we have a compass that is calibrated and we observe headings on a secondary compass relative to the first. The difference between those 2 headings is our final output.
I'm trying to write up an If/Else statement that will evaluate the difference and write the number in a specific color based on whether or not it passes the criteria of a 0.5 tolerance.
Currently I'm using:
varOut1 = (DiffCalc1 - ObsHdgDbl);
if (varOut1 < .5) {
this->Diff1->Font = (gcnew System::Drawing::Font(L"Arial", 8.25F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->Diff1->ForeColor = System::Drawing::Color::SpringGreen;
}
else {
this->Diff1->Font = (gcnew System::Drawing::Font(L"Arial", 9, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->Diff1->ForeColor = System::Drawing::Color::Red;
}
Diff1Final = (varOut1.ToString("n2"));
Diff1->Text = Diff1Final;
So far, this works to an extent... but the problem is that sometimes the Difference is a negative number.
Example: CalculatedHdg = 40.45, ObservedHdg=40.52, Diff1=.07
In this example, the code works because it is within the described statement so the output is in green.
If you have ObservedHdg=41.52 then the output is printed in red...
However if you have ObservedHdg=31.52 then output is still in green even though the overall difference is above .5. The difference can be a Positive or Negative number based on the observed value, so I need a statement that will evaluate if Diff1 is within the tolerance of .5 whether positive or negative.
I'm a little rusty on my If/Else comparisons for math evaluations.
I've attempted searches on advanced If/Else comparisons but mostly I come across examples explaining how to use the && vs || vs <=, etc.
Well, I just simplified the process and used the Abs value of the Difference so that I only need the one comparison.
if (Math::Abs(varOut1) < .5) {
Related
The thinkscript if statement fails to branch as expected in some cases. The following test case can be used to reproduce this bug / defect.
It is shared via Grid containing chart and script
To cut the long story short, a possible workaround in some cases is to use the if-expression which is a function, which may be slower, potentially leading to Script execution timeout in scans.
This fairly nasty bug in thinkscript prevents me from writing some scans and studies the way I need to.
Following is some sample code that shows the problem on a chart.
input price = close;
input smoothPeriods = 20;
def output = Average(price, smoothPeriods);
# Get the current offset from the right edge from BarNumber()
# BarNumber(): The current bar number. On a chart, we can see that the number increases
# from left 1 to number of bars e.g. 140 at the right edge.
def barNumber = BarNumber();
def barCount = HighestAll(barNumber);
# rightOffset: 0 at the right edge, i.e. at the rightmost bar,
# increasing from right to left.
def rightOffset = barCount - barNumber;
# Prepare a lookup table:
def lookup;
if (barNumber == 1) {
lookup = -1;
} else {
lookup = 53;
}
# This script gets the minimum value from data in the offset range between startIndex
# and endIndex. It serves as a functional but not direct replacement for the
# GetMinValueOffset function where a dynamic range is required. Expect it to be slow.
script getMinValueBetween {
input data = low;
input startIndex = 0;
input endIndex = 0;
plot minValue = fold index = startIndex to endIndex with minRunning = Double.POSITIVE_INFINITY do Min(GetValue(data, index), minRunning);
}
# Call this only once at the last bar.
script buildValue {
input lookup = close;
input offsetLast = 0;
# Do an indirect lookup
def lookupPosn = 23;
def indirectLookupPosn = GetValue(lookup, lookupPosn);
# lowAtIndirectLookupPosn is assigned incorrectly. The if statement APPEARS to be executed
# as if indirectLookupPosn was 0 but indirectLookupPosn is NOT 0 so the condition
# for the first branch should be met!
def lowAtIndirectLookupPosn;
if (indirectLookupPosn > offsetLast) {
lowAtIndirectLookupPosn = getMinValueBetween(low, offsetLast, indirectLookupPosn);
} else {
lowAtIndirectLookupPosn = close[offsetLast];
}
plot testResult = lowAtIndirectLookupPosn;
}
plot debugLower;
if (rightOffset == 0) {
debugLower = buildValue(lookup);
} else {
debugLower = 0;
}
declare lower;
To prepare the chart for the stock ADT, please set custom time frame:
10/09/18 to 10/09/19, aggregation period 1 day.
The aim of the script is to find the low value of 4.25 on 08/14/2019.
I DO know that there are various methods to do this in thinkscript such as GetMinValueOffset().
Let us please not discuss alternative methods of achieving the objective to find the low, alternatives for the attached script.
Because I am not asking for help achieving the objective. I am reporting a bug, and I want to know what goes wrong and perhaps how to fix it. In other words, finding the low here is just an example to make the script easier to follow. It could be anything else that one wants a script to compute.
Please let me describe the script.
First it does some smoothing with a moving average. The result is:
def output;
Then the script defines the distance from the right edge so we can work with offsets:
def rightOffset;
Then the script builds a lookup table:
def lookup;
script getMinValueBetween {} is a little function that finds the low between two offset positions, in a dynamic way. It is needed because GetMinValueOffset() does not accept dynamic parameters.
Then we have script buildValue {}
This is where the error occurs. This script is executed at the right edge.
buildValue {} does an indirect lookup as follows:
First it goes into lookup where it finds the value 53 at lookupPosn = 23.
With 53, if finds the low between offset 53 and 0, by calling the script function getMinValueBetween().
It stores the value in def lowAtIndirectLookupPosn;
As you can see, this is very simple indeed - only 38 lines of code!
The problem is, that lowAtIndirectLookupPosn contains the wrong value, as if the wrong branch of the if statement was executed.
plot testResult should put out the low 4.25. Instead it puts out close[offsetLast] which is 6.26.
Quite honestly, this is a disaster because it is impossible to predict which of any if statement in your program will fail or not.
In a limited number of cases, the if-expression can be used instead of the if statement. However the if-expression covers only a subset of use cases and it may execute with lower performance in scans. More importantly,
it defeats the purpose of the if statement in an important case because it supports conditional assignment but not conditional execution. In other words, it executes both branches before assigning one of two values.
lines = ["Pizza", "Vanilla","Los Angeles Pikes","Cookie Washington Tennis Festival","Water Fiesta","Watermelon"]
best= max(set(lines), key=lines.count)
print (best)
The code above returns the greatest occurrence of an element in the list, but in case there is a draw, I want it to return the element with the greatest index. So here I want Watermelon to be printed and if anything is added without a break in the tie the highest index of the draw should be printed.
I need a solution with simple basic code like that seen above and without the importing of libraries. If you could help find a good solution for this it would be really helpful.
You could add the index normalized to a value greater than the length of the array to the result of count. The normalized index will always be less than 1.0, so that it will not affect the first-order comparison, but will guarantee that there are no ties. I would use a small function to do this:
lines = ["Pizza", "Vanilla", "Los Angeles Pikes",
"Cookie Washington Tennis Festival",
"Water Fiesta", "Watermelon"]
def key(x):
return lines.count(x) + lines.index(x) / (len(lines) + 1)
best = max(set(lines), key=key)
print(best)
While your original code returned lines = "Los Angeles Pikes" in my version of Python (because of the way the hashes turned out), the new version returns "Watermelon", as expected.
You can also use a lambda, but I find that a bit harder to read:
best = max(set(lines), key=lambda x: lines.count(x) + lines.index(x) / (len(lines) + 1))
I'm reading values from an excel workbook and I'm trying to stop printing when the value of a specific cell equals a string. Here is my code
import openpyxl
wb = openpyxl.load_workbook('data.xlsx')
for sheet in wb.worksheets:
nrows = sheet.max_row
ncolumns = sheet.max_column
for rowNum in range(20,sheet.max_row):
Sub = sheet.cell(row=rowNum, column=3).value
Unit = sheet.cell(row=rowNum, column=6).value
Concentration = sheet.cell(row=rowNum, column=9).value
com = rowNum[3]
if Sub == "User:":
pass
else:
print(Sub, Concentration, Unit)
The problem is that the if statement doesn't work. When I use type(Sub), python return <type 'unicode'>
Do you have any idea how to do it ?
Thanks
Sounds like you're test is failing. All strings in Excel files are returned as unicode objects but "User:" == u"User:". Maybe the cells you are looking at have some whitespace that isn't visible in your debug statement. In this case it's useful to embed the string in a list when printing it print([Sub])
Alternatively, and this looks to be the case, you are getting confused between Excel's 1-based indexing and Python's zero based indexing. In you code the first cell to be looked at will be C20 (ws.cell(20, 3)) but rowNum[3] is actually D20.
I also recommend you try and avoid using Python's range and the max_column and max_row properties unless you really need them. In your case, ws.get_squared_range() make more sense. Or, in openpyxl 2.4, which allows specify the known edges of a range of cells.
for row in get_squared_range(ws.min_colum, 20, ws.max_column, ws.max_row):
Sub = row[2].value # 3rd item
Unit = row[5].value
Concentration = row[8].value
In openpyxl 2.4 get_squared_range can be replaced:
for row in ws.iter_rows(min_row=20):
I have a LineEdit which I want it to present a float value. I want the float value to have 2 digits precision so I used number function like this:
float tax = value * 0.23;
Qstring strTax = QString::number(tax, 'f', 2);
qDebug() << strTax;
ui->leTax->setText(strTax);
The thing is that while in console the value is printed with 2 digits precision, the widget prints all the decimal digits which might be 3 or more (depends on the value). Is there a way to fix it? I am using Qt 5.0.
So this is the accepted answer. I finally solved my problem. The onTextUpdate had to update two more LineEdits one containing the Tax and one containing The total amount. But the one containing the totalAmount also emitted the onTextChanged to update the net value and the Tax LineEdits, without rounding the values(I Was careless!!). So I corrected the totalAmount's onTextChanged. I also updated it to check if it has focus so to know if it is its turn to update the other LineEdits or not :). The point was that someone could edit the netValue line edit and that would update the tax and total amount or someone would enter the totalAmount and that automatically would update the net Amount and tax field. Everything working now. Thank you all for answering!!
I do not have a float-version for QString::number(), so maybe try cast-to-double
ui->leTax->setText(QString::number((double)tax, 'f', 2));
But since qDebug() is already showing the correct value, you probably change your strTax somewhere else in the code.
setValidator might be your solution. Click here and here
void QLineEdit::setValidator ( const QValidator * v )
Sets this line edit to only accept input that the validator, v, will accept. This allows you to place any arbitrary constraints on the text which may be entered.
Also, Set the number of decimal places with setDecimals().
So, it should looks like this :
// bottom (-999.0), top (999.0), decimals (2)
lineEdit->setValidator(new QDoubleValidator(-999.0, 999.0, 2, lineEdit));
I am using Microsoft's DataVisualization.Charting.Chart, and I have integer values along the X axis, using line-style graphs. However, the chart is adding an extra blank item at the beginning and end of the x-axis, with no value labels to explain what they are.
How can I remove these empty items and make the lines go right up to the ends?
Use the IsMarginVisible property of the xaxis. I believe that will do the trick.
To test this, I changed one of my own charts to be a line chart and then set the value in the code:
ReactivityChart.ChartAreas(0).AxisX.IsMarginVisible = False
Tell me if this is what you were hoping to get or if I have totally misunderstood the question:
(note that I do not have a high enough rep to post this image)
http://www.rectorsquid.com/chartmargintest.gif
You should set the Maximum and Minimum properties in ChartArea.AxisX, e.g. :
this.chart1.ChartAreas[0].AxisX.Minimum = 0; // if your minimum X = 0
this.chart1.ChartAreas[0].AxisX.Maximum = 100; // if your maximum X = 100
In this way, your chart area will show only the values between Minimum and Maximum.