integer-overflow

How to detect overflow when subtracting two signed 32 bit numbers in C?

旧时模样 提交于 2020-02-23 06:09:47
问题 I've got two signed integers, and i'd like to subtract them. I need to know if it overflowed. int one; int two; int result = two-one; if (OVERFLOW) { printf("overflow"); } else { printf("no overflow"); } Something like that. Is there a good way to do this? 回答1: Firstly, overflow in signed calculations causes undefined behavior in C. Secondly, forgetting about UB for a second and sticking to the typical overflow behavior of a 2's complement machine: overflow is revealed by the fact that result

CUDA large input arrays

这一生的挚爱 提交于 2020-01-16 01:47:05
问题 I am new to CUDA, I have been working on a "Reduce algorithm". The algorithm works for any array size less than 1<<24. When I use arrays of size 1<<25 the program returns 0 in "total sum" which is wrong. The sum should me 2^25 EDIT cuda-memcheck compiled_code ========= CUDA-MEMCHECK @@STARTING@@ ========= Program hit cudaErrorInvalidValue (error 11) due to "invalid argument" on CUDA API call to cudaLaunch. ========= Saved host backtrace up to driver entry point at error ========= Host Frame:

does c++ have an equivalent boost::numeric_cast<DestType>(SourceType)?

一个人想着一个人 提交于 2020-01-14 10:22:00
问题 I am doing a bunch of applied-mathematics/signal-processing/algorithms C++ code. I have enabled the -Wconversion compiler warning to catch issues like runtime conversion of numbers that are type double to type int32_t . Obviously I am always concerned during these conversions because: loss of the numbers decimal value possible positive or negative overflow ("positive overflow" is when a double has a value greater than INT32_MAX and tries to store that value in the destination type ( int32_t

how to calculate (a times b) divided by c only using 32-bit integer types even if a times b would not fit such a type

别说谁变了你拦得住时间么 提交于 2020-01-11 03:15:08
问题 Consider the following as a reference implementation: /* calculates (a * b) / c */ uint32_t muldiv(uint32_t a, uint32_t b, uint32_t c) { uint64_t x = a; x = x * b; x = x / c; return x; } I am interested in an implementation (in C or pseudocode) that does not require a 64-bit integer type. I started sketching an implementation that outlines like this: /* calculates (a * b) / c */ uint32_t muldiv(uint32_t a, uint32_t b, uint32_t c) { uint32_t d1, d2, d1d2; d1 = (1 << 10); d2 = (1 << 10); d1d2 =

Is it possible to access the overflow flag register in a CPU with C++?

那年仲夏 提交于 2020-01-01 04:45:25
问题 After performing a mathematical operation, for say, multiplying two integers, is it possible to access the overflow flag register in a CPU with C++ ? If not what are other fast ways to check for an overflow ? 回答1: No, generally it's impossible. Some CPUs don't even have such a flag (e.g. MIPS). The link provided in one of the comments will give you ideas on how you can do overflow checks. Remember that in C and C++ signed integer overflows cause undefined behavior and legally you cannot

Does integer overflow cause undefined behavior because of memory corruption?

梦想的初衷 提交于 2019-12-30 00:13:06
问题 I recently read that signed integer overflow in C and C++ causes undefined behavior: If during the evaluation of an expression, the result is not mathematically defined or not in the range of representable values for its type, the behavior is undefined. I am currently trying to understand the reason of the undefined behavior here. I thought undefined behavior occurs here because the integer starts manipulating the memory around itself when it gets too big to fit the underlying type. So I

what's wrong with golang constant overflows uint64

只愿长相守 提交于 2019-12-29 08:10:10
问题 userid := 12345 did := (userid & ^(0xFFFF << 48)) when compiling this code, I got: ./xxxx.go:511: constant -18446462598732840961 overflows int Do you know what is the matter with this and how to solve it ? Thanks. 回答1: ^(0xFFFF << 48) is an untyped constant, which in go is an arbitrarily large value. 0xffff << 48 is 0xffff000000000000 . When you negate it, you get -0xffff000000000001 (since with two's complement, -x = ^x + 1, or ^x = -(x + 1)). When you write userid := 12345 , userid gets the

How does one trap arithmetic overflow errors in Swift?

此生再无相见时 提交于 2019-12-28 22:07:45
问题 This one is probably easy. We know that the operator &+ does modular arithmetic on integers (wraps around), while the operator + causes an error. $ swift 1> var x: Int8 = 100 x: Int8 = 100 2> x &+ x $R0: Int8 = -56 3> x + x Execution interrupted. Enter Swift code to recover and continue. What kind of error is this? I can't catch it and I can't turn it in to an optional: 4> do {try x + x} catch {print("got it")} Execution interrupted. Enter Swift code to recover and continue. 5> try? x + x

Often big numbers become negative

早过忘川 提交于 2019-12-28 04:24:04
问题 Since I started using eclipse for project euler, I noticed that big numbers sometime become a seemingly random negative numbers. I suppose this has something to do with passing the boudry of the type. I'll be glad if you could explain to me how these negative numbers are generated and what is the logic behind it. Also, how can I avoid them (preferable not with BigInteger class). Danke!=) 回答1: This image shows what you're looking for. In your case it's obviously larger numbers, but the

How to nicely “cast” qint64 to int for QProgressBar

自作多情 提交于 2019-12-23 12:23:27
问题 I'm playing around with QFtp (yes .. I know) and all works well. Using code from their own example(s) as a guideline. http://doc.qt.io/archives/qt-4.7/network-qftp-ftpwindow-cpp.html The only problem I'm having is when sending (or receiving) big files (let's say 3 GB) the progress bar glitches out. This is due to the cast from qint64 to int in: void FtpWindow::updateDataTransferProgress(qint64 readBytes, qint64 totalBytes) { progressDialog->setMaximum(totalBytes); progressDialog->setValue