Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how can I mannage round-off in MATLAB?

884 views
Skip to first unread message

james

unread,
Oct 18, 2008, 3:29:59 AM10/18/08
to
Dear experts,

The matlab always round-off my data even I use "format long". For example,

123456.78987654321

This number is always rounded-off in to the exp form. How can I keep my data with the same digit without any rounding-off?

Thanks a lot
James.

Rune Allnor

unread,
Oct 18, 2008, 4:45:40 AM10/18/08
to

Don't know what you are askig here - the data are always
stored on the same form (double precision floating point,
unless you have specified something else) so the rounding
errors are the same for all stored data (well, within the
scaling constraints etc. of FP number formats).

If you ask about how the numbers appear on the screen,
then the numbers are displayed to within the rules
set up in the command window. So the stored binary
values are almost certainly slightly different from
the numbers that appear on the screen.

I don't know how to configure the displays in the command
window, or if it can be done at all.

Rune

John D'Errico

unread,
Oct 18, 2008, 7:53:02 AM10/18/08
to
james <firstsi...@yahoo.com> wrote in message <12125930.1224315029...@nitrogen.mathforum.org>...

format long g
123456.78987654321
ans =
123456.789876543

may be a better choice for you.

John

Jan Simon

unread,
Oct 18, 2008, 10:06:01 AM10/18/08
to
Dear John D'Errico!

You can increase the number of digits in the output:
sprintf('%.32f\n', pi);
Nevertheless, this does not increase the internal precision.

Jan

John D'Errico

unread,
Oct 18, 2008, 10:44:02 AM10/18/08
to
"Jan Simon" <matlab.T...@nMINUSsimon.de> wrote in message <gdcqg9$fcf$1...@fred.mathworks.com>...

Let me see if I understand you. Your suggestion
is to print out an extra 16 or so garbage digits.

Exactly how is that of any value to anybody
under the sun?

John

Walter Roberson

unread,
Oct 18, 2008, 11:16:58 AM10/18/08
to
Jan Simon wrote:

> You can increase the number of digits in the output:
> sprintf('%.32f\n', pi);
> Nevertheless, this does not increase the internal precision.

If your value is less than 1e-32 then using a %.32f format will show
all zeros, even if the value exactly representable.

Walter Roberson

unread,
Oct 18, 2008, 11:21:39 AM10/18/08
to
james wrote:
> The matlab always round-off my data even I use "format long". For example,

> 123456.78987654321

> This number is always rounded-off in to the exp form. How can I keep my data with the
> same digit without any rounding-off?

You cannot, except by using the symbolic toolbox:

sym('123456.78987654321')

Alternately, you -might- be able to use one of the Matlab File Exchange (FEX)
contributions to handle indefinite precision computations.

Note that neither of these choices will prevent -computations- from being rounded off,
but at least you would be able to exactly preserve any data that you had explicitly
entered...

Jan Simon

unread,
Oct 19, 2008, 7:16:02 AM10/19/08
to
Dear John D'Errico!

>> Jan wrote:
>> sprintf('%.32f\n', pi);

> Let me see if I understand you. Your suggestion


> is to print out an extra 16 or so garbage digits.

Right. You understand exactly.

The question was "How can I keep my data with the same digit without any rounding-off?"
My answer produces garbage digits. You can see very easy, where the garbage starts and you can get a feeling, that "the same number of **digits**" is not well defined. The internal binary representation of numbers can not be displayed as decimal number without either rounding or garbage.

In other words:
X ~= sscanf(sprintf('%<a.b>f', X) (same for 'g' format)
For some X, we get equality, but for any <a> or <b> there are some X, which "fail".

Garbage or rounding-off -- it's your choice!

Kind regrards, Jan

Tim Davis

unread,
Oct 19, 2008, 9:58:02 AM10/19/08
to
"Jan Simon" <matlab.T...@nMINUSsimon.de> wrote in message <gdf4ti$321$1...@fred.mathworks.com>...


Neither. You misunderstood what John was saying. See
Section 1.7 of http://www.mathworks.com/moler/intro.pdf

IEEE *floating* point gives you exactly 53 bits of significant digits (binary, not decimal). Divide that by log2(10) and you get 15.95 ("about 16") decimal digits.

If you try to print a *floating* point number in a *fixed* point format (%f) then the exponent is missing, and must be represented with the position of the fixed point.

To see the exact mantissa, use %.16e instead of %f. Sometimes %.17e is needed to get the exact bits back again.

In CHOLMOD, I have a function which writes out a sparse matrix to a file. To determine the proper ascii format (while at the same time using the fewest characters as possible) I try a range of decimal precisions, reading the number back in until it matches. It always does, once the # of decimal digits reaches at most 17. See

http://www.cise.ufl.edu/research/sparse/cholmod/CHOLMOD/Check/cholmod_write.c

it's in C, but should be readable for MATLAB users. See the "print_value" function.

0 new messages