zero

Can I check in C(++) if an array is all 0 (or false)?

☆樱花仙子☆ 提交于 2019-12-01 17:52:45
问题 Can I check in C(++) if an array is all 0 (or false) without iterating/looping over every single value and without allocating a new array of the same size (to use memcmp )? I'm abusing an array of bools to have arbitrary large bitsets at runtime and do some bitflipping on it 回答1: You can use the following condition: (myvector.end() == std::find(myvector.begin(), myvector.end(), true)) Obviously, internally, this loops over all values. The alternative (which really should avoid looping) is to

`java (0 % 2 != 0) == false`

巧了我就是萌 提交于 2019-12-01 16:34:57
The part I keep getting stuck on is boolean(0 % 2 !=0) == false. I mean if 2 goes into 0, 0 times then the remainder would be 2, and 2 does not equal 0. So it should be true. Yet but when I put the boolean in my java program it is treating it as false. Anyone know why? The only logical answer I can wrap my head around is that maybe integers go into 0 and infinite number of times and so are recognized as false, anyone? There are two steps: 0 % 2 evaluates to 0 . 0 != 0 evaluates to false . To elaborate on the first step, the JLS defines the % operator like so: The binary % operator is said to

Initializing Primitive Array to One Value

半城伤御伤魂 提交于 2019-12-01 16:25:22
问题 Is there a way to initialize an array of primitives, say a integer array, to 0? Without using a for loop? Looking for concise code that doesn't involve a for loop. :) 回答1: int array[10] = {}; // to 0 std::fill(array, array + 10, x); // to x Note if you want a more generic way to get the end: template <typename T, size_t N> T* endof(T (&pArray)[N]) { return &pArray[0] + N; } To get: std::fill(array, endof(array), x); // to x (no explicit size) It should be mentioned std::fill is just a wrapper

Default Initialization Versus Zero Initialization

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-01 12:48:51
I cannot understand the behavior of gcc 4.8.1 or Visual Studio 2015 with respect to default initialization versus value initialization. It doesn't help that I'm trying to understand the differences between these myself and possibly running into compiler bugs? My question is: Can someone explain this behavior? And ideally tell me what should be happening. I have two classes: class Foo{ int _bar; public: void printBar(){ cout << _bar << endl; } }; class bar{ int ent; public: int getEnt(){return ent;} }; I'm using the following code to test: int main() { Foo foo; foo.printBar(); Foo().printBar();

in Java, how to delete all 0s in float?

时光毁灭记忆、已成空白 提交于 2019-12-01 06:19:15
I'd like to change float like this way: 10.5000 -> 10.5 10.0000 -> 10 How can I delete all zeros after the decimal point, and change it either float (if there's non-zeros) or int (if there were only zeros)? Thanks in advance. Why not try regexp? new Float(10.25000f).toString().replaceAll("\\.?0*$", "") Well the trick is that floats and doubles themselves don't really have trailing zeros per se; it's just the way they are printed (or initialized as literals) that might show them. Consider these examples: Float.toString(10.5000); // => "10.5" Float.toString(10.0000); // => "10.0" You can use a

How to display the leading zero's in a number of oracle

送分小仙女□ 提交于 2019-12-01 05:40:42
I have an oracle column(artnr) contains a length of 1 which is of type number(9). I want to update the number as following... Example : If number is 0 then it should be 00000 If number is 1 then it should be 00001 If number is 12 the it should be 00012 Remember : here 00000,0000, and 00012 are of number datatypes The following are the methods I have tried but failed.. UPDATE pitb.toestel b SET b.artnr = LPAD (b.artnr, 5, 0) WHERE b.idinventaris = 403743; Failed because Lpad can only be applied on strings UPDATE pitb.toestel b SET b.artnr = TO_NUMBER (TO_CHAR (artnr, '00009'), '00009') WHERE b

How to parse signed zero?

老子叫甜甜 提交于 2019-12-01 02:18:26
Is it possible to parse signed zero? I tried several approaches but no one gives the proper result: float test1 = Convert.ToSingle("-0.0"); float test2 = float.Parse("-0.0"); float test3; float.TryParse("-0.0", out test3); If I use the value directly initialized it is just fine: float test4 = -0.0f; So the problem seems to be in the parsing procedures of c#. I hope somebody could tell if there is some option or workaround for that. The difference could only be seen by converting to binary: var bin= BitConverter.GetBytes(test4); I think there is no way to force float.Parse (or Convert.ToSingle

Inserting variable number of zeros between non-zero elements in a vector in MATLAB

旧时模样 提交于 2019-11-30 23:25:58
I have a vector like: a = [1,2,3,4,5,6...,n] and I would like to obtain a new vector like this: a_new = [1,0,0,2,0,0,3,0,0,4,0,0,5,0,0,6,...,0,0,n] where a fixed number of zeros (2 in the above example) are inserted between the non-zero elements. If I choose zero_p=3 , the new vector would be: a_new = [1,0,0,0,2,0,0,0,3,0,0,0,4,0,0,0,5,0,0,0,6,...,0,0,0,n] etc. How can I do this? Try this: zero_p=3; a_new=zeros(1, (zero_p+1)*length(a)-zero_p); a_new(1:(zero_p+1):end)=a; (Untested, but should hopefully work.) There's a few ways I can think of: Kronecker product The kronecker product is

Binding an ObservableCollection.Count to Label with WPF

只谈情不闲聊 提交于 2019-11-30 23:25:28
I have a simple Label that should include the bound .Count value of a Property of an ObservableCollection . The thing is, that the result is always 0 (zero). The same Property is bound to a DataGrid, which works perfectly and even updates if something has changed in the Collection. What am I doing wrong here? Here is my code: <Label ContentStringFormat="Members: {0}"> <Label.Content> <Binding Path="MembersList.Count" Mode="OneWay" UpdateSourceTrigger="Default" /> </Label.Content> </Label> The Property looks like: public static ObservableCollection<Mitglied> MembersList { get; set; } Vishal You

How to get a -0 result in floating point calculations and distinguish it from +0 in C#?

只愿长相守 提交于 2019-11-30 21:18:42
The MSDN documentation mentions that double type includes negative zero. However, both -1.0 / double.PositiveInfinity and -double.Epsilon / 2 appear to return normal 0 (and compare equal to it). How can I get -0? Here is a practical example of differentiating between the two without examining the bits. MSDN links here and here assisted me in constructing this example. static void Main(string[] args) { float a = 5 / float.NegativeInfinity; float b = 5 / float.PositiveInfinity; float c = 1 / a; float d = 1 / b; Console.WriteLine(a); Console.WriteLine(b); Console.WriteLine(c); Console.WriteLine(d