comparison

Most advisable way of checking empty strings in C#

ぐ巨炮叔叔 提交于 2020-01-09 12:41:48
问题 What is the best way for checking empty strings (I'm not asking about initializing!) in C# when considering code performance ?(see code below) string a; // some code here....... if(a == string.Empty) or if(string.IsNullOrEmpty(a)) or if(a == "") any help would be appreciated. :) 回答1: Do not compare strings to String.Empty or "" to check for empty strings. Instead, compare by using String.Length == 0 The difference between string.Empty and "" is very small. String.Empty will not create any

Pattern matching variables in a case statement in Haskell

谁说胖子不能爱 提交于 2020-01-09 07:43:07
问题 If I compare a string literal to a string literal using the case statement, I get the expected behavior: if they are the same - it matches, if they are not - it does not. However, if I compare a string literal to a constant that is a string, I get "Pattern matches are overlapped" warning and the branch with the constant always matches. Here's an example session: Prelude> let var1 = "abc" Prelude> let var2 = "def" Prelude> case var1 of { var2 -> "Fail"; _ -> "Win" } <interactive>:1:0: Warning:

Should we compare floating point numbers for equality against a *relative* error?

吃可爱长大的小学妹 提交于 2020-01-09 02:21:26
问题 So far I've seen many posts dealing with equality of floating point numbers. The standard answer to a question like "how should we decide if x and y are equal?" is abs(x - y) < epsilon where epsilon is a fixed , small constant. This is because the "operands" x and y are often the results of some computation where a rounding error is involved, hence the standard equality operator == is not what we mean, and what we should really ask is whether x and y are close , not equal. Now, I feel that if

Should we compare floating point numbers for equality against a *relative* error?

北慕城南 提交于 2020-01-09 02:21:13
问题 So far I've seen many posts dealing with equality of floating point numbers. The standard answer to a question like "how should we decide if x and y are equal?" is abs(x - y) < epsilon where epsilon is a fixed , small constant. This is because the "operands" x and y are often the results of some computation where a rounding error is involved, hence the standard equality operator == is not what we mean, and what we should really ask is whether x and y are close , not equal. Now, I feel that if

C# - Prettier way to compare one value against multiple values in a single line of code [duplicate]

*爱你&永不变心* 提交于 2020-01-07 09:38:12
问题 This question already has answers here : Multiple string comparison with C# (7 answers) Closed last year . I have this piece of code: if (filter != RECENT && filter != TODAY && filter != WEEK && filter != MONTH && filter != ALLTIME) { filter = RECENT; } Note that filter is a string and it's compared against const string types. Is there any way to do this inline and have it be more readable? Doing it with the ternary operator doesn't make it much better since I still have to repeat filter !=

Filtering an array based on a second array?

混江龙づ霸主 提交于 2020-01-06 19:34:43
问题 I have two data arrays: $arrnames=('a','b','c') $arrtypes=('x','y','z') and I have a lookup array: $arrlookup=('y','z') I need to return the elements in $arrnames where the matching element in $arrtypes is contained in $arrlookup . I've been playing with foreach looping over $arrnames and looking at the same element in $arrtypes by index but it's getting sloppy and I know there has to be a better way. I've seen some similar questions, but not anything that strikes me as specifically answering

How to compare two dictionary key values and print text in cell in iOS?

做~自己de王妃 提交于 2020-01-06 18:30:33
问题 I have two dictionary with multiple items. I want to compare values. Dict1:{ "Displacement_trim" = "49.26 "; "Dry_Weight" = "<null>"; } Dict2:{ "Displacement_trim" = "171.20 "; "Dry_Weight" = "<null>"; } I want to know which "Displacement_trim" is greater. Also checking null values. Print the data in cell. How can I achieve this? 回答1: To find out which value from the dict is bigger use the following code: Swift 3 let dict1 = [ "Displacement_trim" : "49.26", "Dry_Weight" : "<null>" ] let dict2

How to test multiple variables against a value?

偶尔善良 提交于 2020-01-06 13:05:41
问题 I'm trying to make a function that will compare multiple variables to an integer and output a string of three letters. I was wondering if there was a way to translate this into Python. So say: x = 0 y = 1 z = 3 mylist = [] if x or y or z == 0 : mylist.append("c") if x or y or z == 1 : mylist.append("d") if x or y or z == 2 : mylist.append("e") if x or y or z == 3 : mylist.append("f") which would return a list of ["c", "d", "f"] Is something like this possible? 回答1: You misunderstand how

How to test multiple variables against a value?

霸气de小男生 提交于 2020-01-06 13:04:49
问题 I'm trying to make a function that will compare multiple variables to an integer and output a string of three letters. I was wondering if there was a way to translate this into Python. So say: x = 0 y = 1 z = 3 mylist = [] if x or y or z == 0 : mylist.append("c") if x or y or z == 1 : mylist.append("d") if x or y or z == 2 : mylist.append("e") if x or y or z == 3 : mylist.append("f") which would return a list of ["c", "d", "f"] Is something like this possible? 回答1: You misunderstand how

SQL Different column type comparison error

五迷三道 提交于 2020-01-06 08:09:48
问题 Hi I have a stored procedure which suppose to compare between 2 columns on different tables Users.ID => int Trans.ID => nvarchar Sometimes the value in Trans.ID is not numeric at all, and sometimes it's null, which causes an error while trying to compare is there a way to try to parse Trans.ID into a number and in case it doesn't succeed to return 0?? I tried NullIf() but it doesn't work when the value inside is not numeric. 回答1: you can do something like: select * from users u inner join