Is it considered better practice when comparing integer values to use the pattern:
if(i > n - 1)
or
if(i >= n)
In my experience with C, C++, and Python the former case seems to be more heavily used. Is there a reason why the >= and <= operators customarily avoided with integers?
Using >= avoids the possibility of (n-1) wrapping around to a large positive value if one is dealing with unsigned integers, so I would find that preferable.
Related
I have a table with latitudes and longitudes of locations. I was using the BETWEEN clause successfully until I reached cases where the values being searched for where the same as those in the database. In these cases it is not returning results. Here is an example, where as:
SELECT
`Location`.`latitude`,
`Location`.`longitude`
FROM
`locations` AS `Location`
WHERE `latitude` >= 40.735619
AND `latitude` <= 40.736561
AND `longitude` >= -74.033882
AND `longitude` <= -74.030861;
Returns:
"latitude" "longitude"
"40.736561" "-74.033882"
"40.735619" "-74.030861"
If I use the BETWEEN CLAUSE (Notice I've even tried this):
SELECT
`Location`.`latitude`,
`Location`.`longitude`
FROM
`locations` AS `Location`
WHERE `latitude` BETWEEN LEAST(40.735619, 40.736561)
AND GREATEST(40.736561, 40.735619)
AND `longitude` BETWEEN LEAST(- 74.033882, - 74.030861)
AND GREATEST(- 74.030861, - 74.033882)
I get 0 results. Oh, whats more, if I add and/or subtract 0.000001 to each value Ex. "BETWEEN (40.735619-0.00001)" etc. If I do this it does return the two results.
Fine, I'll use >= and <= but what I don't understand is why BETWEEN is acting like > and < when in the docs its pretty clear:
If expr is greater than or equal to min and expr is less than or equal to max, BETWEEN returns 1
You should use a decimal data type rather than a float. Equality, and hence between, for floating point values is imprecise
I suspect that this has to do with roundoff errors in floating point conversions. According to the docs, The expression expr BETWEEN min AND max is equivalent to (expr <= max AND expr >= min) only when all three arguments are the same type. Otherwise type conversion is applied to all arguments. It would be during this conversion that roundoff errors would occur.
I came across a mysql query that looks like this:
SELECT
SUM(some_amount*(some_field!=90)*(some_date < '2011-04-22'))
, SUM(some_amount*(some_field =90)*(some_date < '2011-04-22')*(another_field IS NULL))
FROM
some_table
What does the * mean in the select statement in this case?
Looks like CAST() is not necessary for boolean-to-integer conversions. Multiplication is used to convert the sum to 0 for unwanted rows (using the fact that boolean true can be cast to 1 and false to 0):
some_amount*(some_field!=90)*(some_date < '2011-04-22')
if some_field == 90 or some_date >= '2011-04-22', the corresponding term will evaluate to 0, thereby converting the entire expression to 0.
It is a multiplication operation.
example 2*3=6
It's a standard multiplication operator,
select 2 * 2
= 4
:)
SELECT COUNT(*) FROM planets
WHERE ROUND(SQRT(POWER(('71' - coords_x), 2) +
POWER(('97' - coords_y), 2))) <= 17
==> 51
SELECT COUNT(*) FROM planets
WHERE ROUND(SQRT(POWER((71 - coords_x), 2) +
POWER((97 - coords_y), 2))) <= 17
==> 22
coords_x and coords_y are both TINYINT fields containing values in the range [1, 100]. Usually MySQL doesn't care if numbers are quoted or not.. but apparently it does in this case. The question is: Why?
I am a bit rusty on the inerds of MySql but <= on string goes to lexicographical sorting instead of numeric ie, '150' < '17'.
The implicit conversion from string to floating point number is probably causing in inaccurate results. See: Type Conversion in Expression Evaluation
If a price in a row is 38.03, then the following search restrictions should all return the row containg the result.
WHERE price >= '38.02' AND price <= '38.03' (This works)
WHERE price >= '20' AND price <= '100' (This works)
WHERE price >= '38.03' AND price <= '38.03' (This doesn't work)
WHERE price >= '38.03' AND price <= '100' (This doesn't work)
WHERE price >= '38.03' (This doesn't work)
WHERE price <= '38.03' (This works)
The price is stored as a float in the DB.
So basically, <= is working whereas >= is not. Is there a reason why that could be?
keep in mind that float is a flawed data type when it comes to precision. If you represent 12 as float, you will get 11.99999999999998 or something.
'38.03' can be converted to decimal, or other data type that is more precise (depending on RDBMS, I am being general here), and it will differ from the float value.
float is 32 bit, low precision. Double works a lot better, being 64 bit data type. Decimal data type in some systems are 128 bit numeric data types for storing very precise numeric values, and is usually used for denominating money.
And, skip the habit of comparing using the = operator, of float values. Floats are used for approximate and fast calculations, and only comparison with a range is acceptable for checking the value of a float. That's valid for basically every single system.
When you use quotes (') your variables will be treated as strings. I think thats your problem.
Try: WHERE price >= 38.03 AND price <= 38.03
How would you write SO's Popularity algorithm in MySQL?
The algorithm is detailed here: Popularity algorithm.
thanks!
It's relatively simple.
t = (time of entry post) - (Dec 8, 2005)
You would convert the date values to timestamps (you can use unix_timestamp), which gives you an integer that can be used in the rest of the comparisons.
x = upvotes - downvotes
This one should be pretty easy... obviously MySQL supports subtraction.
y = {1 if x > 0, 0 if x = 0, -1 if x < 0)
z = {1 if x < 0, otherwise x}
For these, take a look at MySQL's case statement.
log(z) + (y * t)/45000
MySQL has a log function, so this one should be easy too, just simple math.
And, you tie it all together with a select statement. You can store intermediate calculations in your select statement using user-defined variables. For example:
select #x := (upvotes - downvotes) as x,
(#x > 4) as isXGreaterThanFour