Skip to content

Instantly share code, notes, and snippets.

@ms-tg
Last active August 29, 2015 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ms-tg/88dff77d281c3c70c6cb to your computer and use it in GitHub Desktop.
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"
// 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