1

I have a quite frustrating problem.

In my code, I need Mathematica to ignore everything after 10 digits.

For example I need it to treat:

25.26011447795545 as 25.26011448

That is because there are some rounding errors in my calculations, and two numbers that are supposed to be equal, suddenly are not which messes with the rest of my code. Is there any way of fixing it? I tried Block maxprecision but it did not seem to work

2132123
  • 657
  • 3
  • 9
  • 1
    N[Round[25.26011447795545, 10^-8], 10] – Bob Hanlon Feb 01 '22 at 03:55
  • @Artes not really. All it does is it shows the 10 digits to me ( in some weird form that is not respected by ''==''). I need Mathematica to identify these two numbers as the same. – 2132123 Feb 01 '22 at 04:06
  • 1
    Take a look at new (in version 13) functions Around and CenteredInterval. – Artes Feb 01 '22 at 04:18
  • 4
    You want computations to be in 10 decimal digit floating point? Or do you want arbitrary-precision with the precision fixed at 10? Or do you want machine precision, but have comparisons (==, <, >, etc.) to be done with a tolerance equivalent to ten digits? Are there operations besides comparisons that are to be done with ten digit precision? – Michael E2 Feb 01 '22 at 05:13
  • @MichaelE2 "You want computations to be in 10 decimal digit floating point" and "comparisons (==, <, >, etc.) to be done with a tolerance equivalent to ten digits" – 2132123 Feb 02 '22 at 02:50
  • 10-digit base-10 FP is hard to implement and would be slow, how slow depending on whether is was implemented in WL or a compiled library. But perhaps the [ComputerArithmetic` package](http://reference.wolfram.com/language/ComputerArithmetic/tutorial/ComputerArithmetic.html) could be used, which probably is implemented in WL. – Michael E2 Feb 02 '22 at 03:29

1 Answers1

2

You may use "SetPrecision". Here is an example:

t1 = SetPrecision[25.26011448, 10];
t2 = SetPrecision[25.260114486, 10];
t1 == t2
(* True *)

Although t1 and t2 are still stored as machine numbers with e.g. 64 bits, only the first 10 digital digits are used for a comparison. This makes t1 equal t2 even if they are different if rounded to 10 digits.

However, they are still stored as different machine numbers:

BaseForm[t1, 2]
BaseForm[t2, 2]
BaseForm[t1, 10]
BaseForm[t2, 10]

enter image description here

Daniel Huber
  • 51,463
  • 1
  • 23
  • 57