466 questions
3
votes
1
answer
128
views
How to map a value from one range to another accurately without using floating point? [closed]
I am writing firmware for a microcontroller. (In this specific case it is a Microchip PIC, but I am looking for an answer that is equally applicable to any brand of microcontroller.)
Arduino has a map ...
4
votes
3
answers
187
views
Overflow arithmetic in C programming language
My platform is x86_64, and assume there are 3 variables whose types are all uint16_t:
uint16_t a, b, c;
And for the following two code snippets:
(1)
uint16_t tmp = b - a;
uint16_t result1 = c - tmp;
...
0
votes
1
answer
55
views
Postgres fast wrapping arithmetic operations without overflow on bigint
Is there any way to do wrapping operations (addition, multiplication, SUM) on bigint columns?
By this I mean the standard method of wrapping addition in other programming languages on integer types of ...
2
votes
1
answer
127
views
Does C++ have dynamically declared types based on integer width arithmetics?
The title may seem odd, but I'm not sure what else to call it. If you have suggestions please let me know and I'll edit.
Consider this
uint8_t lo = 0x11; // given
uint8_t hi = 0x22; // given
uint16_t ...
4
votes
1
answer
134
views
Arithmetic shift-right integers with half rounding toward zero
I am looking for an efficient algorithm that computes arithmetic shift-right of integers that rounds to nearest integer with halfway rounding toward zero behavior. An answer can be a proper ...
0
votes
1
answer
145
views
A single byte that is from 0 to 255 to be rescaled from 0 to 7
A single 1 byte could be from 0 to 255. I intend to rescale it to be from 0 to 7. I have done this:
bit8 := uint8(136)
// To re-scale 1 byte from 0 to 2^3-1 i.e. 0 to 7
bit3 := bit8 / 0xff * 0b0111
...
1
vote
1
answer
110
views
What is wrong with my derivation of this carry flag result for SUB?
To be clear, I am certain that the derivation below is incorrect, but I am hoping someone can point out where in my "proof" I am wrong.
I am trying to prove that the x86 instruction setb ...
5
votes
4
answers
341
views
What is the fastest way to add 16 small numbers
I have two arrays, a and b. Each contain 16 bytes and I would like to add each b[i] to their corresponding a[i]. The arrays do not overlap and also I know that the resulting sums always fit in a byte ...
0
votes
0
answers
46
views
MIPS not transferring values properly between registers (adding 28 for no reason)
I'm writing a program in assembly that simulates multiplication via iterative addition. I have the algorithm done, and for my first test case, I set my multiplicand to 50. Upon transferring this value,...
4
votes
1
answer
98
views
Efficient 63-bit long computation of (a*b)/c in Java
I would like to compute (a*b)/c in Java in the most efficient way where:
a, b and c are unsigned long values (guaranteed to fit in 63 bits, i.e. with zero sign bit)
The result is also guaranteed to ...
-1
votes
1
answer
175
views
Subtract unsigned ints into signed long result. Never negative?
Found a bug in my code and noticed the following, when subtracting 2 numbers like
unsigned int a, b (with b > a)
signed long result = a - b
The result is always positive, even though b is larger. I ...
-3
votes
4
answers
273
views
How to divide a nonnegative variable integer by a constant fixed-point rational ≥1 without overflow (at all, if possible)?
How to compute ⌊𝑥÷𝑐⌋ for a variable integer 𝑥 and a fixed-point rational number 𝑐≥1 precisely? The input 𝑥 ∈ [0, INT_MAX] is stored in a variable of type int, 𝑐 ∈ [1, INT_MAX] is given as a ...
1
vote
0
answers
136
views
GMP Library: unsigned/signed arithmetics
I am trying to do some computations on big numbers, using the GMP library.
I am trying to understand how could I indicate GMP to use either signed or unsigned arithmetics.
Let's say that I have two &...
1
vote
2
answers
155
views
Emulate Javascript's 32bit signed integer arithmetic to C (or Perl) - some discrepancies
I am trying to translate simple JS code to C and/or Perl but I found a discrepancy in behaviour when doing arithmetic operations (+ - * / << >>) on integers and the result overflows. I ...
7
votes
3
answers
2k
views
Safely check if signed multiplication would overflow in C++17
I'm looking for a convenient idiom to check if a multiplication of two signed integers (longs or long longs) would overflow since signed arithmetic overflow is undefined behavior in C and C++. I'm ...
2
votes
1
answer
104
views
Logarithm of a positive number result in minus infinity python
I have this image:
HI00008918.png
I want to apply a logarithmic function (f(x) = (1/a)*log(x + 1), where a = 0.01) on the image...
So this is the code:
import numpy as np
import matplotlib.pyplot as ...
0
votes
0
answers
75
views
Matching arithmetic expressions in SWI-Prolog?
I have an arithmetic expression containing Var, and I need to match it to the following format:
Step * Var + Offset
If it can be represented in this form, I need to get Step and Offset values. If not, ...
-2
votes
2
answers
286
views
Can one emulate bitwise arithmetic "and" or "or" using only non-bitwise operations?
Imagine we are tasked with using an idealized scientific calculator which has the following properties:
no direct support for bitwise operations, or for base conversions (eg cannot simply "...
2
votes
1
answer
223
views
What is the minimum odd number representable in a 32-bit float, written in both decimal and binary?
What is the minimum odd number that can be accurately represented with a float? Assume that the float is represented with 32 bits where: 1 bit is used for the sign, 8 bits for the exponent and 23 bits ...
0
votes
1
answer
209
views
rust relative array indexing
I'm a fairly advanced programmer (experience in Java, Python, C#, C, C++) who is now trying to learn Rust for the first time.
Rust is quite different from any language I've tried before, so I'm ...
0
votes
2
answers
67
views
What does mean ARR in the half place in C?
I have code :get_transaltion(&(arr[1/2]) )
The array is of structures that each contain an array of 3 places, which places does the function accept?
I edited the array in the first place in the ...
1
vote
1
answer
68
views
Why is X staying = 1? [closed]
while 1:
x=+1
pic = pyautogui.screenshot()
pic.save(str(x)+".png", "PNG")
print(x)
time.sleep(5)
X won't increase every time the loop, loops
I want the ...
0
votes
2
answers
4k
views
Python Overflow Warning in Scalar Add
I want to perfom a simple operation: b+g+r b,g,r variables are integrers and have value below 256. I get Overflow Warning and wrong results. The problem is that the operation is simple(result not ...
1
vote
0
answers
53
views
Does (++i)+(++i) have the defined behavior in Java [duplicate]
int i=1;
int n = (++i)+(++i);
In C++ the above code has undefined behavior. I want to know in Java, does it have defined behavior and give the expected answer n=5 in this case. In Java, the order of ...
1
vote
1
answer
331
views
Why did Matlab choose saturation arithmetic for integers?
Matlab implements saturation arithmetic instead of wrapping on overflow for integers. While understanding the difference between the two, but not necessarily finer nuances, I wonder what was the ...
5
votes
1
answer
145
views
Unexpected behaviour on underflow of unsigned char
The following program:
#include <iostream>
#include <string>
int main ()
{
unsigned char result1 {0};
unsigned char result2 {0};
result1 = (result1 - 1) % 8;
result2 = ...
1
vote
2
answers
233
views
How to find the biggest possible combination of two numbers under a certain threshold from a list? [closed]
So let's say if the list is [2,5,14,18,44], what I want to find out is what the biggest sum of two numbers under 60 would be. So the correct answer should be (14,44)
Right now I am calculating every ...
3
votes
1
answer
226
views
Signed Word to Integer Conversion in Lisp
I'd like some help in understanding and fixing an SBCL compiler note that says:
; in: DEFUN PRINT-SEARCH-PROGRESS-GRAPH
; (- (1+ WOULDWORK-PKG::*N*)
; (LENGTH WOULDWORK-PKG::*L*))
;
; note: ...
-2
votes
3
answers
88
views
What is happening during this operation (0x99>>j) & (0x80>>i) [closed]
i've been tinkering around, and i have googled and searched about bitwise operations.
and i think i understand some of it, however i have a piece of code i have copied from someone online which allows ...
-2
votes
1
answer
1k
views
I was doing a code for , "Count Digits " , Evenly divides means whether N is divisible by a digit i.e. leaves a remainder 0 when divided
#include<bits/stdc++.h>
using namespace std;
int main() {
int n,m,z;
cout<<"enter n: ";
cin>>n;
z=n;
int count=0;
while(n>0){
m = n % 10;
if(z%...
0
votes
2
answers
87
views
Can't seem to add summation in while loop [duplicate]
I was making a code that adds a summation for a specific formula, but the sum is always 0 for some reason. What is the reason nothing is adding? I think maybe it is the declaration of int and double ...
0
votes
1
answer
75
views
Why is the result of my int multiplication wrong?
I have the the following code where the results exceeds the limit an integer type variable can store and need to understand why I am getting this result (268,435,456=2^28)
public static void main(...
2
votes
3
answers
80
views
Can't we convert string into Integer and add it to other Integers at the same time and check in a if condition if it equals another integer?
I was solving a question on Codechef and I encountered this problem.
Here is the link to the question. https://www.codechef.com/LP0TO101/problems/FLOW013
Basically we are given three angles and we ...
0
votes
4
answers
114
views
I'm struggling with this question about arithmetic
The function must return the sum of all integers between from and to including these numbers.
For example, arithmeticSum(2,4) should be 9 because 2+3+4 = 9.
This is the code right now, i can't change ...
0
votes
0
answers
265
views
PowerShell : Rename files names with math operation, arithmetics (+, -, *, /, %, [math]::floor etc.)
I would like to rename files names in folder using math operation.
The simplest example for multiply *2 of n-th element doesn't work:
cd c:\my_folder\
$n = 38
ls | Sort-Object LastWriteTime | %{Rename-...
0
votes
2
answers
142
views
How does Java handle precedence of operators when unary operators are involved?
I have the following code in Java :
int a=5,b=10,c;
c = ++b * ++a - a++ ;
System.out.println(c);
Now referring to the Java operators precedence table :-
Table
We will first evaluate the postfix a++ ...
65
votes
4
answers
8k
views
Compiler optimizations may cause integer overflow. Is that okay?
I have an int x. For simplicity, say ints occupy the range -2^31 to 2^31-1. I want to compute 2*x-1. I allow x to be any value 0 <= x <= 2^30. If I compute 2*(2^30), I get 2^31, which is an ...
0
votes
2
answers
47
views
Divide integer by qualifying rows and add to column values
I need the variable $tsp to be evenly added to the value in the "count" field, but only in those fields where the "count" is greater than 0 .
$tsp = 9;
$fin = [
"1701"...
0
votes
2
answers
938
views
I got a negative number while trying to return a long long value [duplicate]
I created a function seriesSum to return a sum of the series of a number, and I used long long return data type but it returns a negative number if I insert for example 46341 output will be -...
3
votes
3
answers
159
views
Assigning a variable during arithmetic in Java?
My professor gave us this java snippet during a lecture and I don't understand why it outputs 12.
int b = 9;
b = b + (b = 3);
System.out.println( "b = " + b );
My thinking is that since ...
0
votes
1
answer
213
views
I just started learning 'C'. I keep getting "collect2.exe: error: ld returned 1 exit status" when I try to run the following code on the terminal
I wrote this code to find out the average of any 3 numbers. Please help me out. I'd like to know where it went wrong. I wrote the same code in a new file and that was executed without any issues.
This ...
0
votes
0
answers
33
views
different flags being set for the same arithmetic? [duplicate]
Im currently learning 8086 assembly at school, im using emu8086, i was playing around with some signed arithmetic and this occured.
if i use the SUB instruction to subtract 15(0FH) from -16(F0H) with ...
-3
votes
1
answer
454
views
What are hybrid-integer? [closed]
what are Hybrid Integers mentioned in this problem and what are applications of Hybrid Integers?
0
votes
2
answers
266
views
Generic razor component allowing arithmetic operations
I want to create a razor component allowing to enter int or float numbers and increment or decrement them using a spin control. However, I have no idea to tell the Blazor generator / C# compiler how ...
0
votes
1
answer
448
views
Compile time check that lcm(a,b) doesn't overflow
I would like to have a compile-time check that taking the least common multiple of two numbers doesn't overflow. Difficulty: regarding std::lcm,
The behavior is undefined if |m|, |n|, or the least ...
7
votes
1
answer
3k
views
Time it takes to square in python
I was wondering whether x**2 or x*x is faster
def sqr(x):
for i in range (20):
x = x**2
return x
def sqr_(x):
for i in range (20):
x = x*x
return x
When I time it, ...
2
votes
0
answers
60
views
Is there a C++ library that wraps a internal type to be able to derive from that internal type?
I have this template that wrap an unsigned integer type (any) :
template<std::unsigned_integral T>
struct base_dig_t
{T m_data;};
Then I have this template to modify the behavior ...
-1
votes
1
answer
71
views
triangle area calculator, It keeps showing 0, why?
I am making a triangle area calculator, but it only show 0 instead of answer, It must be the formula 1/2 * ab sin c , can someone tell me what should I change to make it work.
#include <stdio.h>
...
2
votes
1
answer
495
views
Distinction between positive and negative overflow in C# arithmetic operation
I am performing arithmetic operations on integers in a checked scope in C#/.NET, in order to catch when an overflow happens. I want to find out if the overflow was positive or negative, in a short, ...
1
vote
6
answers
210
views
last digit is off by 1 regardless of math methods used
Having a minor issue with the return value of this function for large parameters (works fine for smaller ones). Here is the code:
def pyramid_blocks(n, m, h):
return int(h*n*m + (h*h-h)*(3*n+3*m+2*h-...