long-integer

Long Vs. Int C/C++ - What's The Point?

孤者浪人 提交于 2019-12-17 21:53:11
问题 As I've learned recently, a long in C/C++ is the same length as an int. To put it simply, why? It seems almost pointless to even include the datatype in the language. Does it have any uses specific to it that an int doesn't have? I know we can declare a 64-bit int like so: long long x = 0; But why does the language choose to do it this way, rather than just making a long well...longer than an int? Other languages such as C# do this, so why not C/C++? 回答1: When writing in C or C++, every

strtoull and long long arithmetic

天涯浪子 提交于 2019-12-17 21:28:26
问题 Can anyone explain the output of this program and how I can fix it? unsigned long long ns = strtoull("123110724001300", (char **)NULL, 10); fprintf(stderr, "%llu\n", ns); // 18446744073490980372 回答1: Do you have <stdlib.h> included? I can reproduce on MacOS X if I omit <stdlib.h> . #include <stdio.h> #include <stdlib.h> int main(void) { unsigned long long ns = strtoll("123110724001300", (char **)NULL, 10); printf("%llu\n", ns); return(0); } Omit the header, I get your result. Include the

1000 * 60 * 60 * 24 * 30 results in a negative number [duplicate]

↘锁芯ラ 提交于 2019-12-17 16:49:11
问题 This question already has answers here : Why do these two multiplication operations give different results? (2 answers) Closed 6 years ago . I'm attempting to calculate 30 days by multiplying milliseconds however the result continually ends up being a negative number for the value of days_30 and I'm not sure why. Any suggestions are greatly appreciated! CODE SNIPPET: // check to ensure proper time has elapsed SharedPreferences pref = getApplicationContext() .getSharedPreferences(

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

我们两清 提交于 2019-12-17 14:24:50
问题 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). 回答1: Here is a possible

Wrong result by Java Math.pow

房东的猫 提交于 2019-12-17 09:59:51
问题 If you try to run the following code public class Main { public static void main(String[] args) { long a = (long)Math.pow(13, 15); System.out.println(a + " " + a%13); } } You will get "51185893014090752 8" The correct value of 13^15 is 51185893014090757 , i.e. greater than the result returned by Math.pow by 5 . Any ideas of what may cause it? 回答1: You've exceeded the number of significant digits available (~15 to 16) in double-precision floating-point values. Once you do that, you can't

Wrong result by Java Math.pow

泄露秘密 提交于 2019-12-17 09:59:03
问题 If you try to run the following code public class Main { public static void main(String[] args) { long a = (long)Math.pow(13, 15); System.out.println(a + " " + a%13); } } You will get "51185893014090752 8" The correct value of 13^15 is 51185893014090757 , i.e. greater than the result returned by Math.pow by 5 . Any ideas of what may cause it? 回答1: You've exceeded the number of significant digits available (~15 to 16) in double-precision floating-point values. Once you do that, you can't

C: Casting minimum 32-bit integer (-2147483648) to float gives positive number (2147483648.0)

空扰寡人 提交于 2019-12-17 09:53:47
问题 I was working on an embedded project when I ran into something which I thought was strange behaviour. I managed to reproduce it on codepad (see below) to confirm, but don't have any other C compilers on my machine to try it on them. Scenario: I have a #define for the most negative value a 32-bit integer can hold, and then I try to use this to compare with a floating point value as shown below: #define INT32_MIN (-2147483648L) void main() { float myNumber = 0.0f; if(myNumber > INT32_MIN) {

Generate random values in C#

帅比萌擦擦* 提交于 2019-12-17 09:22:16
问题 How can I generate random Int64 and UInt64 values using the Random class in C#? 回答1: This should do the trick. (It's an extension method so that you can call it just as you call the normal Next or NextDouble methods on a Random object). public static Int64 NextInt64(this Random rnd) { var buffer = new byte[sizeof(Int64)]; rnd.NextBytes(buffer); return BitConverter.ToInt64(buffer, 0); } Just replace Int64 with UInt64 everywhere if you want unsigned integers instead and all should work fine.

Definition of int64_t

别说谁变了你拦得住时间么 提交于 2019-12-17 07:12:26
问题 I am new to C/C++, so I have a couple of questions about a basic type: a) Can you explain to me the difference between int64_t and long ( long int )? In my understanding, both are 64 bit integers. Is there any reason to choose one over the other? b) I tried to look up the definition of int64_t on the web, without much success. Is there an authoritative source I need to consult for such questions? c) For code using int64_t to compile, I am currently including <iostream> , which doesn't make

Definition of int64_t

依然范特西╮ 提交于 2019-12-17 07:12:03
问题 I am new to C/C++, so I have a couple of questions about a basic type: a) Can you explain to me the difference between int64_t and long ( long int )? In my understanding, both are 64 bit integers. Is there any reason to choose one over the other? b) I tried to look up the definition of int64_t on the web, without much success. Is there an authoritative source I need to consult for such questions? c) For code using int64_t to compile, I am currently including <iostream> , which doesn't make