Why do binary arithmetic operations sometimes result in different outputs?

  • Thread starter Thread starter megaflop
  • Start date Start date
  • Tags Tags
    Binary Error
AI Thread Summary
Binary arithmetic operations can yield different results due to the properties of integer division, particularly when odd integers are involved. When computing A + B versus 2 × (A/2 + B/2), the second operation can lead to discrepancies because of how binary addition handles carry bits, especially with odd numbers. The discussion highlights that integer division truncates towards zero, which can affect the outcome when odd integers are divided. This behavior is consistent across binary-coded integers. Understanding these nuances is crucial for accurate binary arithmetic computations.
megaflop
Messages
10
Reaction score
0
Hello everyone !
I got a problem, I can't figure out why when I compute A+B and 2\times (\frac{A}{2}+\frac{B}{2}) the result is sometimes different.
Can anyone explain why the results of the two operations are different ?

Edit : A and B are coded in binary of course and we compute a binary addition.
 
Engineering news on Phys.org
Are you using integer or floating point ?

My guess is former , and one of the numerators is an odd number.
 
Integers I forgot to mention sorry.
 
Well there you go.

Got Basic?

FOR I = 1 , 10 , 1
PRINT I, INT( I/2)
NEXT I
END
 
jim hardy said:
Well there you go.

Got Basic?

FOR I = 1 , 10 , 1
PRINT I, INT( I/2)
NEXT I
END

I see thank you :) .
 
megaflop said:
Integers I forgot to mention sorry.
Does only "misbehave" if you're using one or more odd integers? :rolleyes:
 
Very basic question. Consider a 3-terminal device with terminals say A,B,C. Kirchhoff Current Law (KCL) and Kirchhoff Voltage Law (KVL) establish two relationships between the 3 currents entering the terminals and the 3 terminal's voltage pairs respectively. So we have 2 equations in 6 unknowns. To proceed further we need two more (independent) equations in order to solve the circuit the 3-terminal device is connected to (basically one treats such a device as an unbalanced two-port...
suppose you have two capacitors with a 0.1 Farad value and 12 VDC rating. label these as A and B. label the terminals of each as 1 and 2. you also have a voltmeter with a 40 volt linear range for DC. you also have a 9 volt DC power supply fed by mains. you charge each capacitor to 9 volts with terminal 1 being - (negative) and terminal 2 being + (positive). you connect the voltmeter to terminal A2 and to terminal B1. does it read any voltage? can - of one capacitor discharge + of the...
Thread 'Weird near-field phenomenon I get in my EM simulation'
I recently made a basic simulation of wire antennas and I am not sure if the near field in my simulation is modeled correctly. One of the things that worry me is the fact that sometimes I see in my simulation "movements" in the near field that seems to be faster than the speed of wave propagation I defined (the speed of light in the simulation). Specifically I see "nodes" of low amplitude in the E field that are quickly "emitted" from the antenna and then slow down as they approach the far...
Back
Top