1

I run these lines:

a = 0.833
SetPrecision[a, 20]

and this is the output:

0.833

0.83299999999999996270

I expected to receive

0.83300000000000000000

Do you have any suggestion? Thank you!

Michael E2
  • 235,386
  • 17
  • 334
  • 747
user63612
  • 103
  • 5
  • 2
    0.833'20 gives what you expect, but SetPrecision[a,20] is equivalent to N[FromDigits[RealDigits[a,2],2],20] = 0.83299999999999996270. MMA works with binary numbers. – Alx Aug 25 '19 at 16:40
  • It works with 0.833'20, but not with a'20 – user63612 Aug 25 '19 at 17:59
  • The back tick is used in specifying numeric input. Using it on variables thus a`20 is not syntactically valid. The problem is that a = 0.833 evaluates 0.833 in machine precision binary and obtains the binary floating point number nearest to 0.833, but that number is only approximately equal to 0.833. – Michael E2 Aug 25 '19 at 20:34
  • Related: https://mathematica.stackexchange.com/questions/55292/a-problem-about-function-n – Michael E2 Aug 25 '19 at 20:38
  • 1
    You don't need a'20. Just use a=0.833'20 and a will have 20 digits of precision from then on or until it is reassigned. That is supposed to be a back tick, but I can't write it that way because of the way comments treat back ticks. – Bill Watts Aug 25 '19 at 21:58
  • @BillWatts Enclose single backticks within double backticks to get the backtick show. – Michael E2 Aug 25 '19 at 22:23
  • @Michael E2 Thanks. – Bill Watts Aug 25 '19 at 23:07
  • N[Rationalize[0.033], 20] – Andreas Lauschke Jan 22 '20 at 23:59

1 Answers1

1

I suggest SetPrecision[833/1000 , 20] or SetPrecision[.833 // Rationalize, 20].

Rohit Namjoshi
  • 10,212
  • 6
  • 16
  • 67
Somos
  • 4,897
  • 1
  • 9
  • 15