integer

Extending java Integer cache

喜你入骨 提交于 2019-12-20 02:27:22
问题 There's a general advice to use Integer.valueOf(int) instead of new Integer(int) because of caching. In JDK 5+, you should really use valueOf because Integer now caches Integer objects between -128 and 127 and can hand you back the same exact Integer(0) object every time instead of wasting an object construction on a brand new identical Integer object. How can extend the range? 回答1: You can use the java.lang.Integer.IntegerCache.high property to increase the size of this cache. ex : java

TypeError: cannot concatenate 'str' and 'int' objects

扶醉桌前 提交于 2019-12-20 01:06:00
问题 I'm learning Python now, yay! Anyway, I have small problem. I don't see problem in here: x = 3 y = 7 z = 2 print "I told to the Python, that the first variable is %d!" % x print "Anyway, 2nd and 3rd variables sum is %d. :)" % y + z But Python thinks different - TypeError: cannot concatenate 'str' and 'int' objects . Why is that so? I haven't setted any variable as string... as much as I see. 回答1: % has a higher precedence than + , so s % y + z is parsed as (s % y) + z . If s is a string, then

Dividing integers

北城余情 提交于 2019-12-19 19:55:19
问题 I know when dividing integers the default way it works is to discard the fractional part. E.g., int i, n, calls = 0; n = 1; n /= 3; printf("N = %i\n", n); for (i = 1; i > 0; i /= 3) { calls++; } printf("Calls = %i\n", calls); The code above prints: N = 0 Calls = 1 Could you please explain this behavior? 回答1: 1 divided by 3 = .3333 (repeating of course), mathematically. You can think of the computer as truncating the .3333 since it is doing integer arithmetic ( 0 remainder 1 ). The for loop

VHDL: Finding out/reporting bit width/length of integer (vs. std_logic_vector)?

被刻印的时光 ゝ 提交于 2019-12-19 19:36:50
问题 Say I need a signal to represent numbers from 0 to 5; obviously this needs 3 bits of std_logic to be represented ( i.e if MAXVAL=5, then bitwidth= { wcalc "floor(logtwo($MAXVAL))+1" } ). I'm aware I could do: SIGNAL myLogicVector : STD_LOGIC_VECTOR(2 downto 0) := 5; with which I'd explicitly specify an array of three std_logic 'bits', and set initial value; then I could use REPORT to print out the length (in this case, 3): report("Bit width of myLogicVector is "& integer'image(myLogicVector

How to generate a random integer in the range [0,n] from a stream of random bits without wasting bits?

折月煮酒 提交于 2019-12-19 18:57:07
问题 I have a stream of (uniform) random bits from which I'd like to generate random integers uniformly in the range [0,n] without wasting bits. (I'm considering bits wasted which are in excess of floor(log_2(n))+1, on the assumption that it's always possible to use no more than that.) E.g., if n = 5, then the algorithm I'm looking for should use no more than three bits. How can this be done? 回答1: This is equivalent to find a two-way function between two set of different (finite) cardinality. It

Error declaring integer variable inside MySQL stored function

吃可爱长大的小学妹 提交于 2019-12-19 18:22:54
问题 I'm getting an error when trying to declare a new stored function in MySQL (Server version: 5.5.13) Basically, I have a large table which classifies strings depending on how they start. My function takes a string (from user input) and then tells you the classification of that string by searching the database for the classification. It's a bit like a LIKE query, except in reverse as it's the user input that contains the full string and the database contains the string being searched for. Hope

Flat file destination columns data types validation

强颜欢笑 提交于 2019-12-19 09:48:13
问题 A source database field of type INT is read through an OLE DB Source. It is eventually written to a Flat File Destination. The destination Flat File Connection Manager > Advanced page reports it as a four-byte signed integer [DT_I4] . This data type made me think it indicated binary. Clearly, it does not. I was surprised that it was not the more generic numeric [DT_NUMERIC] . I changed this type setting to single-byte signed integer [DT_I1] . I expected this to fail, but it did not. The

Read all integers from string c++

早过忘川 提交于 2019-12-19 09:00:03
问题 I need some help with getting all the integers from a std::string and getting each of those integer into an int variable. String example: <blah> hi 153 67 216 I would like the program to ignore the "blah" and "hi" and store each of the integers into an int variable. So it comes out to be like: a = 153 b = 67 c = 216 Then i can freely print each separately like: printf("First int: %d", a); printf("Second int: %d", b); printf("Third int: %d", c); Thanks! 回答1: You can create your own function

How to tell if a 32 bit int can fit in a 16 bit short

橙三吉。 提交于 2019-12-19 06:18:35
问题 Using only: ! ~ & ^ | + << >> I need to find out if a signed 32 bit integer can be represented as a 16 bit, two's complement integer. My first thoughts were to separate the MSB 16 bits and the LSB 16 bits and then use a mask to and the last 16 bits so if its not zero, it wont be able to be represented and then use that number to check the MSB bits. An example of the function I need to write is: fitsInShort(33000) = 0 (cant be represented) and fitsInShort(-32768) = 1 (can be represented) 回答1:

How to check if each element in a vector is integer or not in R?

走远了吗. 提交于 2019-12-19 05:32:54
问题 Say, I have a vector y, and I want to check if each element in y is integer or not, and if not, stop with an error message. I tried is.integer(y), but it does not work. 回答1: The simplest (and fastest!) thing is probably this: stopifnot( all(y == floor(y)) ) ...So trying it out: y <- c(3,4,9) stopifnot( all(y == floor(y)) ) # OK y <- c(3,4.01,9) stopifnot( all(y == floor(y)) ) # ERROR! If you want a better error message: y <- c(3, 9, NaN) if (!isTRUE(all(y == floor(y)))) stop("'y' must only