473,465 Members | 1,964 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Garbage data in float datatype - Mingw3.2 (gcc)

{
float F=123.456000;

F*=1000; // Actually I used a for loop F*=10 three times.

printf("%f\n", F);
}

This will produce something like 123456.00XXXX, where XXXX are garbage
digits. Why is this happening, and how do I get rid of it? (using Dev-C++
with Mingw3.2 [gcc])

Gerald Lafreniere
sp*******@shaw.ca
Nov 14 '05 #1
16 2523
Gerald Lafreniere wrote:
{
float F=123.456000;

F*=1000; // Actually I used a for loop F*=10 three times.

printf("%f\n", F);
}

This will produce something like 123456.00XXXX, where XXXX are garbage
digits. Why is this happening, and how do I get rid of it? (using Dev-C++
with Mingw3.2 [gcc])


This is Question 14.1 in the comp.lang.c Frequently
Asked Questions (FAQ) list

http://www.eskimo.com/~scs/C-faq/top.html

Perhaps we should petition the FAQ maintainer to renumber
it to "Question 14.100000381" ...

--
Er*********@sun.com

Nov 14 '05 #2
Thanks,

I never knew about this faq.
I just learnt something new today.

Gerald Lafreniere
sp*******@shaw.ca
"Eric Sosman" <er*********@sun.com> wrote in message
news:cr**********@news1brm.Central.Sun.COM...
Gerald Lafreniere wrote:
{
float F=123.456000;

F*=1000; // Actually I used a for loop F*=10 three times.

printf("%f\n", F);
}

This will produce something like 123456.00XXXX, where XXXX are garbage
digits. Why is this happening, and how do I get rid of it? (using Dev-C++ with Mingw3.2 [gcc])


This is Question 14.1 in the comp.lang.c Frequently
Asked Questions (FAQ) list

http://www.eskimo.com/~scs/C-faq/top.html

Perhaps we should petition the FAQ maintainer to renumber
it to "Question 14.100000381" ...

--
Er*********@sun.com

Nov 14 '05 #3
On Fri, 07 Jan 2005 19:48:04 GMT, Gerald Lafreniere
<ge***************@apollomachine.com> wrote:
{
float F=123.456000;

F*=1000; // Actually I used a for loop F*=10 three times.

printf("%f\n", F);
}

This will produce something like 123456.00XXXX, where XXXX are garbage
digits. Why is this happening, and how do I get rid of it? (using Dev-C++
with Mingw3.2 [gcc])


A float probably only has 24 bits of mantissa (since you specify the
platform, I'm pretty certain that's what it has). That's only 6 full
digits, which is what are printed correctly. The %f in printf, giving
no precision has a default of 6 characters after the decimal point,
which is why you are seeing those extra digits.

If you used %g, that will trim trailing zeros and defaults to a
precision of 6 digits total, which would represent it as just 123456.

Note that float values passed into printf (and its cousins) are extended
to type double (51 bits of precision on the x86 with gcc), there is no
modifier to say "this was a float value originally (in my opinion an
omission in the spec.). If you need extra precision, you can use type
double instead of float.

See the C specification paragraph 7.19.6.1 (it's actually fprintf there,
but it describes the formats allowed and what they do). Or since you
are running Mingw you may be able to do info gcc and search for printf,
or do man printf, to give you the same information.

If you want to see how many bits are represented by each type, look in
header float.h for your platform. The one on my system define values:

/* Radix of exponent representation */
#define FLT_RADIX 2

/* Number of base-FLT_RADIX digits in the significand of a float */
#define FLT_MANT_DIG 24

/* Number of decimal digits of precision in a float */
#define FLT_DIG 6

/* Addition rounds to 0: zero, 1: nearest, 2: +inf, 3: -inf, -1: unknown */
#define FLT_ROUNDS 1

/* Difference between 1.0 and the minimum float greater than 1.0 */
#define FLT_EPSILON 1.19209290e-07F

/* Minimum int x such that FLT_RADIX**(x-1) is a normalised float */
#define FLT_MIN_EXP (-125)

/* Minimum normalised float */
#define FLT_MIN 1.17549435e-38F

/* Minimum int x such that 10**x is a normalised float */
#define FLT_MIN_10_EXP (-37)

/* Maximum int x such that FLT_RADIX**(x-1) is a representable float */
#define FLT_MAX_EXP 128

/* Maximum float */
#define FLT_MAX 3.40282347e+38F

/* Maximum int x such that 10**x is a representable float */
#define FLT_MAX_10_EXP 38

The same sort of thing with DBL instead of FLT for double variables, and
with LDBL for long double (these are specified -- the macros, not the
precise values -- in the C specification).

The C specification is available to buy from ANSI, go to

http://webstore.ansi.org/ansidocstore

and search for 9899. You probably don't want to buy the ISO version
(US$183 or $278), get the ANSI one for $18 as a PDF. The two Corrigenda
are free (both also PDF). I make a lot of reference to it.
Unfortunately the previous (1989) standard is not available
electronically.

Chris C
Nov 14 '05 #4
Gerald Lafreniere wrote:
"Eric Sosman" <er*********@sun.com> wrote in message
Gerald Lafreniere wrote:
{
float F=123.456000;
F*=1000; // Actually I used a for loop F*=10 three times.
printf("%f\n", F);
}

This will produce something like 123456.00XXXX, where XXXX are
garbage digits. Why is this happening, and how do I get rid of
it? (using Dev-C++ with Mingw3.2 [gcc])


This is Question 14.1 in the comp.lang.c Frequently
Asked Questions (FAQ) list

http://www.eskimo.com/~scs/C-faq/top.html

Perhaps we should petition the FAQ maintainer to renumber
it to "Question 14.100000381" ...


I never knew about this faq.
I just learnt something new today.


Good. Now you should learn not to top-post (which I corrected
here). Your answer belongs after, or intermixed with, the material
to which you are responding, with anything immaterial to your reply
snipped out. The snippage is also important.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #5
On Fri, 07 Jan 2005 19:48:04 GMT, "Gerald Lafreniere"
<ge***************@apollomachine.com> wrote:
{
float F=123.456000;

F*=1000; // Actually I used a for loop F*=10 three times.

printf("%f\n", F);
}

This will produce something like 123456.00XXXX, where XXXX are garbage
digits. Why is this happening, and how do I get rid of it? (using Dev-C++
with Mingw3.2 [gcc])


This is not primarily "garbage" but "loss of precision". Ok, of
course, it *is* garbage, but one that can be predicted.
float stores floating point data in 4 bytes and is splitted into a
"mantissa"- and an "exponent"-part: 0.123456 * 10^3

In some cases, the mantissa has not enough space, to precisely encode
the number. This is because of the internal representation of the
number in the mantissa (the number is stored as a sum of 2^-x).

Manipulations to the number (ie. "calculations") cause the error to
show up or aggravate.

In your case, switching to a floating point data type with a higher
precision (double) or to BCD-float-arithmetic could help.
--
,,,
_ _ \(((.
__,,../v\,----../ `-..=.>"" _\,_
_______;/____<_ \_______\ \___////______;__*********@pukys.de_______
,"/ `.) `.) ```
/," /7__ /7_
(( ' \\\ )))
)
/
Nov 14 '05 #6
Frank Schmied wrote:
<ge***************@apollomachine.com> wrote:
{
float F=123.456000;
F*=1000; // Actually I used a for loop F*=10 three times.
printf("%f\n", F);
}

This will produce something like 123456.00XXXX, where XXXX are
garbage digits. Why is this happening, and how do I get rid of
it? (using Dev-C++ with Mingw3.2 [gcc])


This is not primarily "garbage" but "loss of precision". Ok, of
course, it *is* garbage, but one that can be predicted.
float stores floating point data in 4 bytes and is splitted into
a "mantissa"- and an "exponent"-part: 0.123456 * 10^3

^^^^^^^^^^
The correct word is "significand". Mantissas are part of
logarithms. The size is also specified by sizeof(float), and has
no need to be 4. See <float.h> for details on your particular
system.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #7
/me salutes his furer
Nov 14 '05 #8
Thanks for the advice.

I was trying to automatically determine the precision of a decimal number.
I can do it with whole numbers, but once the decimal kicks in, it just
doesn't work properly(because of the garbage).

for ( precision=0, F-=(float) ( (int) F ); F!=0; ++precision, F*=10,
F-=(float) ( (int) F ) );

My work around will be to just specify the precision.

Gerald Lafreniere
sp*******@shaw.ca


Nov 14 '05 #9
Gerald Lafreniere wrote:

I was trying to automatically determine the precision of a decimal
number. I can do it with whole numbers, but once the decimal kicks
in, it just doesn't work properly(because of the garbage).

for ( precision=0, F-=(float) ( (int) F ); F!=0; ++precision, F*=10,
F-=(float) ( (int) F ) );

My work around will be to just specify the precision.


<float.h> has all that information for your system.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

Nov 14 '05 #10
"Gerald Lafreniere" <ge***************@apollomachine.com> writes:
/me salutes his furer


Insulting people who give you good advice is an effective way to get
yourself into a lot of people's killfiles.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #11
I like advice, but outright telling what to do in a condescending way, and
as if your the boss(when you're not!), puts you in bad terms with me.

If you're going to push etiquette, then you should at least be polite about
it and ask someone to do something, instead of telling them to do it,
otherwise you're what I call a hypocrite.

Gerald Lafreniere
sp*******@shaw.ca
Nov 14 '05 #12
Thanks for all the good advice and good information, I learnt a lot here,
and I'm gr8full.
Nov 14 '05 #13
Gerald Lafreniere wrote:

I like advice, but outright telling what to do in a condescending way, and
as if your the boss(when you're not!), puts you in bad terms with me.


The lack of context here is a problem. I haven't seen the article that
prompted this reply (yet), because it hasn't reached my server. I have
no idea who you're talking about or why. If you're going to fight in
public, you might at least indicate with whom you are fighting and
why. Leaving relevant context in your article as a quote allows that.

Better still, in future, please fight your good fight in
email! Thanks. :-)
Nov 14 '05 #14
"Gerald Lafreniere" <ge***************@apollomachine.com> writes:
Thanks for all the good advice and good information, I learnt a lot here,
and I'm gr8full.


Glad to hear it.

Here's some more advice for you. You should provide some context when
you post a followup. The format for doing so is fairly simple.

I also advise you not to take things too personally. If someone
doesn't phrase his advice in the way you like, publicly insulting them
is not an appropriate response.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #15
infobahn wrote:
Gerald Lafreniere wrote:

I like advice, but outright telling what to do in a condescending
way, and as if your the boss(when you're not!), puts you in bad
terms with me.
The lack of context here is a problem. I haven't seen the article
that prompted this reply (yet), because it hasn't reached my server.
I have no idea who you're talking about or why. If you're going to
fight in public, you might at least indicate with whom you are
fighting and why. Leaving relevant context in your article as a
quote allows that.

Better still, in future, please fight your good fight in
email! Thanks. :-)


I believe he got his water hot over this rude response of mine: Gerald Lafreniere wrote:
"Eric Sosman" <er*********@sun.com> wrote in message
Gerald Lafreniere wrote:
.... snip ...
http://www.eskimo.com/~scs/C-faq/top.html

Perhaps we should petition the FAQ maintainer to renumber
it to "Question 14.100000381" ...


I never knew about this faq.
I just learnt something new today.


Good. Now you should learn not to top-post (which I corrected
here). Your answer belongs after, or intermixed with, the material
to which you are responding, with anything immaterial to your reply
snipped out. The snippage is also important.

.... snip ...

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #16
Keith Thompson wrote:
"Gerald Lafreniere" <ge***************@apollomachine.com> writes:
Thanks for all the good advice and good information, I learnt a lot here,
and I'm gr8full.

Glad to hear it.

Here's some more advice for you. You should provide some context when
you post a followup. The format for doing so is fairly simple.

I also advise you not to take things too personally. If someone
doesn't phrase his advice in the way you like, publicly insulting them
is not an appropriate response.


Yeh, I know, I got a little heated about that, and I apologize.
Nov 14 '05 #17

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

Similar topics

3
by: rdraider | last post by:
I'm doing a data conversion project, moving data from one SQL app to another. I'm using INSERT INTO with Select and have the syntax correct. But when executing the script I get: Server: Msg...
7
by: Dave | last post by:
We are trying to migrate a MS SQL server app to DB2 8.1 Linux platform. Our database has got about 300+tables with total size - 150 GB We are using MS SQL's BCP utility to extract data from...
2
by: Mathias Picker | last post by:
While trying to install the roundup issue tracking system I discovered the following - faulty - behaviour: My fresh postgres 7.4.2 install on a FreeBSD 5.2-CURRENT machine replies "ERROR: ...
0
by: sonu | last post by:
I have following client side code which i have used in my asp.net project SummaryFeatured Resources from the IBM Business Values Solution Center WHITEPAPER : CRM Done Right Improve the...
8
by: abdul_n_khan | last post by:
Hello, I have a basic question related to datatypes. I am trying to read a value using Microsoft's ADO recordset from a field (lets call it 'Price') with datatype decimal(19,6) => 19 = Precision,...
8
by: David Wade | last post by:
Folks, Well I am still dabling in the mire of math.h and getting on reasonably well. A couple of questions. Firstly when defining some of the extreme values in the many bits of code seem to use...
1
by: joeedh | last post by:
Hi I'm getting extremely odd behavior. First of all, why isn't PyEval_EvalCode documented anywhere? Anyway, I'm working on blender's python integration (it embeds python, as opposed to python...
2
by: siri11 | last post by:
hi.. My application is in c#.net (windows app) MS Visual studio 2005 ,version 2.0 Backend - Sqlserver can anyone plzz suggest me as to how to assign a datatype in sql server for a column(field)...
0
by: Terry Reedy | last post by:
Prashant Saxena wrote: A property with a working get and set that raises an exception. Don't know. I believe this is what slots is for.
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

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.