Skip to Main Content Skip to Search
Login
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Thread Subject: Rounding a nuber after 4th digit

Subject: Rounding a nuber after 4th digit

From: Hosein

Date: 07 Jul, 2008 18:20:03

Message: 1 of 6

what is a function that can do the following operation?

?(1.23456)=1.2346
?(1.23451)=1.2345

It means that I want to round the number just after the
fourth digit.

Subject: Re: Rounding a nuber after 4th digit

From: someone

Date: 07 Jul, 2008 18:27:02

Message: 2 of 6

"Hosein " <Kalaeimh@yahoo.com> wrote in message <g4tmoj$sl8
$1@fred.mathworks.com>...
> what is a function that can do the following operation?
>
> ?(1.23456)=1.2346
> ?(1.23451)=1.2345
>
> It means that I want to round the number just after the
> fourth digit.
>

% If you mean fourth DECIMAL digit, then:

x=[1.23456 1.23451]
y = round(10000*x)/10000

Subject: Re: Rounding a nuber after 4th digit

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 07 Jul, 2008 18:44:29

Message: 3 of 6

In article <g4tmoj$sl8$1@fred.mathworks.com>,
Hosein <Kalaeimh@yahoo.com> wrote:
>what is a function that can do the following operation?
>
>?(1.23456)=1.2346
>?(1.23451)=1.2345
>
>It means that I want to round the number just after the
>fourth digit.

You can't do it in any binary floating point arithmetic machine.
There is no exact binary floating point representation
possible for 1.2345 or 1.2346

>> sprintf('%.250g',1.2345)

ans =

1.2344999999999999307220832633902318775653839111328125

>> sprintf('%.250g',1.2346)

ans =

1.234599999999999919708670859108678996562957763671875


You can only do this for "presentation purposes", that is as
character strings:

>> sprintf('%.4f',1.23456)

ans =

1.2346

>> sprintf('%.4f',1.23451)

ans =

1.2345


--
  "Whenever there is a hard job to be done I assign it to a lazy
  man; he is sure to find an easy way of doing it."
                                              -- Walter Chrysler

Subject: Re: Rounding a nuber after 4th digit

From: ravi.chilumula@gmail.com

Date: 07 Jul, 2008 19:21:41

Message: 4 of 6

here is a simple function tht i wrote for repetitive use..



% Author: Ravi Chilumula, Last Modified: Mon, jul 07, 2008, 15:20PM EST
% Function Usage: % truncate after specified number of digits
% x= 1.345678;, truncate(x,4) rounds it after 4 decimal points

function [outp] = truncate(inp, noOfDigits) % do polyfit using the logarithm of the x-input

outp = ((10^noOfDigits).*inp)/(10^noOfDigits);

Subject: Re: Rounding a nuber after 4th digit

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 07 Jul, 2008 19:50:54

Message: 5 of 6

In article <31452591.1215458531550.JavaMail.jakarta@nitrogen.mathforum.org>,
ravi.chilumula@gmail.com <ravi.chilumula@gmail.com> wrote:
>here is a simple function tht i wrote for repetitive use..
>
>
>
>% Author: Ravi Chilumula, Last Modified: Mon, jul 07, 2008, 15:20PM EST
>% Function Usage: % truncate after specified number of digits
>% x= 1.345678;, truncate(x,4) rounds it after 4 decimal points
>
>function [outp] = truncate(inp, noOfDigits) % do polyfit using the logarithm of the x-input
>
>outp = ((10^noOfDigits).*inp)/(10^noOfDigits);


Your function does not do what it claims to do. It *cannot* do
what it claims to do. 1.3456 and 1.3457 are not *possible* to
represent exactly in ANY binary floating point number system.

1/1000 is 1/(2^3 * 5^3). The 1/2^3 part is representable exactly
in a binary floating point number systems, but 1/125 is NOT
exactly representable in binary floating point. 1/5 has a repeating
binary representation, binary 0.100110011001(repeated infinitely)
just the same way as in decimal, 1/7 is 0.1428571428571(repeated infinitely)
1/10 and powers thereof are not representable exactly in
IEEE binary floating point.

Your routine does not truncate after the given number of decimal
places: it just produces a number that is -close to- the truncated value.

Actually it doesn't even do that, if double precision numbers
are input: you do not have a fix() or round() or ceil() or floor()
so the output is going to be the same as the input except in
cases of overflow.
--
  "The study of error is not only in the highest degree
  prophylatic, but it serves as a stimulating introduction to the
  study of truth." -- Walter Lipmann

Subject: Re: Rounding a nuber after 4th digit

From: Steven Lord

Date: 08 Jul, 2008 03:46:35

Message: 6 of 6


"Hosein " <Kalaeimh@yahoo.com> wrote in message
news:g4tmoj$sl8$1@fred.mathworks.com...
> what is a function that can do the following operation?
>
> ?(1.23456)=1.2346
> ?(1.23451)=1.2345
>
> It means that I want to round the number just after the
> fourth digit.

For clarification, why do you want to do this? Do you want to try to
perform computions using these truncated numbers, or do you just want to
display them?

In the former case, you'll probably need to use something like Fixed-Point
Toolbox:

http://www.mathworks.com/products/fixed/

In the latter, try to do this using FPRINTF:


x = [1.23456 1.23451];
fprintf('%1.4f\n', x)


--
Steve Lord
slord@mathworks.com


Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
round someone 07 Jul, 2008 14:30:10
rssFeed for this Thread

envelope graphic E-mail this page to a colleague

Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.
Related Topics