659 questions
2
votes
1
answer
183
views
Is it possible to call x86 `idiv r/m8` from MSVC2022 using C++ directly?
I am trying to call idiv r/m8 using MSVC2022's current Microsoft Visual C++ Compiler 17.4.33403.182 and C++.
Using the most straightforward approach:
struct cdiv_t
{
// char
std::int8_t quot;
...
2
votes
2
answers
169
views
Where is GCC's __divti3 defined?
For GCC's __int128 type
__int128 div128(__int128 a, __int128 b) {return a / b;}
GCC will generate a call to __divti3 instead of inline assembly. So where is this actually defined? Github's search ...
6
votes
1
answer
265
views
Speeding up integer division with doubles
I have a fixed-point math-heavy project and I was looking to speed up integer divisions. I tested double division with SSE4 and AVX2 and got nearly 2x speedup versus scalar integer division. I wonder ...
1
vote
0
answers
142
views
How can I handle overflow in bigint / int signed division with x86 idiv?
I have a serious problem I really want to solve. I am trying to implement void operator divide(uint32_t dividend[4],int32_t divisor,uint32_t "ient[4],int32_t &remainder). My algorithm is:
...
2
votes
0
answers
50
views
record accumulate round error for integer divisions to reduce final error
It is well-known that integer division can result in errors, no matter which rounding method is used (we use Floor rounding below as example).
Moreover, if division is calculated multiple times (with ...
4
votes
2
answers
232
views
How to get specified number of decimal places of any fraction?
So I can generate many tuples like this:
(...
0
votes
0
answers
145
views
Question about Knuth's Algorithm D for division
I want to implement Knuth's Algorithm D for division on C (Link to Knuth's book, p272). In step "D4. [Multiply and subtract.]", it states:
if the result of this step is actually negative, (...
33
votes
1
answer
4k
views
Why does a = a * (x + i) / i; and a *= (x + i) / i; return two different results?
I am calculating the number of routes possible in a 20 * 20 grid from top left to bottom right when only down and right moves are possible.
I read about calculating the central binomial coefficient to ...
1
vote
1
answer
123
views
Bug in the normalization step of Knuth Algorithm D (TAOCP 4.3.1)?
Knuth Algorithm D, during the normalization step (D1), states to set d to (b-1)//v_hi where b is the basis (word size or half-word size) and v_hi is the upper limb of the denominator. Then multiply ...
1
vote
0
answers
108
views
Why Java's int division rounds to zero? [duplicate]
I've found that the rule for rounding two int in Java is to get the value closer to 0.
Which mean that if you divide two positive numbers, you get the "floor" value e.g 7/3=2.
And if you ...
1
vote
1
answer
59
views
Overflow error in VBA calculation (division)
In the macro below I get an Overflow error when calculating Rate = cell.Offset(0, 7).Value / cell.Offset(0, 2).Value - regardless of whether Rate is declared as Long or Double. Kindly help to identify ...
0
votes
1
answer
108
views
Divide 8-byte stored in 2 uint32 by a uint32 it on a machine with 32-bit operation
I want to divide uint64 by a uint32 in WebGPU, which currently only handles uint32 operations. So I am trying to simulate this by storing my long into two uint32 buffers high and low. and now I want ...
1
vote
0
answers
45
views
Why is floor division inconsistent with division follow by floor, e.g. 1//0.2 = 4.0 [duplicate]
The operator // for integer division is called "floor division" in the python documentation, but comparing to other floor implementations there is a discrepancy, for example:
>>> ...
5
votes
1
answer
4k
views
Different types of Integer division in Python
In terms of the resulting value (ignoring the resulting data type), are the following the same in Python if x and y are both numbers?
int(x / y)
x // y
If so, which is better to use in real ...
1
vote
1
answer
1k
views
Restoring division algorithm in Risc V
I wanted to implement the restoring division algorithm (without using the div operation) in RISCV assembly language.
The algorithm I have to implement is the following
.
I implemented this, but it is ...
0
votes
0
answers
29
views
Canonical way to ensure float point division across py2 and py3? [duplicate]
I have a function that needs to support both py2 and py3, unfortunately.
Currently, there's a division op:
n = a / b
a and b are usually whole numbers, but represented as floats, but from what I can ...
4
votes
1
answer
234
views
Optimized 53->32 bit modulo computation on 32-bit processors
This question arose when I looked into the efficient implementation of the MRG32k3a PRNG on 32-bit processors without, or slow, support for double computation. I am particularly interested in ARM, ...
3
votes
1
answer
182
views
non-restoring division: how to avoid code bloat for divisor MSB set?
Revisiting 32-bit integer division on AVR 8-bit processors, I tried to code unsigned non-restoring division. Not thinking it looks too bad, there is one problem:
With a divisor with highest bit set, ...
0
votes
3
answers
291
views
How to return integer part using backslash when dividing two decimals in Visual Basic
I am trying to divide two decimals and want to get the exact integer part of the result only using a\b. Consider the following example:
Dim a, b As Integer
a = 200
b = 2
...
-3
votes
4
answers
267
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
2
answers
69
views
Division produces unexpected results [duplicate]
id client_id driver_id city_id status request_at
1 1 10 1 completed 2023-12-23
2 2 11 1 cancelled_by_driver 2023-12-23
3 3 12 6 completed 2023-12-23
4 4 13 6 ...
1
vote
0
answers
164
views
Maximize the length of a sequence of numbers having certain properties
Given a positive integer n, I wish to find the longest unique sequence of positive integers, none greater than n, such that for every pair of adjacent numbers in the sequence one is an integral ...
1
vote
1
answer
305
views
Catching / disabling SIGFPE exceptions from assembly
I get a SIGFPE exception when dividing by 0 in x86 assembly using idiv. How would I disable it from assembly? Do I need a syscall or can it be done directly in x86?
Reproduction:
test.asm
default rel
...
2
votes
1
answer
97
views
Why is my variable returning a large negative number when dividing?
I'm tackling the credit problem from cs50 and I'm baffled by the for loop on one section because I'm dividing a long integer 4003600000000014 by 100 and it returns a large negative number -1685133312.
...
1
vote
1
answer
78
views
How to convert a binary number into decimal and show the answer in 3 digits?
I have this piece of code from my project. It is taking all the amount from the transaction and adding it and showing how much you earned in the day. The problem is it's not showing in decimal format ...
6
votes
1
answer
4k
views
Integer division in PowerShell
What is the easiest way to do integer division in PowerShell? So that the arguments are integers and the result is also an integer.
In C++ it would be e.g. 115/10.
0
votes
2
answers
381
views
Nextion: Calculate inverse tan (arctan) without trig functions or floating point
I am programming an interface on this Nextion display. Specifically, I am trying to program a digital dial that rotates around a centre point.
These screens are great, but they don’t have any trig ...
0
votes
2
answers
587
views
Divide by variable power of 2 using assembly
I've got this task:
dividePower2
Compute x/2n, for 0 ≤ n ≤ 30. Round toward zero.
Argument 1: x
Argument 2: n
Examples:
dividePower2(15,1) = 7
dividePower2(-33,4) = -2
This is what I've got so far ...
1
vote
1
answer
835
views
Convert HEX to decimal in assembly 8086
I am trying to create a code which would take as input 1 8-bit number in HEX form and produce the decimal equivalent.
Here is my code:
data segment
msg1 db 10,13, "Give me a valid hex ...
-3
votes
1
answer
92
views
I found this syntax for printing prime numbers and l am quite confused. I would like this community to clarify
Nums = range(1,60)
def is_prime(Nums):
for x in range(2, Nums):
if (Nums%x) ==0:
return False
return True
prime_numbers = list(filter(is_prime,Nums))
Print(prime_numbers)...
0
votes
0
answers
5k
views
Restoring division algorithm for signed numbers
I am trying to follow the division algorithm for signed numbers (2's complement representation) based on the algorithm described here. This is the standard restoring method with M and AQ registers ...
6
votes
5
answers
653
views
Implementing banker's rounding for integer division
What would be the simplest formula to implement int divround(int a, int b) {...}, in C, where output is a/b with banker's rounding (round half to even)?
For example, divround(3,2) and divround(5,2) ...
2
votes
1
answer
381
views
uint64_t: Division with rounding
I'm trying to create a code which divides a uint64_t by another uint64_t plus it applies rounding to the result. The code should be as fast as possible and work for all inputs (e.g. I would prefer it ...
0
votes
0
answers
78
views
gcc: How is integer division optimized?
I've seen that gcc does a great job in optimizing integer divisions with a constant divisor.
E.g.
uint64_t f(uint64_t x)
{
return x / 1000;
}
uint64_t f2(uint64_t x)
{
return x / 10000;
}
...
2
votes
1
answer
71
views
Do you convert signed integers to hex before doing an `idiv` in 64-bit Intel CPUs?
Suppose you want to divide -198077031546722079779 by 23 using gas. As this dividend is larger than %rax, how do you put it in %rdx:%rax? All the books I read on assembly avoid illustrating idiv with ...
1
vote
0
answers
57
views
Signed Number Division 12 / 10
Based on the method in this question Using 2's Complement to Perform Binary Division for Signed Number , I tried to calcuate 12 / 10, which is 0000 1100 / 0000 1010 , but with its method I got ...
3
votes
1
answer
108
views
Can the x87 perform exact division on UNsigned QUADword integers?
The 8086/8087/8088 MACRO ASSEMBLY LANGUAGE REFERENCE MANUAL (c) 1980 Intel Corporation mentions (Emphasis mine):
... the 8087 provides a very good approximation of the real number system. It is ...
0
votes
1
answer
165
views
Need help fixing calculator's multiplication and division in assembly language
Division and Multiplication part doesn't work. It jumps in to the prompt of the 2nd input number.
I ran the code the addition and subtraction part work, whereas the division and multiplication doesn't ...
0
votes
0
answers
161
views
Why isn't my IDIV instruction working as expected on Windows x64 using NASM assembler? [duplicate]
I tried using the below assembly code to get the quotient and remainder for IDIV instruction, but it didn't work as expected. I use NASM as the assembler.
It prints "The quotient is: -...
0
votes
0
answers
86
views
How do you manually calculate -8 idiv -4?
I got my answer of imul How do you manually calculate imul -1 * 3? but now run into idiv.
For unsigned division, I simply use long division and can get the correct result.
50/4:
1100
...
0
votes
4
answers
345
views
Make sure if a value in register is a multiple of 3 in AVR Studio using assembly
I want to make sure if a number in register is a multiple of 3 using AVR Studio and assembly language but on AVR ATmega 8515 so there is no div instruction/syntax
I already tried a couple method like ...
0
votes
2
answers
87
views
Why does division "/" and floored division "//" operators in python gives different results for divisions with a remainder = 0?
When dividing somewhat large integers with numbers that divides them, I get different results from division / and floored division //.
for instance:
In [1]: a = 123456789012345678
In [2]: int(a/2)
...
1
vote
1
answer
706
views
Powershell: Issues with 'Write-Progress"
I am making a script to backup databases in SSMS from Powershell. I want to add a progress bar since these databases take some time to backup. This is what I have. The progress bar is popping up but ...
1
vote
1
answer
69
views
Decimal representation of a number that doesn't fit in one register in NASM
If we have a number in a register and we need to get its decimal digits values - it's easy. All we need is divide the number by 10 and save the remainders we get.
lp: cmp eax, 0 ; have we ...
-3
votes
1
answer
141
views
Assembly language Division (Problems is getting the remainder)
jmp start
mess1 db 'Enter 1st number: $'
mess2 db 0a,0d, 'Enter 2nd number: $'
nextline db 0a,0d, '$'
start:
mov ax, 03
int 10h
mov dx, offset mess1
call printstring
call input
...
0
votes
2
answers
95
views
Integer division problems [duplicate]
Is it a bug, or there's some official document that says, that this was supposed to happen??
Because my C program worked very well.
0
votes
1
answer
255
views
Cortex M0+/GCC Division with Rounding
I'm trying to implement an integer division with rounding. Obviously, by default integer division does floor and I was thinking I could use the remainder to determine if I should add 1 to my result.
...
2
votes
0
answers
140
views
How do constants for signed division differ from unsigned division when using reciprocal multiplication in assembly?
While using Godbolt Compiler, I was playing around with various functions when I checked what signed division looks like using the following code:
int div10(int num) {
return num / 10;
}
Where ...
0
votes
3
answers
914
views
Dividing two numbers and printing the result and adding one to the result if there is a remainder, without using if-statements or imports or function? [duplicate]
I'm trying to divide number into groups in plain python without importing or the use of if-statements, and get the division into groups + remainder forming one extra group so that 200 / 99 would be 3, ...
-1
votes
3
answers
339
views
Dividing given amount of people into same size groups with remainder forming 1 more group on top, Without using if-statements
So I am trying to figure out how to divide the given amount of people into given group sizes for example:
11 people into groups of 3, and the program giving the answer 4, since the remainder should ...