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:
floating point percision

Subject: floating point percision

From: Eman

Date: 19 Nov, 2010 22:05:09

Message: 1 of 5

Hi All..
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.
thx

Subject: floating point percision

From: Walter Roberson

Date: 19 Nov, 2010 22:25:29

Message: 2 of 5

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

Subject: floating point percision

From: Celine Cluzel

Date: 22 Nov, 2010 11:46:04

Message: 3 of 5

I suppose you can convert to string with desired number of digits and convert back to number.
example:

 b = sprintf('%.2f', 12345.6785) % I ask for 2 digits here

b =

12345.68

>> str2num(b)

ans =

    1.234568000000000e+004



Walter Roberson <roberson@hushmail.com> wrote in message <ic6th4$6tl$1@canopus.cc.umanitoba.ca>...
> 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

Subject: floating point percision

From: James Tursa

Date: 22 Nov, 2010 17:45:06

Message: 4 of 5

"Celine Cluzel" <celine.cluzel@element-energy.co.uk> wrote in message <icdl5s$7tb$1@fred.mathworks.com>...
> I suppose you can convert to string with desired number of digits and convert back to number.
> example:
>
> b = sprintf('%.2f', 12345.6785) % I ask for 2 digits here
>
> b =
>
> 12345.68
>
> >> str2num(b)
>
> ans =
>
> 1.234568000000000e+004

Not sure if your intent was to get a different result from Walter or not, but this doesn't give anything different from what Walter has already posted. e.g.,

>> a = 12345.6785
a =
    1.234567850000000e+004
>> b = sprintf('%.2f', 12345.6785)
b =
12345.68
>> str2num(b)
ans =
    1.234568000000000e+004
>> num2strexact(ans)
ans =
1.23456800000000002910383045673370361328125e4
>> D = 2;
>> round(a .* 10^D) ./ 10^D
ans =
    1.234568000000000e+004
>> num2strexact(ans)
ans =
1.23456800000000002910383045673370361328125e4

The num2strexact utility can be found here:

http://www.mathworks.com/matlabcentral/fileexchange/22239-num2strexact-exact-version-of-num2str

James Tursa

Subject: floating point percision

From: JennaBryant31

Date: 19 Jan, 2011 05:37:43

Message: 5 of 5

Do not money to buy some real estate? Worry not, because it is available to take the <a href="http://bestfinance-blog.com/topics/credit-loans">credit loans</a> to resolve such kind of problems. Thus take a college loan to buy all you need.

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