Created
September 6, 2016 05:12
-
-
Save aurorapar/142413bf3d76218c400871db4fd8aba8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Aurora Pariseau | |
Floating point arithmetic | |
*/ | |
import java.util.*; | |
class main | |
{ | |
public static void main (String args[]) | |
{ | |
float sum = 0.0f; | |
float sum2 = 0.0f; | |
for(float n = 1.0f; n <= 150000.0f; n++) | |
{ | |
if(n == 3) | |
{ | |
System.out.println("sum: " + sum); | |
System.out.println("sum after: " + (sum + 1.0f/n)); | |
System.out.println("difference: " + ((sum + 1.0f/n) - sum)); | |
} | |
if(150001-n == 3) | |
{ | |
System.out.println("sum2: " + sum2); | |
System.out.println("sum2 after: " + (sum2 + 1.0f/(150001-n))); | |
System.out.println("difference: " + ( | |
(sum2 + 1.0f/(150001-n) - sum2)) | |
); | |
} | |
float value = 1.0f/n; | |
float value2 = 1.0f/(150001-n); | |
sum += value; | |
sum2 += value2; | |
} | |
System.out.println(sum + " " + sum2); | |
} | |
} | |
/* | |
As seen above, there is a rounding error in the arithmetic based on the | |
precision of the leading number. This accounts for the difference between | |
sum and sum2 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment