3
$\begingroup$

Can I change the output of Message to not show MachinePrecision numbers? E.g.

Bleh::test = "test `1`";

This is tolerable:

Message[Bleh::test, 0.01]
(* Bleh::test -- test 0.01` *)

This is not:

xx = 0.01;
Do[xx += 0.04, {5}]
Message[Bleh::test, xx]
(* Bleh::test -- test 0.21000000000000002` *)

Is there a way for Message to display this rounded off as:

xx
(* 0.21 *)
$\endgroup$
3
  • 1
    $\begingroup$ Message[Bleh::test, ToString@xx] uses the current PrintPrecision, I think. -- Well, no, apparently not, but it rounds the output as desired. $\endgroup$
    – Michael E2
    Oct 13, 2020 at 1:39
  • $\begingroup$ @MichaelE2 I thought it might be simple. Want to add as an answer? $\endgroup$
    – Chris K
    Oct 13, 2020 at 1:43
  • 1
    $\begingroup$ I think NumberForm or StandardForm will work better than ToString. $\endgroup$
    – Carl Woll
    Oct 13, 2020 at 3:21

1 Answer 1

4
$\begingroup$

ToString seems to round the output as is usually done in output cells (technically in OutputForm, which is not the same as the "Output" style):

xx = 0.01;
Do[xx += 0.04, {5}]
Message[Bleh::test, ToString@xx]

Bleh::test: test 0.21

$\endgroup$
5
  • 2
    $\begingroup$ Single argument ToString uses OutputForm. If the number is a rational or needs scientific notation, the output will be very bad, e.g., xx=1/7; Do[xx += 1/16, {5}]; Message[Bleh::test, ToString@xx] $\endgroup$
    – Carl Woll
    Oct 13, 2020 at 3:18
  • $\begingroup$ @CarlWoll Quite. I took MachinePrecision to mean, well, MachinePrecision. $\endgroup$
    – Michael E2
    Oct 13, 2020 at 3:34
  • 1
    $\begingroup$ A rational example was simpler, but a scientific notation example is xx = 1.1*^6; Do[xx += 1.1*^7, {5}]; Message[Bleh::test, ToString@xx]. Basically, you should almost never use single argument ToString. $\endgroup$
    – Carl Woll
    Oct 13, 2020 at 3:47
  • $\begingroup$ @CarlWoll Thanks, I didn't think of that. $\endgroup$
    – Michael E2
    Oct 13, 2020 at 3:48
  • $\begingroup$ Thanks, this solved my problem, although @CarlWoll's suggestions of StandardForm or NumberForm might be more robust to non-MachinePrecision numbers. $\endgroup$
    – Chris K
    Oct 14, 2020 at 2:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.