Wei Shi
Wei Shi

Reputation: 5055

get arbitrary precision when input contains floating point in Mathematica

I'm trying to compute central moment in Mathematica with arbitrary precision. However with different input format I got different results.

Clearly first moment should be 0 exact, but Mathematica is not giving me 0 for input with floating points. Is there any way to force it to use arbitrary precision? My input is a CSV file with floating point numbers like xxx.xx

CentralMoment[{3,0.7}, 1]=0.x10^-16 // very close to 0, but not exact
CentralMoment[{3,7/10}, 1]=0
//You could try the above with Wolfram alpha online

Upvotes: 2

Views: 286

Answers (2)

Szabolcs
Szabolcs

Reputation: 25703

You can also use Rationalize to convert floating point numbers to exact fractions. Rationalize[0.7] will return 7/10. This is using exact numbers (not quite the same as arbitrary precision, which is usually understood as being able to use an arbitrarily large but still finite precision---as in acl's answer).

Upvotes: 1

acl
acl

Reputation: 6520

Here, it does this

CentralMoment[{3, 0.7`16}, 1]
(*
-> 0.\[Times]10^-16
*)

while

CentralMoment[{3, 0.7}, 1]
(*
-> 0
*)

so presumably it's reading them as of some finite precision. Now, since

CentralMoment[{3, SetPrecision[0.7`12, \[Infinity]]}, 1]
(*
-> 0
*)

I guess what you want is SetPrecision.

Upvotes: 5

Related Questions