472,958 Members | 2,562 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

Floating point rounding error

Why does floating point have a rounding error? How to work around it?

For example, the following:

flaot f = 1234.12345678F;

printf("%2f\n", f) //prints 1234.123413

and

printf("%8.9f\n", f) //prints 1234.123413086

Jun 16 '07 #1
15 7989
Mu***************@yahoo.com said:
Why does floating point have a rounding error?
Consider 1234.12345678

It's easy enough to deal with 1234. Here are the bits: 10011010010

So let's try to deal with 0.12345678, using binary notation.

So 0.1 (binary) is 0.5 (decimal), 0.01 (binary) is 0.25 (decimal), and
so on.
0.1 = 1/2 = 0.5 - too large
0.01 = 1/4 = 0.25 - too large
0.001 = 1/8 = 0.125 - too large
0.0001 = 1/16 = 0.0625 - too small
0.00011 = 3/32 = 0.09375 - too small
0.000111 = 7/64 = 0.109375 - too small
0.0001111 = 15/128 = 0.1171875 - too small
0.00011111 = 31/256 = 0.12109375 - too small
0.000111111 = 63/512 = 0.123046875 - too small
0.0001111111 = 127/1024 = 0.1240234375 - too large
0.00011111101 = 253/2048 = 0.12353515625 - too large
0.000111111001 = 505/4096 = 0.123291015625 - too small
0.0001111110011 = 1011/8192 = 0.1234130859375 - too small
0.00011111100111 = 2023/16384 = 0.12347412109375 - too large
0.000111111001101 = 4045/32768 = 0.123443603515625 - too small
0.0001111110011011 = 8091/65536 = 0.1234588623046875 - too large

So far, we've used 16 bits on this. Keep on calculatin', and find out
how many bits you need if you're to get an *exact* representation of
0.12345678. You might well be surprised by the result.
How to work around it?
That depends on what you want to achieve.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jun 16 '07 #2
On Jun 16, 4:32 pm, Richard Heathfield <r...@see.sig.invalidwrote:
Mukesh_Singh_N...@yahoo.com said:
Why does floating point have a rounding error?

Consider 1234.12345678

It's easy enough to deal with 1234. Here are the bits: 10011010010

So let's try to deal with 0.12345678, using binary notation.

So 0.1 (binary) is 0.5 (decimal), 0.01 (binary) is 0.25 (decimal), and
so on.
0.1 = 1/2 = 0.5 - too large
0.01 = 1/4 = 0.25 - too large
0.001 = 1/8 = 0.125 - too large
0.0001 = 1/16 = 0.0625 - too small
0.00011 = 3/32 = 0.09375 - too small
0.000111 = 7/64 = 0.109375 - too small
0.0001111 = 15/128 = 0.1171875 - too small
0.00011111 = 31/256 = 0.12109375 - too small
0.000111111 = 63/512 = 0.123046875 - too small
0.0001111111 = 127/1024 = 0.1240234375 - too large
0.00011111101 = 253/2048 = 0.12353515625 - too large
0.000111111001 = 505/4096 = 0.123291015625 - too small
0.0001111110011 = 1011/8192 = 0.1234130859375 - too small
0.00011111100111 = 2023/16384 = 0.12347412109375 - too large
0.000111111001101 = 4045/32768 = 0.123443603515625 - too small
0.0001111110011011 = 8091/65536 = 0.1234588623046875 - too large

So far, we've used 16 bits on this. Keep on calculatin', and find out
how many bits you need if you're to get an *exact* representation of
0.12345678. You might well be surprised by the result.
How to work around it?

That depends on what you want to achieve.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999http://www.cpax.org.uk
email: rjh at the above domain, - www.

Thank you for replying with a very elaborate example, Richard. I would
disappoint you if I told you I am intrigued by the representation of
non-integral decimal numbers in their binary form.

I know binary arithmetic with integrals. I sometimes wondered and
never bothered myself as to how decimals were represented as binaries.
I want to understand your example.

I can see a pattern in the representation.

0.1 is half.
0.01 is a right shift and you further halve it.
0.001 two right shifts further halving it and so on.

You lost me at 0.011. Can I please request you to explain.

Jun 16 '07 #3
Mu***************@yahoo.com said:

<snip>
I know binary arithmetic with integrals. I sometimes wondered and
never bothered myself as to how decimals were represented as binaries.
The representation of floating-point numbers is implementation-defined.
IEEE 754 is common but not universal.
I want to understand your example.

I can see a pattern in the representation.

0.1 is half.
0.01 is a right shift and you further halve it.
0.001 two right shifts further halving it and so on.

You lost me at 0.011. Can I please request you to explain.
You understand binary integers. Extend the concept.

13 in binary is 1101 (1 * eight + 1 * four + 0 * two + 1 * one).

So each column represents a multiplier half as big as that of the column
to its left.

So we might reasonably think of the columns beyond the binary point as
representing a half, a quarter, an eighth, etc.

So 0.11 would be (half + quarter) = (three-quarters) = 0.75

0.011 would be (quarter + eighth) = (three-eighths) = 0.375

and so on.

That isn't necessarily how they're stored internally, of course, but it
does give you a good idea of the number of bits you need for storing a
particular value to a particular precision. I recommend that you read
http://docs.sun.com/source/806-3568/ncg_goldberg.html

(Title: "What Every Computer Scientist Should Know About Floating-Point
Arithmetic")

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jun 16 '07 #4
Mu***************@yahoo.com wrote, On 16/06/07 11:49:
Why does floating point have a rounding error?
Calculate 2/3 as a decimal number. Come back when you have understood
the answer to your question or when you have finished writing it without
any rounding or truncation. I'll even get you started 0.6667 (I've
rounded it here).
How to work around it?
Use sufficient care and analysis for the problem at hand. This is a
general problem, not a C specific one, so comp.programming, and there is
no one correct solution for all situations.
--
Flash Gordon
Jun 16 '07 #5
Mu***************@yahoo.com wrote:
Why does floating point have a rounding error?
Knowledge of the Great Mysteries is reserved for those
who are worthy. To prove your worth, you must undertake a
Quest and complete it successfully. Your Quest, Mukesh, is
to discover what fraction one day is of one week, and express
the answer as a decimal number, using as many decimal places
as are needed for perfect accuracy. When you have done this
I will know you are indeed worthy, and I will reveal to you
the secret origin of rounding errors.

--
Eric Sosman
es*****@acm-dot-org.invalid
Jun 16 '07 #6
On Jun 16, 4:53 pm, Richard Heathfield <r...@see.sig.invalidwrote:
Mukesh_Singh_N...@yahoo.com said:

<snip>
I know binary arithmetic with integrals. I sometimes wondered and
never bothered myself as to how decimals were represented as binaries.

The representation of floating-point numbers is implementation-defined.
IEEE 754 is common but not universal.
I want to understand your example.
I can see a pattern in the representation.
0.1 is half.
0.01 is a right shift and you further halve it.
0.001 two right shifts further halving it and so on.
You lost me at 0.011. Can I please request you to explain.

You understand binary integers. Extend the concept.

13 in binary is 1101 (1 * eight + 1 * four + 0 * two + 1 * one).

So each column represents a multiplier half as big as that of the column
to its left.

So we might reasonably think of the columns beyond the binary point as
representing a half, a quarter, an eighth, etc.

So 0.11 would be (half + quarter) = (three-quarters) = 0.75

0.011 would be (quarter + eighth) = (three-eighths) = 0.375

and so on.

That isn't necessarily how they're stored internally, of course, but it
does give you a good idea of the number of bits you need for storing a
particular value to a particular precision. I recommend that you readhttp://docs.sun.com/source/806-3568/ncg_goldberg.html

(Title: "What Every Computer Scientist Should Know About Floating-Point
Arithmetic")

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999http://www.cpax.org.uk
email: rjh at the above domain, - www.

Thank you so very much, Richard. You just taught me something
*fantabulous*. I just learnt something terrific, something I could not
have learnt reading a thousand words. Actually, I think I've just
understood the IEEE 754-1985 implementation in a nutshell.

Is this rule of representing decimals in binary applicable only to
754?

And then I revisited your previous table wherein you try to reach a
precision for 1234.12345678 by heuristic computation. It suddenly
removed a big block in my head.

Thank you, everyday.

Jun 16 '07 #7
I recommend that you readhttp://docs.sun.com/source/806-3568/ncg_goldberg.html
>
(Title: "What Every Computer Scientist Should Know About Floating-Point
Arithmetic")

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999http://www.cpax.org.uk
email: rjh at the above domain, - www.

Thank you. I certainly will.

Jun 16 '07 #8
Mu***************@yahoo.com wrote:
On Jun 16, 4:32 pm, Richard Heathfield <r...@see.sig.invalidwrote:
> Mukesh_Singh_N...@yahoo.com said:
>>Why does floating point have a rounding error?
Consider 1234.12345678

It's easy enough to deal with 1234. Here are the bits: 10011010010

So let's try to deal with 0.12345678, using binary notation.

So 0.1 (binary) is 0.5 (decimal), 0.01 (binary) is 0.25 (decimal), and
so on.
0.1 = 1/2 = 0.5 - too large
0.01 = 1/4 = 0.25 - too large
0.001 = 1/8 = 0.125 - too large
0.0001 = 1/16 = 0.0625 - too small
0.00011 = 3/32 = 0.09375 - too small
0.000111 = 7/64 = 0.109375 - too small
0.0001111 = 15/128 = 0.1171875 - too small
0.00011111 = 31/256 = 0.12109375 - too small
0.000111111 = 63/512 = 0.123046875 - too small
0.0001111111 = 127/1024 = 0.1240234375 - too large
0.00011111101 = 253/2048 = 0.12353515625 - too large
0.000111111001 = 505/4096 = 0.123291015625 - too small
0.0001111110011 = 1011/8192 = 0.1234130859375 - too small
0.00011111100111 = 2023/16384 = 0.12347412109375 - too large
0.000111111001101 = 4045/32768 = 0.123443603515625 - too small
0.0001111110011011 = 8091/65536 = 0.1234588623046875 - too large

So far, we've used 16 bits on this. Keep on calculatin', and find out
how many bits you need if you're to get an *exact* representation of
0.12345678. You might well be surprised by the result.
>>How to work around it?
That depends on what you want to achieve.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999http://www.cpax.org.uk
email: rjh at the above domain, - www.


Thank you for replying with a very elaborate example, Richard. I would
disappoint you if I told you I am intrigued by the representation of
non-integral decimal numbers in their binary form.

I know binary arithmetic with integrals. I sometimes wondered and
never bothered myself as to how decimals were represented as binaries.
I want to understand your example.

I can see a pattern in the representation.

0.1 is half.
0.01 is a right shift and you further halve it.
0.001 two right shifts further halving it and so on.

You lost me at 0.011. Can I please request you to explain.
Here's something to chew on..

00111111 10111111 10011010 11011101 00010000 10010001 11001000 10010101
Exp = 1019 (-3)
111 11111101
Man = .11111 10011010 11011101 00010000 10010001 11001000 10010101
1.2345678000000000e-01

I hope it didn't wrap on you.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Jun 16 '07 #9
Eric Sosman wrote:
Mu***************@yahoo.com wrote:
>Why does floating point have a rounding error?

Knowledge of the Great Mysteries is reserved for those
who are worthy. To prove your worth, you must undertake a
Quest and complete it successfully. Your Quest, Mukesh, is
to discover what fraction one day is of one week, and express
the answer as a decimal number, using as many decimal places
as are needed for perfect accuracy. When you have done this
I will know you are indeed worthy, and I will reveal to you
the secret origin of rounding errors.
Easy. Just use a septal base (which is not decimal), get 0.1.
:-) (Very useful for writing lock combinations).

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

Jun 16 '07 #10
Mu***************@yahoo.com wrote:
>
Why does floating point have a rounding error? How to work around
it? For example, the following:

flaot f = 1234.12345678F;
printf("%2f\n", f) //prints 1234.123413
and
printf("%8.9f\n", f) //prints 1234.123413086
It doesn't have a rounding error. It has a precision limit.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

Jun 16 '07 #11
CBFalconer wrote:
Mu***************@yahoo.com wrote:
>Why does floating point have a rounding error? How to work around
it? For example, the following:

flaot f = 1234.12345678F;
printf("%2f\n", f) //prints 1234.123413
and
printf("%8.9f\n", f) //prints 1234.123413086

It doesn't have a rounding error. It has a precision limit.
Indeed. 1.23412341e+03 is all there is in 32 bits.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Jun 16 '07 #12
CBFalconer wrote:
Eric Sosman wrote:
>Mu***************@yahoo.com wrote:
>>Why does floating point have a rounding error?
Knowledge of the Great Mysteries is reserved for those
who are worthy. To prove your worth, you must undertake a
Quest and complete it successfully. Your Quest, Mukesh, is
to discover what fraction one day is of one week, and express
the answer as a decimal number, using as many decimal places
as are needed for perfect accuracy. When you have done this
I will know you are indeed worthy, and I will reveal to you
the secret origin of rounding errors.

Easy. Just use a septal base (which is not decimal), get 0.1.
:-) (Very useful for writing lock combinations).
Since "as a decimal number" was clearly specified in the
rules of the Quest, your change of base is not septal but septic.
I'll have to look up the traditional rules for punishment of
failure. "Something lingering, with boiling oil in it, I fancy.
Something of that sort. I think boiling oil occurs in it, but
I'm not sure. I know it's something humorous, but lingering,
with either boiling oil or melted lead."

AND you'll never get to learn about rounding error. Nyaahh!

--
Eric Sosman
es*****@acm-dot-org.invalid
Jun 16 '07 #13
Groovy hepcat Mu***************@yahoo.com was jivin' on Sat, 16 Jun
2007 05:07:47 -0700 in comp.lang.c.
Re: Floating point rounding error's a cool scene! Dig it!
>On Jun 16, 4:53 pm, Richard Heathfield <r...@see.sig.invalidwrote:
> Mukesh_Singh_N...@yahoo.com said:
>The representation of floating-point numbers is implementation-defined.
IEEE 754 is common but not universal.
I want to understand your example.
I can see a pattern in the representation.
0.1 is half.
0.01 is a right shift and you further halve it.
0.001 two right shifts further halving it and so on.
You lost me at 0.011. Can I please request you to explain.

You understand binary integers. Extend the concept.

13 in binary is 1101 (1 * eight + 1 * four + 0 * two + 1 * one).

So each column represents a multiplier half as big as that of the column
to its left.

So we might reasonably think of the columns beyond the binary point as
representing a half, a quarter, an eighth, etc.

So 0.11 would be (half + quarter) = (three-quarters) = 0.75

0.011 would be (quarter + eighth) = (three-eighths) = 0.375

and so on.

Is this rule of representing decimals in binary applicable only to
754?
This doesn't represent decimals. It represents values. Values are
neither decimal nor binary, but may be expressed in decimal or binary
or, indeed, any other number system, such as hexadecimal or octal. A
computer stores values expressed internally in binary, but may read in
or write out the values in decimal or other systems.
What Richard showed you is not a complete floating point
representation. It was simply a binary representation of a value. This
is refered to as "fixed point". Floating point representations have a
mantissa part and an exponent part. These parts are (typically)
expressed in binary. For example, instead of representing .75 as .11
in fixed point binary, it might be represented as (just to keep things
simple) an 8 bit binary mantissa, 00000011, and an 8 bit exponent,
11111110 (that's -2 expressed as an 8 bit binary number). This
represents 3 * 2 ** -2 (using ** for exponentiation) or 3 >2. Real
floating point implementations, however, use more bits for the
mantissa and exponent, often with separate sign bits.

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
Jun 20 '07 #14
On Sat, 16 Jun 2007 08:05:28 -0400, Eric Sosman
<es*****@acm-dot-org.invalidwrote:
Mu***************@yahoo.com wrote:
Why does floating point have a rounding error?

Knowledge of the Great Mysteries is reserved for those
who are worthy. To prove your worth, you must undertake a
Quest and complete it successfully. Your Quest, Mukesh, is
to discover what fraction one day is of one week, and express
the answer as a decimal number, using as many decimal places
as are needed for perfect accuracy. When you have done this
I will know you are indeed worthy, and I will reveal to you
the secret origin of rounding errors.
<OTDidn't the French Revolution, among its many variously intriguing
and alarming ideas, (try to) change the week to 10 days? </>

- formerly david.thompson1 || achar(64) || worldnet.att.net
Jul 1 '07 #15
David Thompson <da************@verizon.netwrites:
On Sat, 16 Jun 2007 08:05:28 -0400, Eric Sosman
<es*****@acm-dot-org.invalidwrote:
Mu***************@yahoo.com wrote:
Why does floating point have a rounding error?
Knowledge of the Great Mysteries is reserved for those
who are worthy. To prove your worth, you must undertake a
Quest and complete it successfully. Your Quest, Mukesh, is
to discover what fraction one day is of one week, and express
the answer as a decimal number, using as many decimal places
as are needed for perfect accuracy. When you have done this
I will know you are indeed worthy, and I will reveal to you
the secret origin of rounding errors.

<OTDidn't the French Revolution, among its many variously intriguing
and alarming ideas, (try to) change the week to 10 days? </>
Right. This calendar was in use 13 years or so.

Yours,

--
Jean-Marc
Jul 2 '07 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Mahuskin | last post by:
Using Microsoft Visual C++ .NET 69462-270-0000007-18921 When I build the following code in Visual C++ .NET for release, there is a serious optimization error that corrupts the resulting value ...
14
by: Amit Bhatia | last post by:
Hi there. I am cross posting this on comp.lang.c as well: sorry for same. The problem I am facing is as follows: For example: double a= 0.15; double b=2.4; const double VERYTINY =1.e-10; I...
4
by: jacob navia | last post by:
Hi people I continue to work in the tutorial for lcc-win32, and started to try to explain the floating point flags. Here is the relevant part of the tutorial. Since it is a difficult part, I...
8
by: Jon Rea | last post by:
Hi, I have just tried compiling some scientific calculation code on Redhad Linux using gcc-3.4.4 that I normally compile on MS Visual C++ 03 and 05. I was surprised to get different floating...
7
by: thisismyidentity | last post by:
Hi all, I am trying to predict the behaviour of floating point load and store operations on integer locations. I ve written a small piece of code having some inline assembly code which I am...
13
by: Shirsoft | last post by:
I have a 32 bit intel and 64 bit AMD machine. There is a rounding error in the 8th digit. Unfortunately because of the algorithm we use, the errors percolate into higher digits. C++ code is...
4
by: hg | last post by:
Hi, Here is my issue: f = 1.5 * 0.01 f '%f' % f But I really want to get 0.02 as a result ... is there a way out ?
26
by: y8ycgl | last post by:
i have a program that i made in my class in Turbo C++ on windows 95or98 i belive. it is made to solve for factorials. th problem is that : A) it puts it in exponents B) theres too many numbers to...
31
by: Mark Dickinson | last post by:
On SuSE 10.2/Xeon there seems to be a rounding bug for floating-point addition: dickinsm@weyl:~python Python 2.5 (r25:51908, May 25 2007, 16:14:04) on linux2 Type "help", "copyright",...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.