1

I noticed that SetPrecision doesn't actually guarantee that my number is as precise as I want:

prec=20;    
SetPrecision[1.0, prec]
SetPrecision[1.1, prec]

Out[666]= 1.0000000000000000000

Out[667]= 1.1000000000000000888

I know one way to prevent this would be to replace 1.1 with 11/10, but this gets pretty cumbersome when adding more digits after the decimal. Another way is to use 1.1`20 (thanks @J.M.) but I need to use a variable precision. Is there a better way?

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
Max
  • 1,050
  • 6
  • 14
  • 1
  • @J.M. That works if you're not using a variable for precision (i.e. SetPrecision[1.1, prec]), but in my case I am. Your answer would be fine if 1.1`prec was a thing. See a previous question of mine: http://mathematica.stackexchange.com/questions/89837/is-there-a-way-for-accuracy-backtick-notation-to-accept-variable-inputs – Max Jun 08 '16 at 04:19
  • 1
    You should have clarified what you wanted then; there is no indication that you want such a thing in your question. In any case: you might want to consider Rationalize[], but that doesn't work in general. – J. M.'s missing motivation Jun 08 '16 at 04:22
  • Clarified my question. Rationalize[] actually works great! I'll only be using finite decimal places of course. – Max Jun 08 '16 at 04:34
  • 1
    Possibly informative: https://www.youtube.com/watch?v=PZRI1IfStY0 – QuantumDot Jun 08 '16 at 12:25

1 Answers1

2

While not being a Mathematica expert, I assume that everything works as expected: your 1.1 "actually is" 1.1000000000000000888 since it is probably stored with a binary form in the IEEE754 standard (count the number of exact decimal digits and remember that IEEE754 standard with double-precision type has 15/16 exact decimal digits). See http://mathworld.wolfram.com/Floating-PointArithmetic.html

You can later (even in the same line!) increse the precision as much as you want but your 1.1 already is 1.1000000000000000888 and the displayed result is exactly what it should.

If this is really an issue for you, you should avoid using floating-point numbers (even when typing them in your script) either by using fractions or maybe by using decimal digits in strings and converting them directly from strings (but it is much slower).

Thomas Baruchel
  • 216
  • 2
  • 5