Why data filtering does not work correctly? - flutter

I am getting data from server and I want to filter it by data. I do it with if. But I ran into a problem, I get price data and compare it with my price data, but in the end they are wrong, for some reason the check does not pass, could you tell me what is the reason why the numbers are not compared correctly if I have an <= operator?
List speed = [
7.4,
11,
22,
];
List<double> price = [
0.20,
0.25,
0.30,
0.35,
0.40,
0.45,
0.50,
];
if ((w.power.toInt() == speed[filters.minSpeed]) &&
(double.parse(w.formattedPrice.substring(1)) <=
price[filters.maxPrice]) ||
(i.public == filters.ownershipStationPublic ||
filters.ownershipStationAll)) {
log(double.parse(w.formattedPrice.substring(1)).toString());
log('My price: ${price[filters.maxPrice].toString()}');
filteredStations.add(i);
}

Even if the price is higher, the if statement is also true if either i.public == filters.ownershipStationPublic or filters.ownershipStationAll are true. Maybe this is happening here. Maybe you wanted that first || to be &&

Related

SSRS - Conditional formatting not getting applied?

I have the following expression to apply background colour to a text box but only the red colour is getting applied when that condition is true. All the other conditions are showing up as white? For example when the first condition is true when the report renders, the background colour is white instead of green?
=IIF(Round(SUM(CInt(Fields!TotalAchieved.Value) * 7.14)) >= 86, "Green",
IIF(Round(SUM(CInt(Fields!TotalAchieved.Value) * 7.14)) >= 79 AND
Round(SUM(CInt(Fields!TotalAchieved.Value) * 7.14)) <= 85, "Light Green",
IIF(Round(SUM(CInt(Fields!TotalAchieved.Value) * 7.14)) >= 64 AND
Round(SUM(CInt(Fields!TotalAchieved.Value) * 7.14)) <= 78, "Yellow", "Red" )))
It would seem that your SUM(CINT(Fields!TotalAchieved.Value) * 7.14) calculation is not giving you the results you expect . The first thing I would do is add a column that shows this value to make sure that it's gives you what you expect.
Once you have that correct then I would also suggest that you use a SWITCH statement rather than nested IIFs, they are much easier to read/debug.
You expression would be
=SWITCH(
Round(SUM(CInt(Fields!TotalAchieved.Value) * 7.14)) >= 86, "Green",
Round(SUM(CInt(Fields!TotalAchieved.Value) * 7.14)) >= 79, "LightGreen",
Round(SUM(CInt(Fields!TotalAchieved.Value) * 7.14)) >= 64, "Yellow",
TRUE, "Red"
)
This way you don't need to check for ranges as, for example, if the value was 75, the 1st expression fails but the second one is true so SWITCH will stop at that point and not evaluate the rest, if all fail then the final TRUE will act like an else.

How do I fix the error "Result of operator '>=' is unused"

I have the error mentioned above for the two lines that are commented below, using an equals sign works, but when I use the '>=' sign... The error continues to show up. It's not an error with my pause function, I've tested it with the equals sign, so could you please tell me what to fix. Thanks!
func addScore(playerWhoWon : SKSpriteNode){
ball.position = CGPoint(x: 0, y: 0)
ball.physicsBody?.velocity = CGVector(dx: 0, dy: 0)
if playerWhoWon == main {
score[0] >= 10 // THAT"S WHERE I GET MY ERROR
pauseGame()
ball.physicsBody?.applyImpulse(CGVector(dx: 10, dy: 10))
}
else if playerWhoWon == enemy {
score[1] >= 10 // SECOND LINE WHERE I GET MY ERROR
pauseGame()
ball.physicsBody?.applyImpulse(CGVector(dx: -10, dy: -10))
}
topLbl.text = "\(score[1])"
btmLbl.text = "\(score[0])"
}
Based on your previous question, what you should be doing is creating an if statement:
if score[0] >= 10 {
pauseGame()
}
I also strongly urge you to spend lots of time reading the Swift Programming Language book from Apple.
Using an equal sign (=) means assignment. When you say: score[0] = 10 it means that you assign 10 to the first element of the array score. This is why it works.
Using greater than or equals sign (>=) means comparison, so it returns a boolean value as the result of the comparison. And you are not doing anything with that result, so this is why you are getting the warning.
When you use something that returns a result and you don't want to use that result, you can use:
_ = score[0] >= 10
You shouldn't do this in your case for sure, and as mentioned by #rmaddy, you should be using an if statement to fix your issue.

ThinkinSphinx query not working with sphinx_select with four conditions

I'm trying to use ThinkingSphinx to return records that have a start date within a range OR an end date within the same range, basically any record that starts or ends within this range.
To do this, I am using a computed attribute and sphinx_select as per the documentation in combination with what this post suggests for date ranges, as follows (assume there are two records, record_a starts outside the range, but ends within the range and record_b starts and ends within the range):
with_display = "*, IF(start_at >= #{range_start.to_i}, 1, 0) + " +
"IF(start_at <= #{range_end.to_i}, 1, 0) + " +
"IF(end_at >= #{range_start.to_i}, 10, 0) + " +
"IF(end_at <= #{range_end.to_i}, 10, 0) AS display"
{
sphinx_select: with_display,
with: {'display' => [2, 20, 22]},
}
=> [record_b]
However, if I only use the start_at conditions, I get one record, and if I use only the end_at conditions, it returns both records.
with_display = "*, IF(start_at >= #{range_start.to_i}, 1, 0) + " +
"IF(start_at <= #{range_end.to_i}, 1, 0) AS display"
=> [record_b]
with_display = "*, IF(end_at >= #{range_start.to_i}, 10, 0) + " +
"IF(end_at <= #{range_end.to_i}, 10, 0) AS display"
=> [record_a, record_b]
If I'm understanding this correctly, having all four conditions, should result in both record_a and record_b being returned, since record_a should have a display value of 20, while record_b should have a display value of 22.
Am I missing something?
I just realized my math was wrong, given the cases I want to handle:
record_a will have a display of 21
record_b will have a display of 22
What I needed to do was change my array to:
{
sphinx_select: with_display,
with: {'display' => [12, 21, 22]},
}
in order the handle the cases of records that end within the range (21), records that start within range (12), and records than start and end within the range (22)

Objective-C If Statement Comparing Floats not working

This is literally the weirdest thing I have ever encountered. So, I have a float called ratingDecimal, and I use some conditions to compare it. Here is what the conditions look like:
if (ratingDecimal >= 0.0) {
if (ratingDecimal < 0.3 && ratingDecimal != 0.3) {
ratingDecimal = 0.0;
NSLog(#"bigger or equal to 0 and smaller than 0.3");
}
}
if (ratingDecimal >= 0.3) {
if (ratingDecimal < 0.8 && ratingDecimal != 0.8) {
ratingDecimal = 0.5;
NSLog(#"middle");
}
}
if (ratingDecimal >= 0.8) {
NSLog(#"bigger or equal to 0.8");
ratingDecimal = 1.0;
}
Here is a weird problem. I have set the ratingDecimal to 0.8, the console logs ratingDecimal as:
0.8
but calls:
middle
Which should only be called if ratingDecimal is bigger or equal to 0.3 but smaller than 0.8 and DOES NOT equal to 0.8.
The statement that should be getting called is:
if (ratingDecimal >= 0.8) {
NSLog(#"bigger or equal to 0.8");
ratingDecimal = 1.0;
}
But I do not see
bigger or equal to 0.8
You are probably wondering why my conditions are so tedious and complex, they can be as simple as:
if (ratingDecimal >= 0.3 < 0.8)
They used to be like that but since this has never worked I kept on breaking the statement down and it is STILL acting weird.
Why is this happening!?
Here is an image example:
Your ratingDecimal value is probably something like 0.7999999999999. When printed to the console, something is probably rounding it to 0.8, but the comparison still sees it as less than 0.8.
This is due to the binary nature of floating point numbers. See What Every Computer Scientist Should Know About Floating-Point Arithmetic for extensive information on this.
#Greg gave a good answer. I'd like to point out that you can write that code in a much clearer and simpler fashion:
if (ratingDecimal >= 0.0) {
if (ratingDecimal < 0.3) {
ratingDecimal = 0.0;
NSLog(#"bigger or equal to 0 and smaller than 0.3");
} else if (ratingDecimal < 0.8) {
ratingDecimal = 0.5;
NSLog(#"middle");
} else {
ratingDecimal = 1.0;
NSLog(#"bigger or equal to 0.8");
}
} else {
// negative
}
As an FYI - a line such as:
if (ratingDecimal < 0.3 && ratingDecimal != 0.3) {
doesn't make such sense. If a number is less than 0.3 then it must be unequal to 0.3 so the second check doesn't do anything useful.
you can use functions like roundf() according to your need.

Range inside switch case statement in Coffeescript

I am using Handlebar in my Rails 3.2 jquery mobile application.
I am trying to write a switch case statement inside a Coffeescript method like
Handlebars.registerHelper 'status', (blog) ->
switch(parseInt(blog.status))
when [0..20]
status = "active"
when [20..40]
status = "Moderately Active"
when [40..60]
status = "Very Active"
when [60..100]
status = "Hyper Active"
return status
I am not getting any result . How to use range in when . Please suggest
Your switch won't work as Cygal notes in the comments (i.e. see issue 1383). A switch is just a glorified if(a == b) construct and you need to be able to say things like:
a = [1,2,3]
switch a
...
and have it work when you switch on an array. The CoffeeScript designers thought adding a (fragile) special case to handle arrays (which is all [a..b] is) specially wasn't worth it.
You can do it with an if:
Handlebars.registerHelper 'status', (blog) ->
status = parseInt(blog.status, 10)
if 0 <= status <= 20
'Active'
else if 20 < status <= 40
'Moderately Active'
else if 40 < status <= 60
'Very Active'
else if 60 < status <= 100
'Hyper Active'
else
# You need to figure out what to say here
Or with short circuiting returns like this:
Handlebars.registerHelper 'status', (blog) ->
status = parseInt(blog.status, 10)
return 'Something...' if status <= 0
return 'Active' if status <= 20
return 'Moderately Active' if status <= 40
return 'Very Active' if status <= 60
return 'Hyper Active' if status <= 100
return 'Something else' # This return isn't necessary but I like the symmetry
Note that you have three special cases that you need to add strings for:
status < 0.
status > 100.
status is NaN. This case would usually fall under the final "it isn't less than or equal to 100" branch since NaN => n and NaN <= n are both false for all n.
Yes, you're absolutely certain that the status will always fall within the assumed range. On the other hand, the impossible happens all the time software (hence the comp.risks mailing list) and there's no good reason to leave holes that are so easily filled.
Also note the addition of the radix argument to the parseInt call, you wouldn't want a leading zero to make a mess of things. Yes, the radix argument is optional but it really shouldn't be and your fingers should automatically add the , 10 to every parseInt call you make.
Adding a tiny bit to mu is too short's answer, you can transform its second code snippet into a switch expression:
Handlebars.registerHelper 'status', (blog) ->
status = parseInt(blog.status, 10)
switch
when status <= 0 then 'Something...'
when status <= 20 then 'Active'
when status <= 40 then 'Moderately Active'
when status <= 60 then 'Very Active'
when status <= 100 then 'Hyper Active'
else 'Something else'
This is basically equivalent to doing a switch (true) in JavaScript (though the CS compiler will generate a switch (false) statement with the negated conditions to ensure boolean results from the expressions... i think).
And the reason why the switch over ranges doesn't work is that ranges literals in CS represent plain old JS arrays (though the compiler will do some optimization tricks when doing something like for i in [1..something]), so when they are found inside a switch they are treated just like normal array values:
// Generated JS for question's CS code:
switch (parseInt(blog.status)) {
case [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]:
status = "active";
break;
case [20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40]:
status = "Moderately Active";
break;
case [40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60]:
status = "Very Active";
break;
case (function() {
_results = [];
for (_i = 60; _i <= 100; _i++){ _results.push(_i); }
return _results;
}).apply(this):
status = "Hyper Active";
}
The value inside the switch statement is basically compared to each case value using ===, which only works for primitives, not for arrays (and even if it worked for arrays, it would be testing array equality, not if the switched value is contained in the caseed arrays).