Path: news.mathworks.com!newsfeed-00.mathworks.com!newsfeed2.dallas1.level3.net!news.level3.com!postnews.google.com!news1.google.com!news.glorb.com!news.litech.org!News.Dal.Ca!newsflash.concordia.ca!canopus.cc.umanitoba.ca!not-for-mail
From: Walter Roberson <roberson@hushmail.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: floating point percision
Date: Fri, 19 Nov 2010 16:25:29 -0600
Organization: Canada Eat The Cookie Foundation
Lines: 25
Message-ID: <ic6th4$6tl$1@canopus.cc.umanitoba.ca>
References: <ic6sal$78f$1@fred.mathworks.com>
NNTP-Posting-Host: ibd-nat.ibd.nrc.ca
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: canopus.cc.umanitoba.ca 1290205541 7093 132.246.133.10 (19 Nov 2010 22:25:41 GMT)
X-Complaints-To: abuse@cc.umanitoba.ca
NNTP-Posting-Date: Fri, 19 Nov 2010 22:25:41 +0000 (UTC)
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.15) Gecko/20101027 Thunderbird/3.0.10
In-Reply-To: <ic6sal$78f$1@fred.mathworks.com>
Xref: news.mathworks.com comp.soft-sys.matlab:688489

On 10-11-19 04:05 PM, Eman wrote:

> how can I determine the number of digits after the decimal point?
> I mean:
> If I need 2 digits after decimal point, If a=74.67859 it should be a=74.68
> If I need 3 digits after decimal point, If a=74.67859 it should be a=74.679
> note: I need to use the new value not only format them in printing.

The only way to do that is to use symbolic numbers with the symbolic toolkit, 
or fixed-point numbers with the fixed-point toolbox.


You can use transformations such as

round(a .* 10^D) ./ 10^D

where D is the number of digits you want after the decimal place. This will 
not, however, round to *exactly* 74.68: it would instead round to 
74.68000000000000682121026329696178436279296875

When you use binary floating point numbers, the only numbers with two decimal 
places that can be *exactly* represented are those that end in .0, .25, .5, or 
.75 . It is not possible, for example, to exactly represent 0.1 in binary 
floating point: the closest you can get would be 
0.1000000000000000055511151231257827021181583404541015625