Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Precision

57 views
Skip to first unread message

Rob Ryan

unread,
Jan 5, 2013, 2:17:35 AM1/5/13
to
My frustration is growing. I simply want to add (for example):
103971.66+52282.64+2998.27. These are dollars and cents and I'd like to keep the cents. But when I input that line, I get 159923. When I input: N[(103971.66+52282.64+2998.27),20] I STILL get 159923. What do I do to get 159952.57? I've used "SetPrecision", etc. with still no result.
I note that when I type 103971.66 and hit shift-return, the output is 103972. If I then type "InputForm[%]" I get 103971.66.

There simply has to be an easy way to add these numbers - any calculator watch can do it!

Nasser M. Abbasi

unread,
Jan 7, 2013, 12:35:27 AM1/7/13
to
On 1/5/2013 1:17 AM, Rob Ryan wrote:
> My frustration is growing. I simply want to add (for example):
> 103971.66+52282.64+2998.27. These are dollars and cents and I'd like to keep the cents.
>But when I input that line, I get 159923. When I input: N[(103971.66+52282.64+2998.27),20]
>I STILL get 159923. What do I do to get 159952.57?

SetOptions[$FrontEndSession, PrintPrecision -> 16]

In[43]:= 103971.66+52282.64+2998.27
Out[43]= 159252.57

--Nasser


djmpark

unread,
Jan 7, 2013, 12:35:37 AM1/7/13
to
To permanently change your number display use Menu -> Edit -> Preferences,
then the Appearance, Numbers tab and set the display you wish.

For custom formatting within a notebook use NumberForm and its options.

Note that the purpose of N is to convert an exact number to an approximate
number and it is not intended for number formatting.


David Park
djm...@comcast.net
http://home.comcast.net/~djmpark/index.html



From: Rob Ryan [mailto:tio5...@gmail.com]


My frustration is growing. I simply want to add (for example):
103971.66+52282.64+2998.27. These are dollars and cents and I'd like to keep
the cents. But when I input that line, I get 159923. When I input:
N[(103971.66+52282.64+2998.27),20] I STILL get 159923. What do I do to get

Richard Fateman

unread,
Jan 7, 2013, 12:36:09 AM1/7/13
to
On 1/4/2013 11:17 PM, Rob Ryan wrote:
> My frustration is growing. I simply want to add (for example):
> 103971.66+52282.64+2998.27.


InputForm[103971.66+52282.64+2998.27] gives 159252.56999999998

If you don't like that, use the calculator on your phone (or watch? do
people still have watches? Such big ones?)


If this all seems mysterious, read up on floating-point binary numbers .




Andrzej Kozlowski

unread,
Jan 7, 2013, 12:36:14 AM1/7/13
to

On 5 Jan 2013, at 08:19, Rob Ryan <tio5...@gmail.com> wrote:

> My frustration is growing. I simply want to add (for example):
> 103971.66+52282.64+2998.27. These are dollars and cents and I'd like to keep the cents. But when I input that line, I get 159923. When I input: N[(103971.66+52282.64+2998.27),20] I STILL get 159923. What do I do to get 159952.57? I've used "SetPrecision", etc. with still no result.
> I note that when I type 103971.66 and hit shift-return, the output is 103972. If I then type "InputForm[%]" I get 103971.66.
>
> There simply has to be an easy way to add these numbers - any calculator watch can do it!


In this case you can simply use NumberForm:

NumberForm[103971.66 + 52282.64 + 2998.27, 8]

159252.57

However, this approach will not always work (to understand why you have to first understand that Mathematica's approximate reals are not what you probably think they are)

A much more reliable way is to do such computations using exact numbers. This will never give you a wrong answer:

QuotientRemainder[10397166 + 5228264 + 299827, 100]

{159252, 57}

If you don't like this, you would be better off using a calculator watch.


Andrzej Kozlowski




Tomas Garza

unread,
Jan 7, 2013, 12:35:53 AM1/7/13
to
Since you're dealing with dollars and cents you don't have to worry about precision. Do your operations in the conventional way, and at the end, for purposes of viewing or printing your final result, use NumberForm:
In[1]:= 103971.66 + 52282.64 + 2998.27

Out[1]= 159253.
In[2]:= NumberForm[103971.66+52282.64+2998.27, {8,2}]
Out[2]//NumberForm= 159252.57
You can also ask for some other type of display, like inserting commas:
In[3]:= NumberForm[103971.66+52282.64+2998.27, {8,2}, DigitBlock->3]
Out[3]//NumberForm= 159,252.57
Check the OnLine Help browser or the tutorial on output formats for numbers.
-Tomas
> Subject: Precision
> From: tio5...@gmail.com
> To: math...@smc.vnet.net
> Date: Sat, 5 Jan 2013 02:19:25 -0500

Ray Koopman

unread,
Jan 7, 2013, 11:05:22 PM1/7/13
to
If you want the cents to *always* print, use PaddedForm with an
explicit request for 2 digits to the right of the decimal point.

s = 103971.66 + 52282.64 + 2998.27
NumberForm[s,8]

159253.
159252.57

t = s + .43
NumberForm[t,8]
NumberForm[t,{8,2}]
PaddedForm[t,{8,2}]

159253.
159253.
159253.
159253.00

Murray Eisenberg

unread,
Jan 7, 2013, 11:05:00 PM1/7/13
to
Of course in the case of dollars and cents, one should not ordinarily need to worry about loss of precision -- unless one is dealing with "exact" quantities on the order of the U.S. National Debt, which is roughly 2 * 10^13 and hence, including cents, would use 16 digits.

On Jan 7, 2013, at 12:38 AM, Andrzej Kozlowski <akozl...@gmail.com> wrote:

>
> On 5 Jan 2013, at 08:19, Rob Ryan <tio5...@gmail.com> wrote:
>
>> My frustration is growing. I simply want to add (for example):
>> 103971.66+52282.64+2998.27. These are dollars and cents and I'd like to keep the cents. But when I input that line, I get 159923. When I input: N[(103971.66+52282.64+2998.27),20] I STILL get 159923. What do I do to get 159952.57? I've used "SetPrecision", etc. with still no result.
>> I note that when I type 103971.66 and hit shift-return, the output is 103972. If I then type "InputForm[%]" I get 103971.66.
>>
>> There simply has to be an easy way to add these numbers - any calculator watch can do it!
>
>
> In this case you can simply use NumberForm:
>
> NumberForm[103971.66 + 52282.64 + 2998.27, 8]
>
> 159252.57
>
> However, this approach will not always work (to understand why you have to first understand that Mathematica's approximate reals are not what you probably think they are)
>
> A much more reliable way is to do such computations using exact numbers. This will never give you a wrong answer:
>
> QuotientRemainder[10397166 + 5228264 + 299827, 100]
>
> {159252, 57}
>
> If you don't like this, you would be better off using a calculator watch.

---
Murray Eisenberg mur...@math.umass.edu
Mathematics & Statistics Dept.
Lederle Graduate Research Tower phone 413 549-1020 (H)
University of Massachusetts 413 545-2838 (W)
710 North Pleasant Street fax 413 545-1801
Amherst, MA 01003-9305






Andrzej Kozlowski

unread,
Jan 10, 2013, 2:19:49 AM1/10/13
to
Well, no. For large enough numbers the final cents will be rounded, e.g.

NumberForm[1234500000990000 + 12345.98, {16, 2}, ExponentFunction -> (Null &)]
1234500001002346.00

while the correct answer is:

QuotientRemainder[1234500000990000 + 1234598, 100]

{12345000022245, 98}

Of course Murray was right; this won't be a problem unless you want to compute the US national debt up to cent or something of this kind. However, note also another problem, which also will come up when the number involved become large, the need to use the option ExponentFunction. Without it you get:

NumberForm[1234500000990000 + 12345.98, 30]

1.234500001002346*10^(15)

As for your last question, the answer is simple: Mathematica is not Excel. If you want to do this sort of computations the way you would do in grade school use Excel or a pocket calculator.


Andrzej Kozlowski




On 8 Jan 2013, at 19:02, Kenneth Ramsey <ramse...@msn.com> wrote:

> I think number form will always give the correct answer but drop ending zeros from the cents. That is 751.60 -> "751.6" and 751.00 -> "751." Don't know how to get around that., but I think it is better than QuotientRemainder which forces you to type in the final zeros in the cents. Excel allows you to chose the number of decimal places in the number. Why isn't that so with Mathematical?
>
>> From: akozl...@gmail.com
>> Subject: Re: Precision
>> To: math...@smc.vnet.net
>> Date: Mon, 7 Jan 2013 00:38:54 -0500
>>
>>
>> On 5 Jan 2013, at 08:19, Rob Ryan <tio5...@gmail.com> wrote:
>>
>>> My frustration is growing. I simply want to add (for example):
>>> 103971.66+52282.64+2998.27. These are dollars and cents and I'd like to keep the cents. But when I input that line, I get 159923. When I input: N[(103971.66+52282.64+2998.27),20] I STILL get 159923. What do I do to get 159952.57? I've used "SetPrecision", etc. with still no result.
>>> I note that when I type 103971.66 and hit shift-return, the output is 103972. If I then type "InputForm[%]" I get 103971.66.
>>>
>>> There simply has to be an easy way to add these numbers - any calculator watch can do it!
>>
>>
>> In this case you can simply use NumberForm:
>>
>> NumberForm[103971.66 + 52282.64 + 2998.27, 8]
>>
>> 159252.57
>>
>> However, this approach will not always work (to understand why you have to first understand that Mathematica's approximate reals are not what you probably think they are)
>>
>> A much more reliable way is to do such computations using exact numbers. This will never give you a wrong answer:
>>
>> QuotientRemainder[10397166 + 5228264 + 299827, 100]
>>
>> {159252, 57}
>>
>> If you don't like this, you would be better off using a calculator atch.
>>
>>
>> Andrzej Kozlowski
>>
>
>>
>>


0 new messages