Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I'm working on a software and, at some point in the program, a double becomes NaN which destroys the entire program.
Can I get some help on how to debug a problem like this and how to find the actual line where a double is accidentally set to NaN?
Narrowing down where a NaN occurs can be quite difficult sometimes in a context doing very complex work. The author of Physically-Based Rendering documents a tricky case, for example, where his raytracer was slowed down to a crawl as a result of some expressions evaluating to NaN which caused an excessive amount of computation subsequently. These things can be quite tricky to spot, especially if it's an edge case that only occurs one in a million times.
A handy trick is to rely on the IEEE standard where a variable compared to itself will return false if its value is NaN. This may not work on all compilers so you may want to make sure it does before you sprinkle assertions, but...
boolean is_nan(double val)
{
return val != val;
}
In Java we don't need this trick, however. We already have isNan in java.lang.Double.
With this handy, you can narrow down where a NaN occurs through a process of elimination by doing sanity checks like:
double val = ...;
// after various arithmetical operations
assert !Double.isNan(val);
You can then work your way down (up?) and narrow down exactly what line of code is producing a NaN by adding more granular checks of this sort whenever you hit an assertion failure, taking note of which line of code in which the assertion fails. In very complex scenarios like the raytracer scenario cited where this might only occur one in a million times, this can be a lot quicker than trying to trace everything through a debugger.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
Improve this question
for a project of mine i need to detect the current incoming megabytes per second on linux coded in python.
i found someone elses code that does the correct thing but its coded in java and i dont exactly understand it. any pointers?
TOTAL_INCOMING_BYTES_FILE = "/sys/class/net/%s/statistics/rx_bytes",
final double current_mbps =
((current_total_bytes - Long.parseLong(Files.readAllLines(Paths.get(new File(String.format(TOTAL_INCOMING_BYTES_FILE, ETH_INTERFACE)).toURI())).get(0))) / 125000) * (-1);
i found someone elses code that does the correct thing but its coded in java and i dont exactly understand it.
Indeed Long.parseLong(Files.readAllLines(Paths.get(new File(String.format(TOTAL_INCOMING_BYTES_FILE, ETH_INTERFACE)).toURI())).get(0)) is an incredibly convoluted way to read a number from a file, and to take the abstruseness a bit further the fraction of a difference is multiplied by −1 instead of exchanging the subtrahend and the minuend.
With the constant 125000 the expression computes the current_mbps if current_total_bytes has been fetched one eighth of a second ago (since 125000 is one eighth of a million). The (re-)initialization of current_total_bytes is missing from the code.
Here's a Python snippet for the computation of e. g. the eth0 rate:
import time
earlier_total_bytes = None
while 0 <= (current_total_bytes := int(open("/sys/class/net/eth0/statistics/rx_bytes").read())) \
and earlier_total_bytes is None: earlier_total_bytes = current_total_bytes; time.sleep(.125)
current_mbps = (current_total_bytes - earlier_total_bytes) / 125000
Of course this can be adapted for other sampling intervals, also repeated and variable.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I've been wondering this for a while, and thought I'd pose the question today.
Example code:
private void createLinks(int numUsers) {
for (int i = 1; i <= numUsers; i++) {
String userId = "ID-" + i;
String randomId;
// Generate an ID to link to.
do {
randomId = "ID-" + random.nextInt(i);
} while (randomId.equals(iUserId));
link(userId, randomId); //links the first argument to the second,
//links are not bi-directional.
// Generate 4 more ID's to link to.
for (int j = 1; j <= 4; j++) {
do {
randomId = "ID-" + random.nextInt(i);
} while (randomId.equals(iUserId));
link(userId, randomId);
link(randomId, userId);
}
// Generate another ID to link
do {
randomId = "ID-" + random.nextInt(i);
} while (randomId.equals(iUserId));
link(randomId, userId)
}
}
#createLinks is invoked a lot, and the do...while code snippet is being repeated in the method. Does it make sense to extract these 3 lines of code out to a method called generateRandomId(int i) and incur the function overhead to avoid this repetition? If createLinks gets invoked a 100 times, generateRandomId would get invoked 100*6 = 600 times.
This is more a language agnostic question rather than one specific to java, but it'd be interesting to know if some languages handle function overhead better than others. E.g. JVM does function inlining to optimize function calls, which might mean that a developer need not wonder about things that I mentioned above.
This is definitely opinion-based question, and I expect it will be closed. But I'll try to answer it anyway, because it's quite frequently asked.
If you want simple answer – don't bother about it. It's probably too soon. Really, the manner you ask a question tells me that you have a lack of information about how frequently this code will be called and how slow it really is. And it's ok. We all face this situation when there are just a lot of unknowns in the context of development. The trick is – those unknown will become knowns in operation context (when your code is actually running). You'll get a feedback about performance issues if any. It should be said, getting this feedback is not so simple task by itself and requires some skills and mature toolchain. But it's not the question you asked.
Does I advocate skip any performance optimization while developing? Of course no, it's silly. There are issues which could and should be solved early. I'm just advising to follow simple and straightforward principle:
If you're in doubt – wait for reality to show you the right way.
This principle could be misused as any other. But I hope you get my point – premature optimization is the root of all evil, right?
My opinionated answer is "always." Whenever I find myself writing the same code twice, I make it a function.
The point where this practice ceases to be opinion-based is when two pieces of code doing exactly the same thing is important to the proper operation of the program.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
If rounding is required what exactly happens? I was looking in the documentation and it says it just throws an exception, yet in my application it seems to be rounding with half up.
RoundingMode.UNNECESSARY mandates that a BigDecimal does not need to be rounded to fit the scale specified by it.
Here's a sample:
System.out.println(new BigDecimal("1.1").setScale(1, RoundingMode.UNNECESSARY));
1.1 is an exact result, and when the new BigDecimal is created as a result of setScale, it does not need to round the result to get that precise value.
It would also work if you blew out the scale:
System.out.println(new BigDecimal("1.1").setScale(1_000, RoundingMode.UNNECESSARY));
...but, it would break if you tried something like this:
System.out.println(new BigDecimal("1.12").setScale(1, RoundingMode.UNNECESSARY));
The reason for that: you have to round your BigDecimal now in order for you to represent the appropriate scale (1 number after the decimal).
You wouldn't see the behavior if you had your scale larger than the amount of digits after your decimal, but may see another rounding behavior if it was already established on that instance of your BigDecimal. You have complete control over the rounding, so use that judiciously.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I want to be able to compare a number of files (at most 30) against each other in order to find some sort of degree of similarity. It wouldn't need to be perfect I just want some sort of red flag if two files are unusually similar. What would be a good way to go about this?
You could use Regular Expressions (commonly known as regex: python regex docs). Using grouping, you could find variable and function names, unique lines of code (lines that aren't whitespace or comments), etc.
However, creating a system that is smart enough to be able to detect similarities on its own can be very difficult. If you had some way of getting a number between 0 and 1 of two files and their similarities, you could test it against a high threshold. Anything over the threshold (say for example, 0.97) could be considered suspicious.
Besides looking at the physical code, you could also observe code density in the files. Imagine if you printed out a page of code and turned it 90 degrees. You essentially get a graph of the number of lines on each file. Using that, you can observe where there are peaks and valleys to see where the code is more or less dense. Two similar files may have the same or very close code densities. Also, using this method you don't have to worry about looking for variable or function names that are the same as you aren't so much looking at the code itself but rather how it's organized
Fleshing out #mgilson's comment, here's a very simple start:
def file_similarity(path1, path2):
"Return float in [0., 1.] giving some measure of file similarity."
import difflib
with open(path1, "rb") as f1, open(path2, "rb") as f2:
s = difflib.SequenceMatcher(
lambda ch: ch in " \t", # don't sync on blanks or tabs
f1.read(),
f2.read())
return s.ratio()
Read the SequenceMatcher docs for more. In particular, if you have many files to compare, it's much more efficient to reuse a SequenceMatcher object (see the set_seq1() and set_seq2() methods). And if you're using a threshold, as the accepted answer suggested, see the real_quick_ratio() and quick_ratio() methods to slash the time more.
To get better results, I'd feed the files through a normalization transformation first, primarily to replace tab characters with spaces (tabs and spaces are as different to character comparison as, say, 'a' and '/', but the distinction is invisible to the human eye). Removing all-whitespace lines would probably help too.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I just looked at the AndPredicate and the ALWAYS_FALSE predicates and didn't find any indication that an AndPredicate noticed an ALWAYS_FALSE component to short circuit itself.
It seems like there may be a number of optimization opportunities if a Predicate is known to be ALWAYS_TRUE or ALWAYS_FALSE. For example, could Iterables.filter(Iterable,Predicate) just return the input Iterable(perhaps an unmodifiable view of it) with an ALWAYS_TRUE Predicate, and an empty Iterable with an ALWAYS_FALSE Predicate?
Similarly an AndPredicate which contains an ALWAYS_FALSE could be considered an ALWAYS_FALSE for this type of optimization (and OrPredicate with an ALWAYS_TRUE, etc.)
Is this optimization already in place, and I missed it in my casual search, or are there reasons (side effect behavior changed?) why this would not be a good idea?
There are reasons why this would not be a good idea: it wouldn't end up being an "optimization."
Specifically, the users who'd be affected by this optimization are a very small fraction of the users of Predicates.and and Predicates.or. Let's say for the purposes of argument that this is 1%, though honestly I think that's probably generous. Not many users are knowingly passing ALWAYS_TRUE to Predicates.or.
Now, is the win for the 1% worth the cost of the check to see if the optimization is possible to the 99%? To speed up that 1%, you've slowed down everyone else -- and everyone else massively outnumbers the users getting sped up.
It's possible that the JIT might do enough inlining to figure out that it can do an equivalent optimization -- i.e. working out that the if condition is always true, and skipping the if statement. But the JIT is heavily optimized to only do optimizations that, on average, are worth the time invested in checking whether the optimization is applicable.