Last active
August 29, 2015 14:26
-
-
Save ms-tg/88dff77d281c3c70c6cb to your computer and use it in GitHub Desktop.
In response to Slack post "Please explain why Doubles work this way"
This file contains hidden or 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
// In response to Slack post "Please explain why Doubles work this way" | |
// | |
// For doubles... | |
// 1.001484375 - 1 = 0.0014843749999999822 | |
// 0.001484375 + 1 = 1.001484375 | |
// | |
// To run, start the repl with `sbt console`, then paste all this in | |
// | |
import java.lang.Double.doubleToRawLongBits | |
import java.lang.Long.{toBinaryString, toHexString} | |
val l1 = List[Double]( | |
1, | |
1.001484375, | |
1.001484375 - 1, | |
0.001484375, | |
0.001484375 + 1) | |
val l2 = l1.map(doubleToRawLongBits) | |
val l3 = l2.map(toHexString) | |
val l4 = l2.map(toBinaryString) | |
List(l1, l2, l3, l4).foreach(_.foreach(println _)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment