Pure Programmer
Blue Matrix


Cluster Map

Project: Floating Point Error

Demonstrate the limitations of floating point math by computing 1/3 and storing it in a double variable. Then print the variable with 1/3 and the result of multiplying that variable by 3, 9 and 300 to sixteen decimal places. Do we get the exact results we expected? How do we get this result when 1/3 is infinitely repeating in its decimal form 0.3333...?

Try a similar experiment by assigning 0.2 to a variable. Then with a loop, add it 1000 times to another summation variable that was initialized with 0.0. Print the two-tenths variable, the final summation variable and the two-tenths variable times 1000, all to sixteen decimal places. Do we get the exact answer of 200.0 that we expect in both cases? Why does multiplication result in a better answer than repeated addition?

See [[Floating-point_arithmetic#Accuracy_problems|Floating Point Arithmetic Accuracy Problems]]

Output
$ swiftc FloatingPointError.swift -I . -L . -lUtils error: link command failed with exit code 1 (use -v to see invocation) ld: library not found for -lUtils clang: error: linker command failed with exit code 1 (use -v to see invocation)

Solution