error-checking

How do I check if int[] contains only certain numbers?

老子叫甜甜 提交于 2019-12-10 14:22:53
问题 I need to check that an int[] contains only certain values (in this case 0s & 1s) and throw an exception if it doesn't. Is there a more efficient way to do it than either of the following solutions? Simple (but O(n)): for(int n = 0; n < myArray.Length; n++) if(!(myArray[n] == 0 || myArray[n] == 1)) throw new Exception("Array contains invalid values"); Using Where(): if(myArray.Where(n => !(n==1 || n==0)).ToArray().Length > 0) throw new Exception("Array contains invalid values"); 回答1: You can

What algorithm to use to calculate a check digit?

。_饼干妹妹 提交于 2019-12-05 23:42:28
问题 What algorithm to use to calculate a check digit for a list of digits? The length of the list is between 8 and 12 digits. see also: How to generate a verification code/number? 回答1: The Luhn algorithm is good enough for the credit card industry... 回答2: As RichieHindle points out, the Luhn algorithm is pretty good. It will detect (but not correct) any one error or transposition (except a transposition of 0 and 9). You could also consider the algorithm for ISBN check digits, although for old

Any reason to use a run-time assert instead of compile-time assert?

﹥>﹥吖頭↗ 提交于 2019-12-04 09:11:40
While reviewing Visual C++ codebase I found a following strange thing. A run-time assert (which is check the condition and throw an exception if the condition is violated ) was used in a case when the condition could be evaluated at compile time: assert( sizeof( SomeType ) == sizeof( SomeOtherType ) ); clearly the compiler will evaluate the condition and replace the code that will effectively be either assert( true ); which does nothing or assert( false ); which throws an exception every time control passes through that line. IMO a compile-time assert should have be used instead for the

What algorithm to use to calculate a check digit?

混江龙づ霸主 提交于 2019-12-04 03:26:15
What algorithm to use to calculate a check digit for a list of digits? The length of the list is between 8 and 12 digits. see also: How to generate a verification code/number? The Luhn algorithm is good enough for the credit card industry... As RichieHindle points out, the Luhn algorithm is pretty good. It will detect (but not correct) any one error or transposition (except a transposition of 0 and 9). You could also consider the algorithm for ISBN check digits , although for old-style ISBN, the check digit is sometimes "X", which may be a problem for you if you're using integer fields. New

How can I use VBA to ignore green triangle error in range without looping cell by cell?

浪子不回头ぞ 提交于 2019-11-30 23:07:36
I have some large data sets that I am automating and distributing. I want to eliminate the little green triangles that warn the user about numbers stored as text. I have used the following code but it's VERY slow on massive sheets. Range(Cells(1, 1), Cells(lastrow, lColumn)).Select 'kill those dang green cell triagles Dim rngCell As Range, bError As Byte For Each rngCell In Selection.Cells For bError = 3 To 3 Step 1 With rngCell If .Errors(bError).Value Then .Errors(bError).Ignore = True End If End With Next bError Next rngCell As you can see I already cut it down to 1/7th of the time by not

Gray code in .NET

橙三吉。 提交于 2019-11-30 15:29:37
问题 Is there a built in Gray code datatype anywhere in the .NET framework? Or conversion utility between Gray and binary? I could do it myself, but if the wheel has already been invented... 回答1: Use this trick. /* The purpose of this function is to convert an unsigned binary number to reflected binary Gray code. */ unsigned short binaryToGray(unsigned short num) { return (num>>1) ^ num; } A tricky Trick: for up to 2^n bits, you can convert Gray to binary by performing (2^n) - 1 binary-to Gray

Gray code in .NET

心已入冬 提交于 2019-11-30 14:51:04
Is there a built in Gray code datatype anywhere in the .NET framework? Or conversion utility between Gray and binary? I could do it myself, but if the wheel has already been invented... Artelius Use this trick . /* The purpose of this function is to convert an unsigned binary number to reflected binary Gray code. */ unsigned short binaryToGray(unsigned short num) { return (num>>1) ^ num; } A tricky Trick: for up to 2^n bits, you can convert Gray to binary by performing (2^n) - 1 binary-to Gray conversions. All you need is the function above and a 'for' loop. /* The purpose of this function is

What is the canonical way to check for errors using the CUDA runtime API?

我只是一个虾纸丫 提交于 2019-11-25 22:51:02
问题 Looking through the answers and comments on CUDA questions, and in the CUDA tag wiki, I see it is often suggested that the return status of every API call should checked for errors. The API documentation contains functions like cudaGetLastError , cudaPeekAtLastError , and cudaGetErrorString , but what is the best way to put these together to reliably catch and report errors without requiring lots of extra code? 回答1: Probably the best way to check for errors in runtime API code is to define an