Message Boards Message Boards

0
|
13494 Views
|
10 Replies
|
2 Total Likes
View groups...
Share
Share this post:

[?] Round to x significant digits before printing it?

Posted 7 years ago

I have some numerical computations, in MachinePrecsion, which need to be displayed to a user. But any more than 5 significant digits is wasted. The data is in the variables C1, C1, C2 and C3. I tried

C0=Round[C0,0.0001]; C1=Round[C1,0.0001]; C2=Round[C2,0.0001]

but whilst that works ok if the numbers are small, if for example C3 is 1000,1234567, then its printing 1000.12346 which is unjustified. Using some real example, I get as output from the Print[] command as:

C0=74.721, C1=686.2041, C2=-1892.2151, C3=177.2297

which is acceptable for C0, but not for C1, C2 or C3.

On a related topic, I have a numbers 3.0 and 50.0, which I want to print, with one decimal place, so it shows as 3.0 and 50.0, rather than 3. and 50. What I am seeing is

OffsetZo 50.
OffsetLoss 3.

which is not the normal way expects to see numbers printed.

POSTED BY: David Kirkby
10 Replies
Posted 7 years ago

Thank you everyone. Using NumberForm[] achieves what I wanted to achieve.

Dave

POSTED BY: David Kirkby
Posted 7 years ago

Duplicate. Please delete

POSTED BY: Hans Milton
Posted 7 years ago

Compare these 2 variations of NumberForm:

enter image description here

(As a picture. Code does not turn up very well here in this case)

POSTED BY: Hans Milton

David,

I read your post too fast and did not realize what you wanted. Sander is correct, for printing you need to use NumberForm (or its cousin, PaddedForm). However, what you want to do is a bit more complicated -- NumberForm will give you places after the decimal and you want 5 digits, period. I posted this answer a while back here for someone else-- you need to make a function that uses NumberForm:

The idea is that you can specify digits after the decimal for NumberForm but this varies depending on the order of magnitude of the number so you need a function to compute it.

This other post will also help you if you want more complicated formatting-- see my replies.

POSTED BY: Neil Singer
Posted 7 years ago

N[] seems to work for some numbers (I assume things like irrationals), but not for machine precision

In[321]:= c0Agilent

Out[321]= 74.7209748595

In[322]:= N[c0Agilent, 5]

Out[322]= 74.7209748595

Despite using N[], the number is still printed to far more significant digits than wanted. I agree this method works with irrational numbers

In[323]:= N[Pi, 5]

Out[323]= 3.1416

In[324]:= N[1/7, 5]

Out[324]= 0.14286

but it does not work with machine precision numbers.

Any other suggestions?

POSTED BY: David Kirkby

N is not used for printing as I said above, N is used to give an numerical approximation of something. Since it is already a machine precision number it doesn't make much senseĀ…

Use NumberForm to display rounded numbers. Use Round to actually round.

POSTED BY: Sander Huisman

Notice the differences beween the following methods:

c = 1/1.7;

Round[%, 0.00001]
FullForm[%]

N[c, 5]
FullForm[%]

NumberForm[c, {\[Infinity], 5}]
FullForm[%]

Round will round the number, but because numbers are stored in binary, the least significant digits might cause a slight deviation.

N will change a MachinePrecision number to arbitrary precision number, this number will now 'act' differently, because you set the precision of a Machine precision number to 2.

N[Sqrt[2], 2]
%^2

will not return 1.96, but rather 2. because of the limited precision it was given/told.

NumberForm will only display a certain number of digits but does not change the number, i.e. excellent for printing.

POSTED BY: Sander Huisman
Posted 7 years ago

Thank you, and point taken about the use of upper case characters. I did know they were not supposed to be used, but ignored it!

Dave

POSTED BY: David Kirkby

... Also, I strongly suggest that you avoid using Capital letters to begin your variable names -- these are reserved for internal Mathematica variables and functions. It works fine until you accidentally stumble on an internal name and then it is really hard to figure out why something suddenly does not work. For example C1 and C2, etc. are often returned as arbitrary constants from equation solving.

POSTED BY: Neil Singer

David,

Use N[]

N[Pi,5]

returns

3.1416

while

N[100*Pi,5] returns

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

Group Abstract Group Abstract