long-integer

Multiplication of two integers in C++

女生的网名这么多〃 提交于 2019-11-27 16:00:51
I have a pretty basic question, but I am not sure if I understand the concept or not. Suppose we have: int a = 1000000; int b = 1000000; long long c = a * b; When I run this, c shows negative value, so I changed also a and b to long long and then everything was fine. So why do I have to change a and b , when their values are in range of int and their product is assigned to c (which is long long )? I am using C/C++ The int s are not promoted to long long before multiplication, they remain int s and the product as well. Then the product is cast to long long , but too late, overflow has struck.

How can I generate large, ranged random numbers in Swift?

£可爱£侵袭症+ 提交于 2019-11-27 15:54:26
I'm looking for an efficient method of generating large numbers (that includes floating point types!) in Swift, with arbitrary ranges (which may even be UInt.max or Int.max ) All the existing questions I've seen either crash for large values ( UInt.max ) or don't support ranges. I know that you can read from /dev/urandom for random bytes, but that doesn't help restrict these values to a given interval (and I'm pretty sure looping until it does isn't efficient). Martin R Here is a possible solution for UInt , Int and Double which works with the full range of those types. It is written as

Java 8 - converting an Integer to a long compilation issue

為{幸葍}努か 提交于 2019-11-27 14:57:19
问题 I have the following abstract generic data holder in my project (simplified): public abstract static class Value<E> { E value; public void setValue(E value) { this.value = value; } public E getValue() { return this.value; } public String toString() { return "[" + value + "]"; } } Along with an InputCollection which contains a list of Objects : public static class InputCollection { private ArrayList<Object> values; public InputCollection() { this.values = new ArrayList<>(); } public void

Conversion IPv6 to long and long to IPv6

坚强是说给别人听的谎言 提交于 2019-11-27 14:41:34
问题 How should I perform conversion from IPv6 to long and vice versa? So far I have: public static long IPToLong(String addr) { String[] addrArray = addr.split("\\."); long num = 0; for (int i = 0; i < addrArray.length; i++) { int power = 3 - i; num += ((Integer.parseInt(addrArray[i], 16) % 256 * Math.pow(256, power))); } return num; } public static String longToIP(long ip) { return ((ip >> 24) & 0xFF) + "." + ((ip >> 16) & 0xFF) + "." + ((ip >> 8) & 0xFF) + "." + (ip & 0xFF); } Is it correct

node.js is there any proper way to parse JSON with large numbers? (long, bigint, int64)

ⅰ亾dé卋堺 提交于 2019-11-27 14:39:59
When I parse this little piece of JSON { "value" : 9223372036854775807 } that's what I get { hello: 9223372036854776000 } Is there any way to parse it properly? Not with built-in JSON.parse. You'll need to parse it manually and treat values as string (if you want to do arithmetics with them there is bignumber.js ) You can use Douglas Crockford JSON.js library as a base for your parser. EDIT: I created a package for you :) var JSONbig = require('json-bigint'); var json = '{ "value" : 9223372036854775807, "v2": 123 }'; console.log('Input:', json); console.log(''); console.log('node.js bult-in

Cast int to pointer - why cast to long first? (as in p = (void*) 42; )

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-27 13:58:30
问题 In the GLib documentation, there is a chapter on type conversion macros. In the discussion on converting an int to a *void pointer it says (emphasis mine): Naively, you might try this, but it's incorrect: gpointer p; int i; p = (void*) 42; i = (int) p; Again, that example was not correct, don't copy it. The problem is that on some systems you need to do this: gpointer p; int i; p = (void*) (long) 42; i = (int) (long) p; (source: GLib Reference Manual for GLib 2.39.92, chapter Type Conversion

Primary Key Type: int vs long

不羁岁月 提交于 2019-11-27 13:33:19
问题 I know some software shops have been burned by using the int type for the primary key of a persistent class. That being said, not all tables grow past 2 billions. As a matter of fact, most don't. So, do you guys use the long type only for those classes that are mapped to potentially large tables OR for every persistent class just to be consistent? What's the industry concensus? I'll leave this question open for a while so that you can share with us your success/horror stories. 回答1: Long can

What happens when I assign long int to int in C?

偶尔善良 提交于 2019-11-27 12:00:42
In a recent homework assignment I've been told to use long variable to store a result, since it may be a big number. I decided to check will it really matter for me, on my system (intel core i5/64-bit windows 7/gnu gcc compiler) and found out that the following code: printf("sizeof(char) => %d\n", sizeof(char)); printf("sizeof(short) => %d\n", sizeof(short)); printf("sizeof(short int) => %d\n", sizeof(short int)); printf("sizeof(int) => %d\n", sizeof(int)); printf("sizeof(long) => %d\n", sizeof(long)); printf("sizeof(long int) => %d\n", sizeof(long int)); printf("sizeof(long long) => %d\n",

Why can't your switch statement data type be long, Java?

浪尽此生 提交于 2019-11-27 11:24:45
Here's an excerpt from Sun's Java tutorials : A switch works with the byte , short , char , and int primitive data types. It also works with enumerated types (discussed in Classes and Inheritance) and a few special classes that "wrap" certain primitive types: Character , Byte , Short , and Integer (discussed in Simple Data Objects). There must be a good reason why the long primitive data type is not allowed. Anyone know what it is? I think to some extent it was probably an arbitrary decision based on typical use of switch. A switch can essentially be implemented in two ways (or in principle, a

Is “long x = 1/2” equal to 1 or 0, and why? [duplicate]

霸气de小男生 提交于 2019-11-27 09:42:04
This question already has an answer here: Integer division: How do you produce a double? 10 answers if I have something like: long x = 1/2; shouldn't this be rounded up to 1? When I print it on the screen it say 0. It's doing integer division, which truncates everything to the right of the decimal point. Integer division has its roots in number theory. When you do 1/2 you are asking how many times does 2 equal 1? The answer is never, so the equation becomes 0*2 + 1 = 1, where 0 is the quotient (what you get from 1/2) and 1 is the remainder (what you get from 1%2). It is right to point out that