0

I'm new to scheme and I was a little surprised to see what the compiler returns for (- 112 (* 56 (- 3 1.1))) Obviously the answer is 5.6, but I get 5.60000000000001 on my machine (guile) and 5.6000000000000085 using an online compiler (https://repl.it/languages/scheme). Is this normal? Why is this happening?

mrtnmgs
  • 1,402
  • 14
  • 26
  • 1
    Yes. Mind that even though you are writing 1.1 in base 10 your computer does arithmetic in base 2. This happens in all languages so it has nothing to do with Scheme and more to do with [how computers do floating point arithmetic](https://youtu.be/PZRI1IfStY0) – Sylwester Jul 28 '16 at 01:39
  • 1
    If it's helpful for you at all, I also wrote a "pizza cutter analogy" for binary floating numbers here: http://stackoverflow.com/a/27030789/13. In this analogy, you can only halve pizza slices (and add up slices in any combination you like). You will find that it is **impossible** to create 1.1 using any combination of slices, because no combination of slices will create exactly 0.1. – C. K. Young Jul 28 '16 at 02:03
  • 4
    The standard solution to this, in Scheme, is to use rational numbers. So instead of using `1.1`, use `11/10`. Then it doesn't touch floating-point at all. – C. K. Young Jul 28 '16 at 02:10

0 Answers0