Group Abstract Group Abstract

Message Boards Message Boards

2
|
20.5K Views
|
25 Replies
|
27 Total Likes
View groups...
Share
Share this post:

[?] Change the default number of digits to display in the output?

Posted 6 years ago

Here is some disturbing behavior that caused me a lot of trouble and was difficult to track down.

In[1]:= 50.01 + 50.03
Out[1]= 100.04

In[2]:= 500.01 + 500.03
Out[2]= 1000.04

In[3]:= 5000.01 + 5000.03
Out[3]= 10000.

Really? Definitely don't use Mathematica for your accounting tasks. And it's not a case of the lost digits actually being there but hidden when printing. Let's try looking at 1000 digits just to be safe.

In[4]:= N[5000.01 + 5000.03, 1000]
Out[4]= 10000.

So we have to change the input precision, which is a problem I discovered long ago but have learned to live with to some extent. Although, sometimes it's easier to just use the Windows Calculator. I wish there was a way to make Mathematica extend the precision of all numbers automatically if you wanted to.

But how much precision do we need to get the right answer? You might think, ok, in the last line the inputs have 6 significant digits and the answer has 7. So we should just have to add one extra 0 on the inputs to give them 7 digits.

In[5]:= 5000.010 + 5000.030
Out[5]= 10000.

But no. Let's try adding a lot more.

In[6]:= 5000.01000000000000 + 5000.0300000000000
Out[6]= 10000.

Still not enough. We have to add a full 12 zeros on the end just to get the correct answer to 2 decimal places!

In[7]:= 5000.01000000000000 + 5000.03000000000000
Out[7]= 10000.0400000000000

I never liked the way Mathematica handled precision, and this makes me like it even less.

POSTED BY: Doug Lerner
25 Replies

The questions of this thread has been answered more than sufficiently. Please keep discussion contained within the bounds of the original questions and Wolfram technologies. Please make sure you know the rules: https://wolfr.am/READ-1ST

POSTED BY: EDITORIAL BOARD
Posted 6 years ago
POSTED BY: Doug Lerner
POSTED BY: Neil Singer
POSTED BY: Michael Rogers
Posted 6 years ago
POSTED BY: Doug Lerner
Posted 6 years ago
POSTED BY: Doug Lerner
POSTED BY: Daniel Lichtblau
Posted 6 years ago

I thought that might be the case, but it seems wrong that setting the precision should override the digit display while the N function does not. If anything, it should be the reverse. Setting precision should only affect the internal representation of the number, not the display, whereas the N function is just about synonymous with "display more digits". Seems like the problem now is the N function.

POSTED BY: Doug Lerner
POSTED BY: Daniel Lichtblau
POSTED BY: Neil Singer
Posted 6 years ago

Wow, good answer, Thanks.

That solves the problem, but I still feel like there are inconsistencies. Like why does 5000.01`7 + 5000.03`7 = 10000.04 with display digits set to 6, but N[5000.01 + 5000.03, 1000] = 1000. using the same settings?

POSTED BY: Doug Lerner

What does "7" even mean

You can specify the precision of a number with the prime character. For example if I type 10. I get 10 to machine precision (normally about 16 digits) but if I type 10.`20 I get a floating point number with 20 digits of precision. You really need to read this tutorial.

POSTED BY: Neil Singer
POSTED BY: Neil Singer
POSTED BY: Henrik Schachner
Posted 6 years ago
POSTED BY: Doug Lerner

This discussion is somewhat hard to understand. I anyone really thinking that there is a miscalculation?

enter image description here

POSTED BY: Henrik Schachner
Posted 6 years ago
POSTED BY: Doug Lerner

.. I've been thinking about a way of not having to manually put the precision number in SetPrecision... I found this way if you turn the numbers into string to then use the command. Is there any other way?

a = "5000.01700000";
b = "5000.00000000";
SetPrecision[(a // ToExpression) + (b // ToExpression), 
 StringLength[a]]

a2 = "50000.01700000";
b2 = "50000.00000010";
SetPrecision[(a2 // ToExpression) + (b2 // ToExpression), 
 StringLength[a2]]

a3 = "500000.017000000";
b3 = "500000.000000100";
SetPrecision[(a3 // ToExpression) + (b3 // ToExpression), 
 StringLength[a3]]

imagz1

Thanks

POSTED BY: Claudio Chaib

If you insist on copying and pasting data that is used elsewhere, try

 SetPrecision[5000.01 + 5000.03, 7]

(* 10000.04.*)

.

Posted 6 years ago

I was aware of this method and should have mentioned it. In fact, it's the way I worked around the problem in my notebook. But adding `x on the end of every number is just as tedious as adding 0's, especially when you have a long list of numbers possibly copied from somewhere else.

Another issue with that is why doesn't 5000.01`7 behave the same as 5000.010 or even 5000.0100000? What does the 7 even mean?

POSTED BY: Doug Lerner

You can try

5000.01`7 + 5000.03`7

 (* 10000.04   . *) 

Yes! This method is perfect! Works for many many decimal places! Thanks a lot, Marvin, I learned something new!

5000.0000000000000000000000000000001`36 + 5000.0000000000000000000000000000003`36

( * 10000.0000000000000000000000000000004 * )

POSTED BY: Claudio Chaib
POSTED BY: Claudio Chaib