How to panic when a NaN is created? - rust

I'm trying to find where a NaN is created in a huge codebase. Is there some compiler flag or something I can use to panic on NaN so I can find what line it's on?

There's no compiler flag. The best you can do is to wrap your floats in noisy_float newtype.

Related

(Python) Why does running the line "wut:2+2=5" not give a syntax error?

Why is code like this apparently legal (does not give a syntax error when running) in Python 3?
wut:2+2=5
I've tried to find out what this type of syntax could mean and I could not find it. It seems like a key-value pair, but can you just have one lying around? And "2+2=5" is not a valid... anything, is it?
They are type hints in python. Basically, you hint the type of the object(s) you're using. Type-hints are for maintainability and don't get interpreted by Python. So, you might say that a variable has type float whereas python will internally interpret as int only. Eg -
my_var:float = 4
print(type(my_var))
OUTPUT :
<class 'int'>
In your case, it doesn't make much sense. But to ease maintainability when your projects grow large, it is helpful. These can be considered as the next step from # type comments. Eg-
my_var = 4.2 # type: float
my_var: float = 4.2
The above two codes can be considered to be similar in their functionalities.
You can read more about it here - https://docs.python.org/3/library/typing.html
More on this - What are type hints in Python 3.5?

What do the optimization levels `-Os` and `-Oz` do in rustc?

Executing rustc -C help shows (among other things):
-C opt-level=val -- optimize with possible levels 0-3, s, or z
The levels 0 to 3 are fairly intuitive, I think: the higher the level, the more aggressive optimizations will be performed. However, I have no clue what the s and z options are doing and I couldn't find Rust-related information about them.
It seems like you are not the only one confused, as described in a Rust issue. It seems to follow the same pattern as Clang:
Os For optimising the size when compiling.
Oz For even more size optimisation.
Looking at these and these lines in Rust's source code, I can say that s means optimize for size, and z means optimize for size some more.
All optimizations seem to be performed by the LLVM code-generation engine.
These two sequences, Os and Oz, within LLVM, are pretty similar. Oz invokes 260 passes (I am using LLVM 12.0), whereas Os invokes 264. Oz' sequence of analyses and optimizations is almost a strict subsequence of Os', except for one pass (opt -loops), which appears in a different place within Os. This said, notice that the effects of the optimizations can still be different, because they use different cost models, e.g., constants that determine the behavior of optimizations. Thus, optimizations that have impact on size, like loop unrolling and inlining can behave differently in these two sequences.

Is 5dp same as 5.0dp?

Reading android code I find out is not uncommon to fill the xml with the second variant, like here.
To be honest, this alone would be enough for my head to torment me with the question, but then I see code in which the two notations are even mixed, like in this StackOverflow question.
Google wasn't of help, and I couldn't find this question on SO.
Anyone knows what's the difference, if any?
EDIT
Based on the firsts question I got here, there seems to be no improvement in performance or other things like that when using the second variant, so writing them as float when they can be integers just tells people they can insert decimal values in there. Anything else?
If that's the only reason, I think using the second option it's just like polluting the xml for little to no reason, are there other opinions?
Check out this link: http://developer.android.com/guide/topics/resources/more-resources.html#Dimension. It shows the possible values for dimensions. They are all floats. So, you can write them either way.
The 5.0 version is because certain properties accept a float/double value. All in all, 5.0 is the same as 5 when DP are involved.
one is an int the other is a float. They are types of primitives
It is the same. They both have the same values. 5 and 5.0 are of different types. 5 is of type int and 5.0 is of type float/double.

Modeling z on a real in system verilog

I am making a very simple model of a an output driver which can have 3 output levels. It can also be turned off. What I want to do is model this as a real so I can assign different numbers for the three levels. I am unsure to do to model the 'off' state. I found some discussion about using inf or NaN to model z in a real number, but it doesn't seem like this is a settled argument. So this is a 2 part question:
1) is NaN the right way to model z on a real in system verilog?
2) how do I assign NaN in system verilog? This works:
realnum = 0.0/0.0;
But it seems like a bit of a kludge to do this. Is there already a defined way to get NaN?
The right way to do what I want is probably to use verilog-AMS but I want to avoid using a license for a simulator that supports it for such a simple model.
Edit: To clarify why I want to do this: this model will be a cell view in the top level schematic that I will netlist. I can't do a 2 bit output since the schematic has only one wire.
A new feature 6.6.7 User-defined nettypes was added to the 1800-2012 LRM, but I do not think it has been implemented by most vendors yet. In the meantime, you can create your own resolution functions, and manually insert them where needed to convert your real to a 4-state value.

How to find uninitialized variables in C on Linux?

My C source code has many unintialized variables. The code is on RHEL 6.4 operating system.
Is there a way to find all the uninitialized variables?
Finding all of them is impossible, in the mathematical sense (at least without false-positives). However, there are some tools to help find some of them:
Turn on compiler warnings. With gcc, this would be -Wuninitialized, -Winit-self, and -Wmaybe-uninitialized. Note that you will need to try this with various levels of optimization; you'll get different warnings at different -O levels. Note that -Wmaybe-uninitialized (as the name suggests) may give false positives.
For uninitialized memory (as in malloc, etc.), you can use valgrind. This actually requires running the program.
Static checkers such as splint. (Thanks to Andy Lester for this suggestion.)
Assuming you're using GCC, compile your program with -Wuninitialized. Better to just always compile with -Wall, because with C a programmer needs all the help he can get.

Resources