473,647 Members | 3,347 Online
Bytes | Software Development & Data Engineering Community
+ 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 2547
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*********@su n.com> wrote in message
news:cr******** **@news1brm.Cen tral.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************ ***@apollomachi ne.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*********@su n.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********@yah oo.com) (cb********@wor ldnet.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************ ***@apollomachi ne.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. "calculatio ns") 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************ ***@apollomachi ne.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 "significan d". 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********@yah oo.com) (cb********@wor ldnet.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(becaus e 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(becaus e 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********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!

Nov 14 '05 #10

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

Similar topics

3
12093
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 8114, Level 16, State 5, Line 1 Error converting data type varchar to float. Is it possible to change the data type during the INSERT INTO statement?
7
8063
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 MS SQL's tables and loading into DB2 using DB2's LOAD utility. There are tons of colums of floating point types (singe precion & double precision types) in the database and when extracted using BCP, it generates data only upto 17 digits....
2
2063
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: precision for FLOAT must be less than 16" to "create table otks (otk_key varchar(255),otk_value
0
12070
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 likelihood of CRM success from less than 20 percent to 60 percent. WHITEPAPER :
8
5451
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, 6 = Scale 1) When I read this field into float datatype. I get a value 1.9000, which is correct. But when I read its value in a double datatype I get 1.8999999761581.
8
2428
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 "extern" definitions of various types. Is it OK to do something like this in float.h:- extern _ex_dbl_vals; DBL_MAX _ex_dbl_vals;
1
2209
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 embedding it). I have a function that executes a string buffer of python code, fetches a function from its global dictionary then calls it. When the function code returns a local variable, PyObject_Call() appears to be returning garbage. ...
2
1191
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) which should accept decimal,double or float data as per the result which is obtained in front end. The problem is I have given the "Float" datatype to this field.And for ex: If the result is 12.56 then it is storing the value as 12.5600004196167 in...
0
863
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.
0
8210
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8645
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8371
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8516
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7209
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6126
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4209
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2633
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1516
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.