Group Abstract Group Abstract

Message Boards Message Boards

Explain SetPrecision and N

Posted 14 hours ago

I calculate huge matrices (of the order of 10^4 x 10^4 or more) in C++ (because Mathematica is too slow for this...) in 80-bit (long double) and export it to 128-bit (because Mathematica can only read either 64-bit or 128-bit with BinaryReadList). I import the matrices into Mathematica because it's much easier to make graphics, convergence analysis and so on. So after having loaded them into Mathematica, the numbers have mantissas of about 50 digits (the numbers on the left after the back tick `). But because they were originally calculated only in 80-bits, 50 digits are useless. In order to load them quickly, I need to convert them into compressed text. But before doing this, I would like to reduce the numbers to 20 digits, because this is 80-bit precision. This would reduce space on disk and load them more quickly. However, SetPrecision[z,p] or N[z,p] where z is a number with a mantissa of 50 decimal digits and p=20 is the desired precision does not reduce the digits at all. If I want to reduce it to 16 digits for example, I need to set p=4 or so. Why is this? Normally, when someone talks of a number z having p precision, I understand that it has a mantissa of p decimal digits. Am I missing something?

POSTED BY: Ulrich Utiger
2 Replies

SetPrecision and N do not reduce the number of digits, they just take note to disregard the digits beyond a position. You may try ResourceFunction["DecimalRound"].

POSTED BY: Gianluca Gorni

Related to Gianluca Gorni's response, you can round to a certain decimal place and then reset precision like so.

pi50 = N[Pi, 50]; InputForm[pi20 = SetPrecision[Round[pi50, 10^(-20)], 20]] (* Out[72]//InputForm= 3.14159265358979323846`20. *)

This cuts down on the retained guard digits.

POSTED BY: Daniel Lichtblau
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard