integer

How to check if the value is integer in java? [duplicate]

前提是你 提交于 2019-12-18 05:15:13
问题 This question already has answers here : Determine if object is integer (3 answers) Closed 2 years ago . I'm using some API by restTemplate. The API returns a key whose type is integer. But I'm not sure of that value, so I want to check whether the key is really an integer or not. I think it might be a string. What is the best way of checking if the value is really integer? added: I mean that some API might return value like below. {id : 10} or {id : "10"} 回答1: Object x = someApi(); if (x

Java: How do I perform integer division that rounds towards -Infinity rather than 0?

♀尐吖头ヾ 提交于 2019-12-18 04:44:13
问题 ( note : not the same as this other question since the OP never explicitly specified rounding towards 0 or -Infinity) JLS 15.17.2 says that integer division rounds towards zero. If I want floor() -like behavior for positive divisors (I don't care about the behavior for negative divisors), what's the simplest way to achieve this that is numerically correct for all inputs? int ifloor(int n, int d) { /* returns q such that n = d*q + r where 0 <= r < d * for all integer n, d where d > 0 * * d = 0

Compress sorted integers

眉间皱痕 提交于 2019-12-18 04:42:30
问题 I'm building a index which is just several sets of ordered 32 bit integers stored continuously in a binary file. The problem is that this file grows pretty large. I've been thinking of adding some compressions scheme but that's a bit out of my expertise. So I'm wondering, what compression algorithm would work best in this case? Also, decompression has to be fast since this index will be used to make make look ups. 回答1: If you are storing integers which are close together (eg: 1, 3 ,4, 5, 9,

Calculate Nth root with integer arithmetic

笑着哭i 提交于 2019-12-18 04:40:20
问题 There are a couple of ways to find integer square roots using only integer arithmetic. For example this one. It makes for interesting reading and also a very interesting theory, particularly for my generation where such techniques aren't so useful any more. The main thing is that it can't use floating point arithmetic, so that rules out newtons method and it's derivations. The only other way I know of to find roots is binomial expansion, but that also requires floating point arithmetic. What

php hash form string to integer

♀尐吖头ヾ 提交于 2019-12-18 04:34:31
问题 Does PHP have a built in function for doing string to integer hashes, something that's difficult to reverse? Now, I know I can probably get away with doing an md5, and treating a substring of it as a radix 16 number, but I'm looking for something built in. Thanks. 回答1: I think the best bet would chose a standard hash [either md5() or sha1() ] to obtain a hash of your string, and then to get an integer hash, to a base_convert($hash, 16, 10) and that should convert your hash into a integer hash

Integer calculations on GPU

杀马特。学长 韩版系。学妹 提交于 2019-12-18 04:33:28
问题 For my work it's particularly interesting to do integer calculations, which obviously are not what GPUs were made for. My question is: Do modern GPUs support efficient integer operations? I realize this should be easy to figure out for myself, but I find conflicting answers (for example yes vs no), so I thought it best to ask. Also, are there any libraries/techniques for arbitrary precision integers on GPUs? 回答1: First, you need to consider the hardware you're using: GPU devices performance

How to tell apart numeric scalars and string scalars in Perl?

走远了吗. 提交于 2019-12-18 03:52:10
问题 Perl usually converts numeric to string values and vice versa transparently. Yet there must be something which allows e.g. Data::Dumper to discriminate between both, as in this example: use Data::Dumper; print Dumper('1', 1); # output: $VAR1 = '1'; $VAR2 = 1; Is there a Perl function which allows me to discriminate in a similar way whether a scalar's value is stored as number or as string? 回答1: There's no way to find this out using pure perl. Data::Dumper uses a C library to achieve it. If

Does Lua make use of 64-bit integers?

ぃ、小莉子 提交于 2019-12-18 03:43:34
问题 Does Lua make use of 64-bit integers? How do I use it? 回答1: Compile it yourself. Lua uses double-precision floating point numbers by default. However, this can be changed in the source ( luaconf.h , look for LUA_NUMBER ). 回答2: require "bit" -- Lua unsigned 64bit emulated bitwises -- Slow. But it works. function i64(v) local o = {}; o.l = v; o.h = 0; return o; end -- constructor +assign 32-bit value function i64_ax(h,l) local o = {}; o.l = l; o.h = h; return o; end -- +assign 64-bit v.as 2

Constructing the largest number possible by rearranging a list

你离开我真会死。 提交于 2019-12-18 03:06:10
问题 Say I have an array of positive whole integers; I'd like to manipulate the order so that the concatenation of the resultant array is the largest number possible. For example [97, 9, 13] results in 99713 ; [9,1,95,17,5] results in 9955171 . I'm not sure of an answer. 回答1: sorted(x, cmp=lambda a, b: -1 if str(b)+str(a) < str(a)+str(b) else 1) 回答2: Intuitively, we can see that a reverse sort of single digit numbers would lead to the higest number: >>> ''.join(sorted(['1', '5', '2', '9'], reverse

Binary String to Integer

六眼飞鱼酱① 提交于 2019-12-17 23:46:35
问题 I have a binary string, entered by the user, which I need to convert to an integer. At first I naivly used this simple line: Convert.ToInt32("11011",2); Unfortunately this throws an exception if the user enters the integer directly. Convert.ToInt32("123",2); // throws Exception How can I make sure that the string entered by the user actually is a binary string? try..catch....but that's just too ugly. something like 'Int32.TryParse' maybe. Thanks 回答1: You could use a Regex to check that it is