This is a group for folks designing and programming embedded systems using the Rabbit Semiconductor C-programmable microcontroller. Rabbit Semi is a spin-off from Z-World who makes a variety of embedded modules and tools. This group is not affiliated with either Rabbit or Z-World, but is a user forum for sharing ideas, asking questions,
flaunting knowledge, and other typical user group stuff. The Rabbit is a powerful uC, supported by a full-featured C-compiler.
Floating point precision - Dave McLaughlin - Oct 29 5:17:11 2009
Hi All,
Anyone know how I can guarantee floating point precision with a number input
from the user.
I need to input coefficients for a pressure sensor and I need to be able to
get them to 10 decimal places. So far when I input as a string and then do a
ATOF on them, the value is truncated to about 5 or 6 decimal places at most.
I need to be able to input the value to at least 10 decimal places.
If I create a small application with 1 float variable and I assign a 10
decimal place number to it, then printf if to 10 decimal places I get the
same value back. For some reason, ATOF is not achieving this for me with the
user input value.
Any ideas how I could achieve this?
Dave...
---
Very funny Scotty, now beam down my clothes!!!
---
http://www.embeddedcomputer.co.uk
---

(You need to be a member of rabbit-semi -- send a blank email to rabbit-semi-subscribe@yahoogroups.com )
Re: Floating point precision - mehiegl - Oct 29 8:13:04 2009
See this thread. It may shed a little light on your problem.
http://tech.groups.yahoo.com/group/rabbit-semi/message/32958
--- In r...@yahoogroups.com, "Dave McLaughlin"
wrote:
>
> Hi All,
>
>
>
> Anyone know how I can guarantee floating point precision with a number input
> from the user.
>
>
>
> I need to input coefficients for a pressure sensor and I need to be able to
> get them to 10 decimal places. So far when I input as a string and then do a
> ATOF on them, the value is truncated to about 5 or 6 decimal places at most.
>
>
>
> I need to be able to input the value to at least 10 decimal places.
>
>
>
> If I create a small application with 1 float variable and I assign a 10
> decimal place number to it, then printf if to 10 decimal places I get the
> same value back. For some reason, ATOF is not achieving this for me with the
> user input value.
>
>
>
>
>
> Any ideas how I could achieve this?
>
>
>
>
>
> Dave...
>
> ---
> Very funny Scotty, now beam down my clothes!!!
> ---
> http://www.embeddedcomputer.co.uk
> ---
>
------------------------------------

(You need to be a member of rabbit-semi -- send a blank email to rabbit-semi-subscribe@yahoogroups.com )Re: Floating point precision - Adrian Mojoatca - Oct 29 12:07:28 2009
Hi,
DC manual describes "float" data type as it follows:
"32-bit IEEE floating-point value. The sign bit is 1 for negative values. The exponent has
8 bits, giving exponents from -127 to +128. The mantissa has 24 bits. Only the 23 least
significant bits are stored; the high bit is 1 implicitly. (Rabbit controllers do not have
floating-point hardware.) Range: 1.18 x 10-38 to 3.40 x 1038 "
Even you cannot use a floating point number with more than 6 decimal places (anyway ANSI
stipulates, as far as i know 6 decimal places for a float and 10 for a double), you can
convert your value (from a string if i understand well) into two long integers (one for
the integer part and one for the decimal part of your float value).
You can use the code bellow:
void makeAFloatFromMe ( char *sz_buff, unsigned long int *int_part, unsigned long int
*dec_part )
{
char *p;
int j,len;
p = sz_buff;
*int_part = atol(p);
len = 0;
while ( *p != '.' ) p++,len++;
for ( j = len+1; j < strlen(sz_buff); j++ ) if ( sz_buff[j] != 0x30 ) {*dec_part =
atol ( sz_buff+j); break;}
}
void main (void)
{
unsigned long int int_part, dec_part;
makeAFloatFromMe("9999.0000000789",&int_part, &dec_part);
printf ( "AS INPUT STRING:9999.0000000789\r\n");
printf ( "AS RESULT %ld.%010ld\r\n", int_part,dec_part );
makeAFloatFromMe("9999.0001000789",&int_part, &dec_part);
printf ( "AS INPUT STRING:9999.0001000789\r\n");
printf ( "AS RESULT %ld.%010ld\r\n", int_part,dec_part );
makeAFloatFromMe("9999.1000000789",&int_part, &dec_part);
printf ( "AS INPUT STRING:9999.1000000789\r\n");
printf ( "AS RESULT %ld.%010ld\r\n", int_part,dec_part );
makeAFloatFromMe("9999.1000000000",&int_part, &dec_part);
printf ( "AS INPUT STRING:9999.1000000000\r\n");
printf ( "AS RESULT %ld.%010ld\r\n", int_part,dec_part );
}
Good luck!
Adrian
--- On Thu, 10/29/09, Dave McLaughlin
wrote:
From: Dave McLaughlin
Subject: [rabbit-semi] Floating point precision
To: r...@yahoogroups.com
Date: Thursday, October 29, 2009, 2:15 AM
Hi All,
Anyone know how I can guarantee floating point precision with a number input from the
user.
I need to input coefficients for a pressure sensor and I need to be able to get them to 10
decimal places. So far when I input as a string and then do a ATOF on them, the value is
truncated to about 5 or 6 decimal places at most.
I need to be able to input the value to at least 10 decimal places.
If I create a small application with 1 float variable and I assign a 10 decimal place
number to it, then printf if to 10 decimal places I get the same value back. For some
reason, ATOF is not achieving this for me with the user input value.
Any ideas how I could achieve this?
Dave...
---
Very funny Scotty, now beam down my clothes!!!
---
http://www.embedded computer. co.uk
---
______________________________
Stellaris® MCU Family: New Parts, New Package, New Price.

(You need to be a member of rabbit-semi -- send a blank email to rabbit-semi-subscribe@yahoogroups.com )RE: Floating point precision - Don Starr - Oct 29 13:16:33 2009
>Even you cannot use a floating point number with more than 6 decimal places
>(anyway ANSI stipulates, as far as i know 6 decimal places for a float and 10
>for a double)
We really shouldn't confuse "number of decimal places" with what can be
stored in the IEEE-754 floating point format used by DC*. The two have
little to do with each other. What matters is whether a given number can
be represented in the IEEE-754 format.
For example, 1.99999988079071044921875 can be stored exactly - even though
it has far more than 6 (or even 10) significant digits.
However, 102.7, with only 4 significant digits, CANNOT be stored exactly.
If you need to manipulate floating point numbers *exactly*, you'll have to
either find or write code to handle them.
* The issue isn't limited to DC, of course. It's present in any compiler,
programming language, library, and/or floating point hardware that uses
IEEE-754.
------------------------------------
______________________________
Stellaris® MCU Family: New Parts, New Package, New Price.
(You need to be a member of rabbit-semi -- send a blank email to rabbit-semi-subscribe@yahoogroups.com )
Re: Floating point precision - fairy_dave - Oct 30 0:40:04 2009
Do you need just really tiny numbers or accuracy over 10 significant figures? While you
can get a number in the range of 0.000000001 into the Rabbit you have no chance of getting
1.0000000001 into it. I did some tests a while back on this and found that you can have
about 5 to 8 significant figures reliably in the Rabbit floating point (it depends on the
exact number, some numbers fit perfectly into floating point, others not so well)
--- In r...@yahoogroups.com, "Dave McLaughlin"
wrote:
>
> Hi All,
>
>
>
> Anyone know how I can guarantee floating point precision with a number input
> from the user.
>
>
>
> I need to input coefficients for a pressure sensor and I need to be able to
> get them to 10 decimal places. So far when I input as a string and then do a
> ATOF on them, the value is truncated to about 5 or 6 decimal places at most.
>
>
>
> I need to be able to input the value to at least 10 decimal places.
>
>
>
> If I create a small application with 1 float variable and I assign a 10
> decimal place number to it, then printf if to 10 decimal places I get the
> same value back. For some reason, ATOF is not achieving this for me with the
> user input value.
>
>
>
>
>
> Any ideas how I could achieve this?
>
>
>
>
>
> Dave...
>
> ---
> Very funny Scotty, now beam down my clothes!!!
> ---
> http://www.embeddedcomputer.co.uk
> ---
>
------------------------------------
______________________________
Stellaris® MCU Family: New Parts, New Package, New Price.

(You need to be a member of rabbit-semi -- send a blank email to rabbit-semi-subscribe@yahoogroups.com )RE: Re: Floating point precision - Nathan Johnston - Oct 30 0:53:43 2009
There was some correspondence about this quite a few years ago when I
had a floating point accuracy issue. A processor does floating point
calculations by first rationalizing both numbers. I believe the
difference between the Rabbit (and other "basic" processors) and an X86
is that an X86 saves the mantissa as a full 32 bits whereas the Rabbit
doesn't (during the calculations).
What does this mean in plane English? You can accurately do maths on
numbers of similar magnitude but if the magnitudes are massively
difference then expect a small error. If you are clever you can re-order
your calculations to minimize precision loss which is what I had to do
when developing some equipment for a surveying application.
Regards,
Nathan
-----Original Message-----
From: r...@yahoogroups.com [mailto:r...@yahoogroups.com]
On Behalf Of fairy_dave
Sent: Friday, 30 October 2009 3:39 PM
To: r...@yahoogroups.com
Subject: [rabbit-semi] Re: Floating point precision
Do you need just really tiny numbers or accuracy over 10 significant
figures? While you can get a number in the range of 0.000000001 into the
Rabbit you have no chance of getting 1.0000000001 into it. I did some
tests a while back on this and found that you can have about 5 to 8
significant figures reliably in the Rabbit floating point (it depends on
the exact number, some numbers fit perfectly into floating point, others
not so well)
--- In r...@yahoogroups.com
, "Dave McLaughlin"
wrote:
>
> Hi All,
>
> Anyone know how I can guarantee floating point precision with a number
input
> from the user.
>
> I need to input coefficients for a pressure sensor and I need to be
able to
> get them to 10 decimal places. So far when I input as a string and
then do a
> ATOF on them, the value is truncated to about 5 or 6 decimal places at
most.
>
> I need to be able to input the value to at least 10 decimal places.
>
> If I create a small application with 1 float variable and I assign a
10
> decimal place number to it, then printf if to 10 decimal places I get
the
> same value back. For some reason, ATOF is not achieving this for me
with the
> user input value.
>
> Any ideas how I could achieve this?
>
> Dave...
>
> ---
> Very funny Scotty, now beam down my clothes!!!
> ---
>
> http://www.embeddedcomputer.co.uk
> ---
>
______________________________
Stellaris® MCU Family: New Parts, New Package, New Price.

(You need to be a member of rabbit-semi -- send a blank email to rabbit-semi-subscribe@yahoogroups.com )Re: RE: Floating point precision - Adrian Mojoatca - Oct 30 5:16:17 2009
Don,
The "float" type in DC is represented on 4 bytes. The "double" is represented on 4 bytes
also, either in other IDEs (Visual C++ for ex) it's stored on 8 bytes. This means you can
have only 6 or 7 decimal places (depending on the number you store). A given number, for
example the one you mentioned, can be stored, but not exactly (only with 7 decimal
places). That's single precision.
For example, you can use the converter from this page:
http://www.h-schmidt.net/FloatApplet/IEEE754.html, and test your number. You'll see that
only 7 decimal places are accurate.
Regards.
--- On Thu, 10/29/09, Don Starr
wrote:
From: Don Starr
Subject: [rabbit-semi] RE: Floating point precision
To: r...@yahoogroups.com
Date: Thursday, October 29, 2009, 10:16 AM
>Even you cannot use a floating point number with more than 6 decimal places
>(anyway ANSI stipulates, as far as i know 6 decimal places for a float and 10
>for a double)
We really shouldn't confuse "number of decimal places" with what can be
stored in the IEEE-754 floating point format used by DC*. The two have
little to do with each other. What matters is whether a given number can
be represented in the IEEE-754 format.
For example, 1.99999988079071044 921875 can be stored exactly - even though
it has far more than 6 (or even 10) significant digits.
However, 102.7, with only 4 significant digits, CANNOT be stored exactly.
If you need to manipulate floating point numbers *exactly*, you'll have to
either find or write code to handle them.
* The issue isn't limited to DC, of course. It's present in any compiler,
programming language, library, and/or floating point hardware that uses
IEEE-754.

(You need to be a member of rabbit-semi -- send a blank email to rabbit-semi-subscribe@yahoogroups.com ) Re: RE: Floating point precision - Maurits van de Kamp - Oct 30 5:26:21 2009
Hi all,
The 'moral' of the story :) is that floating point isn't so much
meant for extreme accuracy, but for a constant accuracy across a huge
scale (hence you want the point to float). :)
If what you want is a fixed number of *decimals* in a limited range
(for example if the number will always be between 0 or 1), it's much
better to use a longword and manually put a decimal point in during
display. An unsigned long can hold numbers between 0 and
4,294,967,295 so with the right conversion you could use it for
storing 8 decimal places (or even 9 if the range happens to fit). :)
Maurits.
Op Oct 30, 2009, om 10:14 AM heeft Adrian Mojoatca het volgende
geschreven:
> Don,
>
> The "float" type in DC is represented on 4 bytes. The "double" is
> represented on 4 bytes also, either in other IDEs (Visual C++ for
> ex) it's stored on 8 bytes. This means you can have only 6 or 7
> decimal places (depending on the number you store). A given number,
> for example the one you mentioned, can be stored, but not exactly
> (only with 7 decimal places). That's single precision.
> For example, you can use the converter from this page: http://www.h-
> schmidt.net/FloatApplet/IEEE754.html, and test your number. You'll
> see that only 7 decimal places are accurate.
>
> Regards.
>
> --- On Thu, 10/29/09, Don Starr
wrote:
>
> From: Don Starr
> Subject: [rabbit-semi] RE: Floating point precision
> To: r...@yahoogroups.com
> Date: Thursday, October 29, 2009, 10:16 AM
>
> >Even you cannot use a floating point number with more than 6
> decimal places
> >(anyway ANSI stipulates, as far as i know 6 decimal places for a
> float and 10
> >for a double)
>
> We really shouldn't confuse "number of decimal places" with what
> can be
> stored in the IEEE-754 floating point format used by DC*. The two have
> little to do with each other. What matters is whether a given
> number can
> be represented in the IEEE-754 format.
>
> For example, 1.99999988079071044 921875 can be stored exactly -
> even though
> it has far more than 6 (or even 10) significant digits.
>
> However, 102.7, with only 4 significant digits, CANNOT be stored
> exactly.
>
> If you need to manipulate floating point numbers *exactly*, you'll
> have to
> either find or write code to handle them.
>
> * The issue isn't limited to DC, of course. It's present in any
> compiler,
> programming language, library, and/or floating point hardware that
> uses
> IEEE-754.
>

(You need to be a member of rabbit-semi -- send a blank email to rabbit-semi-subscribe@yahoogroups.com )RE: {Disarmed} Re: Floating point precision - Dave McLaughlin - Oct 30 5:28:07 2009
Thanks Dave,
I need to get as good a precision as I can but as I have discovered, the 32
bit float is severely limited so I am stuck with having to use lower (7
decimal places) values instead. With this design this works out at about
4psi for certain temperatures but as the actual pressure sensor is only as
good as 12.5 PSI full scale we will live with this error for this design. It
still remains within the design specification we were aiming for anyway.
Dave...
---
Very funny Scotty, now beam down my clothes!!!
---
http://www.embeddedcomputer.co.uk
---
From: r...@yahoogroups.com [mailto:r...@yahoogroups.com] On
Behalf Of fairy_dave
Sent: 30 October 2009 11:39
To: r...@yahoogroups.com
Subject: {Disarmed} [rabbit-semi] Re: Floating point precision
Do you need just really tiny numbers or accuracy over 10 significant
figures? While you can get a number in the range of 0.000000001 into the
Rabbit you have no chance of getting 1.0000000001 into it. I did some tests
a while back on this and found that you can have about 5 to 8 significant
figures reliably in the Rabbit floating point (it depends on the exact
number, some numbers fit perfectly into floating point, others not so well)

(You need to be a member of rabbit-semi -- send a blank email to rabbit-semi-subscribe@yahoogroups.com )
RE: {Disarmed} Re: RE: Floating point precision - Dave McLaughlin - Oct 30 5:29:46 2009
Thanks Adrian,
That website is pretty useful and I have tried all the values I want to use.
Lowering my requirements to less decimal places does seem to give me a
fighting chance.
Cheers
Dave...
---
Very funny Scotty, now beam down my clothes!!!
---
http://www.embeddedcomputer.co.uk
---
From: r...@yahoogroups.com [mailto:r...@yahoogroups.com] On
Behalf Of Adrian Mojoatca
Sent: 30 October 2009 16:15
To: r...@yahoogroups.com
Subject: {Disarmed} Re: [rabbit-semi] RE: Floating point precision
Don,
The "float" type in DC is represented on 4 bytes. The "double" is
represented on 4 bytes also, either in other IDEs (Visual C++ for ex) it's
stored on 8 bytes. This means you can have only 6 or 7 decimal places
(depending on the number you store). A given number, for example the one you
mentioned, can be stored, but not exactly (only with 7 decimal places).
That's single precision.
For example, you can use the converter from this page:
http://www.h-schmidt.net/FloatApplet/IEEE754.html, and test your number.
You'll see that only 7 decimal places are accurate.
Regards.

(You need to be a member of rabbit-semi -- send a blank email to rabbit-semi-subscribe@yahoogroups.com )
RE: RE: Floating point precision - Don Starr - Oct 30 10:27:56 2009
>The "float" type in DC is represented on 4 bytes. The "double" is
>represented on 4 bytes also, either in other IDEs (Visual C++ for
>ex) it's stored on 8 bytes. This means you can have only 6 or=A07
>decimal places (depending on the number you store).
Yes, I'm aware of how the "float" type is stored according to
IEEE-754. This discussion took place in some detail back in
May 2004:
http://tech.groups.yahoo.com/group/rabbit-semi/message/21374
(though that post has an error in an exponent within a binary
representation).
>A given number, for example the one you mentioned, can be stored,
>but not exactly (only with 7 decimal places). That's single precision.
The number I mentioned:
1.99999988079071044921875
can be stored EXACTLY in IEEE-754. It is the quotient:
16777215 / 8388608
which does fit, EXACTLY, into the format. The corresponding 32-bit
"float" value is:
0 01111111 11111111111111111111111
S EEEEEEEE MMMMMMMMMMMMMMMMMMMMMMM
(S=3Dsign, E=3Dexponent, M=3Dmantissa)
value =3D (-1)**S * 2**(E - 127) * (1 + M/(2**23))
('**' means "to the power")
>For example, you can use the converter from this page:
>http://www.h-schmidt.net/FloatApplet/IEEE754.html, and test your=20
>number. You'll see that only 7 decimal places are accurate.
That converter, or the associated display function, is faulty. There
is no reason why, given the bit pattern above, it should not display
all 24 digits.
-Don
--- On Thu, 10/29/09, Don Starr
wrote:
From: Don Starr
Subject: [rabbit-semi] RE: Floating point precision
To: r...@yahoogroups.com
Date: Thursday, October 29, 2009, 10:16 AM
=A0=20
>Even you cannot use a floating point number with more than 6 decimal place=
s
>(anyway ANSI stipulates, as far as i know 6 decimal places for a float and=
10
>for a double)
We really shouldn't confuse "number of decimal places" with what can be
stored in the IEEE-754 floating point format used by DC*. The two have
little to do with each other. What matters is whether a given number can
be represented in the IEEE-754 format.
For example, 1.99999988079071044 921875 can be stored exactly - even though
it has far more than 6 (or even 10) significant digits.
However, 102.7, with only 4 significant digits, CANNOT be stored exactly.
If you need to manipulate floating point numbers *exactly*, you'll have to
either find or write code to handle them.=20
* The issue isn't limited to DC, of course. It's present in any compiler,
programming language, library, and/or floating point hardware that uses
IEEE-754.
------------------------------------

(You need to be a member of rabbit-semi -- send a blank email to rabbit-semi-subscribe@yahoogroups.com )Re: Floating point precision - glcoakley - Oct 30 11:34:54 2009
Dave,
I have a *proven* ( sold and used in a product ) variable length floating point library
that is user configurable to the number of bits of accuracy desired.
I provide the encrypted source so that it can be compiled and used as desired.
Contact me at g...@cbcc.cc for more details. Some of the longer term members on here can
verify my bonafides and I'm a Registered Rabbit Consultant.
Gary L. Coakley
--- In r...@yahoogroups.com, "Dave McLaughlin"
wrote:
>
> Hi All,
>
>
>
> Anyone know how I can guarantee floating point precision with a number input
> from the user.
>
>
>
> I need to input coefficients for a pressure sensor and I need to be able to
> get them to 10 decimal places. So far when I input as a string and then do a
> ATOF on them, the value is truncated to about 5 or 6 decimal places at most.
>
>
>
> I need to be able to input the value to at least 10 decimal places.
>
>
>
> If I create a small application with 1 float variable and I assign a 10
> decimal place number to it, then printf if to 10 decimal places I get the
> same value back. For some reason, ATOF is not achieving this for me with the
> user input value.
>
>
>
>
>
> Any ideas how I could achieve this?
>
>
>
>
>
> Dave...
>
> ---
> Very funny Scotty, now beam down my clothes!!!
> ---
> http://www.embeddedcomputer.co.uk
> ---
>
------------------------------------

(You need to be a member of rabbit-semi -- send a blank email to rabbit-semi-subscribe@yahoogroups.com )RE: RE: Floating point precision - Adrian Mojoatca - Oct 30 11:57:00 2009
Hi Don,
Your number ( 1.99999988079071044921875 ) can fit exactly only on more than 32 bit. As far
as I know on 32 bit (4 bytes) you can represent "only" 4294967296 values. Those are not
enough for an "exact" representation of 1.99999988079071044921875.
Indeed, you can show (print) this number with 16 decimal places, but in another
environment (not DC). For ex. try
float f = 1.99999988079071044921875;
CString inf;
inf.Format("%.30f", f );
MessageBox(inf);
in Visual C++ (you can use printf() in a nonMFC app if you want) an you will see that only
16 decimal places are shown correctly.
If you try the same thing in DC:
float f;
f = 1.99999988079071044921875;
printf ( "f: %.30f\r\n", f );
you will get only 8 correct decimal places.
In fact your number is stored as 1.9999998807907104 on a regullar pc (or arm, etc) and
1.99999988 on rabbit.
Anyway, that converter works fine (just put "00111111111111111111111111111111" into binary
representation field and you will get in "With double precision" field your original value
- of course...with 16 decimal places, meaning 1.9999998807907104).
Regards,
Adrian
--- On Fri, 10/30/09, Don Starr
wrote:
From: Don Starr
Subject: RE: [rabbit-semi] RE: Floating point precision
To: r...@yahoogroups.com
Date: Friday, October 30, 2009, 7:27 AM
>The "float" type in DC is represented on 4 bytes. The "double" is
>represented on 4 bytes also, either in other IDEs (Visual C++ for
>ex) it's stored on 8 bytes. This means you can have only 6 or 7
>decimal places (depending on the number you store).
Yes, I'm aware of how the "float" type is stored according to
IEEE-754. This discussion took place in some detail back in
May 2004:
http://tech. groups.yahoo. com/group/ rabbit-semi/ message/21374
(though that post has an error in an exponent within a binary
representation) .
>A given number, for example the one you mentioned, can be stored,
>but not exactly (only with 7 decimal places). That's single precision.
The number I mentioned:
1.99999988079071044 921875
can be stored EXACTLY in IEEE-754. It is the quotient:
16777215 / 8388608
which does fit, EXACTLY, into the format. The corresponding 32-bit
"float" value is:
0 01111111 1111111111111111111 1111
S EEEEEEEE MMMMMMMMMMMMMMMMMMM MMMM
(S=sign, E=exponent, M=mantissa)
value = (-1)**S * 2**(E - 127) * (1 + M/(2**23))
('**' means "to the power")
>For example, you can use the converter from this page:
>http://www.h- schmidt.net/ FloatApplet/ IEEE754.html, and test your
>number. You'll see that only 7 decimal places are accurate.
That converter, or the associated display function, is faulty. There
is no reason why, given the bit pattern above, it should not display
all 24 digits.
-Don
--- On Thu, 10/29/09, Don Starr wrote:
From: Don Starr
Subject: [rabbit-semi] RE: Floating point precision
To: rabbit-semi@ yahoogroups. com
Date: Thursday, October 29, 2009, 10:16 AM
>Even you cannot use a floating point number with more than 6 decimal places
>(anyway ANSI stipulates, as far as i know 6 decimal places for a float and 10
>for a double)
We really shouldn't confuse "number of decimal places" with what can be
stored in the IEEE-754 floating point format used by DC*. The two have
little to do with each other. What matters is whether a given number can
be represented in the IEEE-754 format.
For example, 1.99999988079071044 921875 can be stored exactly - even though
it has far more than 6 (or even 10) significant digits.
However, 102.7, with only 4 significant digits, CANNOT be stored exactly.
If you need to manipulate floating point numbers *exactly*, you'll have to
either find or write code to handle them.
* The issue isn't limited to DC, of course. It's present in any compiler,
programming language, library, and/or floating point hardware that uses
IEEE-754.

(You need to be a member of rabbit-semi -- send a blank email to rabbit-semi-subscribe@yahoogroups.com )RE: Floating point precision - Don Starr - Oct 30 12:44:40 2009
>Hi Don,
>Your number ( 1.99999988079071044921875 ) can fit exactly only on
>more than 32 bit. As far as I know on 32 bit (4 bytes) you can=20
>represent "only" 4294967296=A0values. =A0Those are not enough for an=20
>"exact" representation of=A01.99999988079071044921875.
In a 32-bit "float", you can store slightly less than 4294967296
unique values (some of the bit patterns are reserved for NaN,=20
positive and negative infinity, etc.).
>In fact your number is stored as 1.9999998807907104 on a regullar pc
>(or arm, etc) and 1.99999988 on rabbit.
That is not correct. The value that is **stored**, even on the
Rabbit, is exactly 1.99999988079071044921875. Whether DC's (or=20
anyone else's) formatted-printing routines can actually **display**
the exact, human-readable, decimal representation of the stored=20
data is another matter.
With a 32-bit IEEE-754 value of 00111111111111111111111111111111:
S: 0
E: 01111111
M: 11111111111111111111111
The sign bit is zero, indicating a positive number.
The exponent field is 127. Subtracting the 127 offset, we get
a resulting exponent of zero. Raising 2 to the 0th power gives
us 1.
Working from left to right, the mantissa's 23 bits have these
values:
01: 0.5
02: 0.25
03: 0.125
04: 0.0625
05: 0.03125
06: 0.015625
07: 0.0078125
08: 0.00390625
09: 0.001953125
10: 0.0009765625
11: 0.00048828125
12: 0.000244140625
13: 0.0001220703125
14: 0.00006103515625
15: 0.000030517578125
16: 0.0000152587890625
17: 0.00000762939453125
18: 0.000003814697265625
19: 0.0000019073486328125
20: 0.00000095367431640625
21: 0.000000476837158203125
22: 0.0000002384185791015625
23: 0.00000011920928955078125
(Note that even the text of the "converter page" you linked says
the above: "bit 23 has a value of 1/2, bit 22 has value 1/4 etc.",
though my list above starts numbering at 1. If the page's author
had listed all 23 bits, it would say "bit 1 has value 1/8388608",
which is the 0.00000011920928955078125 in my list above).
Since all 23 bits of the mantissa are set in this example, we
add those 23 values above and get:
0.99999988079071044921875
Adding the implied 24th bit of mantissa, we get:
1.99999988079071044921875
We then multiply by [2 to the (E-127)]. In this case, since
E is 127, we're multiplying by 1. We get:
1.99999988079071044921875
***EXACTLY***
This is the very definition of the IEEE-754 32-bit floating
point format.
=20
------------------------------------

(You need to be a member of rabbit-semi -- send a blank email to rabbit-semi-subscribe@yahoogroups.com )
RE: {Disarmed} Re: Floating point precision - Dave McLaughlin - Oct 30 12:53:21 2009
Hi Gary
Thanks indeed for the offer. The issue I have with the DC floating point
also extends to another compiler I am using for the Atmel AVR that the DC
design talks to. It is limited to 32 bit also and the same rounding errors.
I will however file away your details as I may need your code for a future
design and might even consider putting the code to calculate the
coefficients in the Rabbit design instead. The current design was so that we
could programme the Atmel based units with the coefficients in the factory
and then have it plug into any Rabbit based unit. The work to put this on
the Rabbit would need to be confirmed with the client first.
Cheers
Dave...
---
Very funny Scotty, now beam down my clothes!!!
---
http://www.embeddedcomputer.co.uk
---
From: r...@yahoogroups.com [mailto:r...@yahoogroups.com] On
Behalf Of glcoakley
Sent: 30 October 2009 22:34
To: r...@yahoogroups.com
Subject: {Disarmed} [rabbit-semi] Re: Floating point precision
Dave,
I have a *proven* ( sold and used in a product ) variable length floating
point library that is user configurable to the number of bits of accuracy
desired.
I provide the encrypted source so that it can be compiled and used as
desired.
Contact me at g...@cbcc.cc
for more details. Some of
the longer term members on here can verify my bonafides and I'm a Registered
Rabbit Consultant.
Gary L. Coakley

(You need to be a member of rabbit-semi -- send a blank email to rabbit-semi-subscribe@yahoogroups.com )RE: Floating point precision - Don Starr - Oct 30 15:58:14 2009
>With a 32-bit IEEE-754 value of 00111111111111111111111111111111:
>S: 0
>E: 01111111
>M: 11111111111111111111111
A much simpler example...
32-bit IEEE-754 value: 01110001100000000000000000000000
S: 0
E: 11100011
M: 00000000000000000000000
That bit pattern means "2 to the 100th power", EXACTLY. It should be
obvious that this number has far more than 6 or 8 significant decimal
digits (it has 31): 1267650600228229401496703205376
Of course, it's not likely that DC will actually present all of those
digits via "printf". It will likely chop it off at some (arbitrary?)
point then fill with zeroes. That does not mean, however, that the
EXACT number is not present in the stored data.
Even Microsoft's Visual C++ (6.0) doesn't print that number correctly,
but it DOES assign a literal correctly at compile time:
float lx = 1267650600228229401496703205376.0;
char lb[100];
sprintf( lb, "%50.2f", lx );
The variable
contains the correct bit pattern (0x71800000), which
exactly represents 1267650600228229401496703205376.0, but Microsoft's
"printf" library code does not represent it correctly. ends up
with:
"1267650600228229400000000000000.00".
(I can only imagine that Adrian's deduction of "how many significant
digits are stored" was somehow based on what DC and VC++ gave via printf.
That, of course, is not a valid experiment. It only tells you what that
particular implementation of printf can do; it does NOT tell you what
the stored data means - that's precisely defined by the IEEE-754 spec.)
------------------------------------

(You need to be a member of rabbit-semi -- send a blank email to rabbit-semi-subscribe@yahoogroups.com )