Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Rounding Arithmetic

Subject: Rounding Arithmetic

From: veronica

Date: 14 Jan, 2011 17:08:04

Message: 1 of 7

How can I make my program round all my numbers to 5 digits for all operations?

Subject: Rounding Arithmetic

From: Sean de

Date: 14 Jan, 2011 17:13:05

Message: 2 of 7

"veronica " <veronicak5678@gmail.com> wrote in message <igpvtk$3e6$1@fred.mathworks.com>...
> How can I make my program round all my numbers to 5 digits for all operations?

round(x*100000)/100000

Subject: Rounding Arithmetic

From: veronica

Date: 14 Jan, 2011 17:44:05

Message: 3 of 7

> round(x*100000)/100000

That doesn't give me 5 digits. I mean if I have .567876, I want it to return .56788.

Subject: Rounding Arithmetic

From: Sean de

Date: 14 Jan, 2011 17:55:20

Message: 4 of 7

"veronica " <veronicak5678@gmail.com> wrote in message <igq215$j2p$1@fred.mathworks.com>...
> > round(x*100000)/100000
>
> That doesn't give me 5 digits. I mean if I have .567876, I want it to return .56788.


It does...:
%%%
x = 0.567876;
x5 = round(x*100000)/100000;
format long
disp(x5);
%%%

Do you mean 5 digits, including whole integer parts, or 5 decimal places?

Subject: Rounding Arithmetic

From: veronica

Date: 14 Jan, 2011 18:01:05

Message: 5 of 7

>
> Do you mean 5 digits, including whole integer parts, or 5 decimal places?

I mean 5 digits including the integer parts.

Subject: Rounding Arithmetic

From: Sean de

Date: 14 Jan, 2011 18:15:05

Message: 6 of 7

"veronica " <veronicak5678@gmail.com> wrote in message <igq311$lm6$1@fred.mathworks.com>...
> >
> > Do you mean 5 digits, including whole integer parts, or 5 decimal places?
>
> I mean 5 digits including the integer parts.

Okay:
rnd5 = @(x)(round((x/(10^(ceil(log10(x)))))*100000)/10000).*(10^(floor(log10(x))));

rnd5(123456789)


-SCd

Subject: Rounding Arithmetic

From: By learning you will teach; by teaching you will learn.

Date: 14 Jan, 2011 18:58:20

Message: 7 of 7

On 11-01-14 11:08 AM, veronica wrote:
> How can I make my program round all my numbers to 5 digits for all operations?

Other people have answered, but their answers are not complete.

The only way you can do what you want to do is used the Fixed Point Toolkit,
or the Symbolic Math toolkit. Nothing else can round numbers with decimal
fractions to only have 5 digits of decimal fractions.

Using your example of desired result:

 >> sprintf('%.60g',.56788)
ans =
0.56788000000000005140776693224324844777584075927734375

That is, Matlab is completely unable to represent the binary floating point
number _exactly_ equal to your desired result of 0.56788 and so no matter what
mathematical computations you apply, you will not get the answer you want.
Matlab will only get to somewhere near

 >> eps(.56788)
ans =
       1.11022302462516e-16

relative to the exact result you want.

There is no way around this as long as you continue to use binary floating
point arithmetic. The earlier-mentioned Fixed Point Toolkit and Symbolic Math
toolkit do not use floating point arithmetic.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us