Group Abstract Group Abstract

Message Boards Message Boards

Explain SetPrecision and N

Posted 1 day 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
3 Replies
Posted 20 hours ago

The problem is that the numbers in the matrices are mostly very small, of the order of 10^-4000, which is also why I calculated them in C++ using long double. So Round[mantissa*10^-4000, 10^-20]=0. I could create a little function with MantissaExponent and separate the numbers in two and round only the mantissa. But this would take much more time than to create the matrices. Mathematica can handle arbitrary precision numbers but this is also slow. Speed is key here as I need to maximize the size of the matrices. I could do the same with C++ and save the file in text format, but then I had to compress the text, otherwise the file would get too voluminous. But if I am well informed, Mathematica would not be able to read the file as Compress and Uncompress use another format.

POSTED BY: Ulrich Utiger

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