correctness

Switch statement with returns — code correctness

余生颓废 提交于 2019-11-28 16:36:55
Let's say I have code in C with approximately this structure: switch (something) { case 0: return "blah"; break; case 1: case 4: return "foo"; break; case 2: case 3: return "bar"; break; default: return "foobar"; break; } Now obviously, the "break"s are not necessary for the code to run correctly, but it sort of looks like bad practice if I don't put them there to me. What do you think? Is it fine to remove them? Or would you keep them for increased "correctness"? kgiannakakis Remove the break statements. They aren't needed and perhaps some compilers will issue "Unreachable code" warnings. I

Which compiler is right? 'template' before templated return type needed?

99封情书 提交于 2019-11-28 09:50:21
This snippet (taken from this question ) compiles fine with g++ (as seen), so long the template before the return type is there. In contrast, VC10 does not compile that code with the following error: error C2244: 'A::getAttr' : unable to match function definition to an existing declaration If I remove the template , VC10 is happy but g++ screams this error: error: non-template 'AttributeType' used as template note: use 'A::template AttributeType' to indicate that it is a template Is it again because of VC's broken two-phase look-up or what is the cause? Which compiler is right here? I suspect

Verifying correctness of FFT algorithm

女生的网名这么多〃 提交于 2019-11-28 05:51:02
问题 Today I wrote an algorithm to compute the Fast Fourier Transform from a given array of points representing a discrete function. Now I'm trying to test it to see if it is working. I've tried about a dozen different input sets, and they seem to match up with examples I've found online. However, for my final test, I gave it the input of cos(i / 2), with i from 0 to 31, and I've gotten 3 different results based on which solver I use. My solution seems to be the least accurate: Does this indicate

Counting trailing zeros of numbers resulted from factorial

扶醉桌前 提交于 2019-11-27 13:47:30
I'm trying to count trailing zeros of numbers that are resulted from factorials (meaning that the numbers get quite large). Following code takes a number, compute the factorial of the number, and count the trailing zeros. However, when the number is about as large as 25!, numZeros don't work. public static void main(String[] args) { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); double fact; int answer; try { int number = Integer.parseInt(br.readLine()); fact = factorial(number); answer = numZeros(fact); } catch (NumberFormatException e) { e.printStackTrace(); }

Switch statement with returns — code correctness

♀尐吖头ヾ 提交于 2019-11-27 09:48:48
问题 Let's say I have code in C with approximately this structure: switch (something) { case 0: return "blah"; break; case 1: case 4: return "foo"; break; case 2: case 3: return "bar"; break; default: return "foobar"; break; } Now obviously, the break s are not necessary for the code to run correctly, but it sort of looks like bad practice if I don't put them there to me. What do you think? Is it fine to remove them? Or would you keep them for increased "correctness"? 回答1: Remove the break

Which compiler is right? 'template' before templated return type needed?

送分小仙女□ 提交于 2019-11-27 03:14:36
问题 This snippet (taken from this question) compiles fine with g++ (as seen), so long the template before the return type is there. In contrast, VC10 does not compile that code with the following error: error C2244: 'A::getAttr' : unable to match function definition to an existing declaration If I remove the template , VC10 is happy but g++ screams this error: error: non-template 'AttributeType' used as template note: use 'A::template AttributeType' to indicate that it is a template Is it again

Correctness of Sakamoto's algorithm to find the day of week

血红的双手。 提交于 2019-11-27 02:36:55
I am using Sakamoto's algorithm to find out the day of week from a given date. Can anybody tell me the correctness of this algorithm? I just want this from 2000 to 2099. The algorithm from Wikipedia is given for reference. int dow(int y, int m, int d) { static int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4}; y -= m < 3; return (y + y/4 - y/100 + y/400 + t[m-1] + d) % 7; } Well, you can tell just by looking at it that it is correct... Assuming that the t[] array is correct, which you can verify with just 12 spot checks (one for each month using any day/year). The y -= m < 3 is a nice trick. It

Counting trailing zeros of numbers resulted from factorial

試著忘記壹切 提交于 2019-11-26 16:28:58
问题 I'm trying to count trailing zeros of numbers that are resulted from factorials (meaning that the numbers get quite large). Following code takes a number, compute the factorial of the number, and count the trailing zeros. However, when the number is about as large as 25!, numZeros don't work. public static void main(String[] args) { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); double fact; int answer; try { int number = Integer.parseInt(br.readLine()); fact =

When do I need to specify the JavaScript protocol?

淺唱寂寞╮ 提交于 2019-11-26 14:37:51
I was under the impression that I only need to specify the "protocol" when using JavaScript in URL attributes, such as in hrefs. Is this the only "useful" context for javascript: ? Sensible: <a href="javascript:alert('Hello')">World!</a> Silly: <form onsubmit="javascript:alert('oops!')"> Is this right? Or is there some obscure bug/use case I need to be aware of? The javascript: pseudo-protocol on event handlers will be only ignored, you don't need it, the JavaScript engine will interpret javascript: as a Label Statement . A label simply provides an identifier to a statement, and lets you refer

No-throw VirtualMachineError guarantees

喜你入骨 提交于 2019-11-26 13:47:25
I've come to Java from C++. In the C++ world we pay attention to exception safety, and note that mutators can provide different guarantees in the face of exceptions thrown by the mutator itself or a method it delegates to (minimum, strong, no-throw). Implementing a method that has a strong exception guarantee requires that some basic operations are guaranteed never to throw an exception. The JLS makes statements about which operations can throw which kinds of exceptions, but the VirtualMachineError error presents a problem. Quoth the JLS : an internal error or resource limitation prevents the