Kahan summation algorithm, also known as compensated summation and summation with the carry algorithm, is used to minimize the loss of significance in the total result obtained by adding a sequence of finite-precision floating-point numbers. This is done by keeping a separate running compensation (a variable to accumulate small errors).
Reason for Loss of Significance:
- As we know in Java language we have two primitive floating-point types, float and double, with single-precision 32-bit and double-precision 64-bit format values and operations specified by IEEE 754. That is, they are represented in a form like:
SIGN FRACTION * 2EXP
- For example, 0.15625 = (0.00101)2, which in floating-point format is represented as: 1.01 * 2-3. However, not all fractions can be represented exactly as a fraction of a power of two. For example, 0.1 = (0.000110011001100110011001100110011001100110011001100110011001… )2 and thus cannot be stored inside a floating-point variable.
- Therefore, floating-point error/loss of significance refers to when a number that cannot be stored as it is in the IEEE floating-point representation and repetitively some arithmetic operation is performed on it. This leads to some unexpected value and the difference between the expected and the obtained value is the error.
Below is an implementation that simulates the significance error:
Note: The expected value for the above implementation is 1.0 but the value returned is 0.9999999999999999. Therefore, in this article, a method to reduce this error by using Kahan’s Summation algorithm is discussed.
Kahan Summation Algorithm: The idea of the Kahan summation algorithm is to compensate for the floating-point error by keeping a separate variable to store the running time errors as the arithmetic operations are being performed. This can be visualised by the following pseudocode:
function KahanSum(input) var sum = 0.0 var c = 0.0 for i = 1 to input.length do var y = input[i] - c var t = sum + y c = (t - sum) - y sum = t next i return sum
In the above pseudocode, algebraically, the variable c in which the error is stored is always 0. However, when there is a loss of significance, it stores the error in it.
Below is the implementation of the above approach:
Normal sum: 0.9999999999999999 Kahan sum: 1.0
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- MO's Algorithm (Query Square Root Decomposition) | Set 1 (Introduction)
- A* Search Algorithm
- DDA Line generation Algorithm in Computer Graphics
- Line Clipping | Set 1 (Cohen–Sutherland Algorithm)
- Bresenham’s Line Generation Algorithm
- Mid-Point Line Generation Algorithm
- Point Clipping Algorithm in Computer Graphics
- Commonly Asked Algorithm Interview Questions | Set 1
- Anti-aliased Line | Xiaolin Wu's algorithm
- Mid-Point Circle Drawing Algorithm
- Exact Cover Problem and Algorithm X | Set 1
- Jump Pointer Algorithm
- Exact Cover Problem and Algorithm X | Set 2 (Implementation with DLX)
- Shortest Path Faster Algorithm
- Peterson's Algorithm in Process Synchronization
- Program for SSTF disk scheduling algorithm
- Banker's Algorithm in Operating System
- Dekker's algorithm in Process Synchronization
- Freivald’s Algorithm to check if a matrix is product of two
- Extended Mo's Algorithm with ≈ O(1) time complexity
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : mohit kumar 29