473,386 Members | 1,733 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

precise string representation of float number

Consider following code segment:

#1: double pi = 3.141592653589;
#2: printf("%lf\n", pi);
#3: printf("%1.12lf\n", pi);
#4: printf("%1.15lf\n", pi);

The above code outputs as following:

3.141593
3.141592653589
3.141592653589000

The default output precision of printf is 6, so #2 outputs '3.141593'
with the effect of rounding error. To get the complete string
representation of pi, the precision should be explicitly specified as a
number equal or greater than pi's real precision, e.g., 12 in '%1.12lf'
of #3. But when specified precision is greater than real precision,
just like #4, the annoying trailing zeros are padded.

My question is that how to get the complete/precise string
representation of a float number using C standard library, but not in a
style of printf in which the output precision should be specified as a
great number (it will bring out superfluous trailing zeros).

Could somebody here give me some advices?

Thank you very much.

Nov 15 '05 #1
7 3625
A. L. wrote on 17/09/05 :
Consider following code segment:

#1: double pi = 3.141592653589;
#2: printf("%lf\n", pi);
#3: printf("%1.12lf\n", pi);
#4: printf("%1.15lf\n", pi);

The above code outputs as following:

3.141593
3.141592653589
3.141592653589000

The default output precision of printf is 6, so #2 outputs '3.141593'
with the effect of rounding error. To get the complete string
representation of pi, the precision should be explicitly specified as a
number equal or greater than pi's real precision, e.g., 12 in '%1.12lf'
of #3. But when specified precision is greater than real precision,
just like #4, the annoying trailing zeros are padded.

My question is that how to get the complete/precise string
representation of a float number using C standard library, but not in a
style of printf in which the output precision should be specified as a
great number (it will bring out superfluous trailing zeros).


Use "%g". The 'l' is useless.

#include <stdio.h>
int main (void)
{
double pi = 3.141592653589;
printf ("%g\n", pi);
printf ("%1.12g\n", pi);
printf ("%1.15g\n", pi);
return 0;
}

3.14159
3.14159265359
3.141592653589

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"There are 10 types of people in the world today;
those that understand binary, and those that dont."
Nov 15 '05 #2
In article <11**********************@g14g2000cwa.googlegroups .com>,
A. L. <ba*****@gmail.com> wrote:
My question is that how to get the complete/precise string
representation of a float number using C standard library, but not in a
style of printf in which the output precision should be specified as a
great number (it will bring out superfluous trailing zeros). Could somebody here give me some advices?


There is no way to do that with the standard library. It requires
extra information about the "real" precision of a number is.

Your Pi example dealt with stripping off trailing 0's, but
if I use const double Foo = 1.234567890; then the 0 *is* part of the
precision and stripping it off would be incorrect.

If you do any arithmetic manipulation of numbers, such as Pi + Foo
or Pi / Foo then "something" needs to know how to figure out the
correct precision of the result. C doesn't do that for you.

Any discussion of the "precise" string representation of a float is
doomed to failure if you use the standard machine representations
instead of indefinite-precision libraries and numeric analysis to
figure out how many digits you ought to be calculating.

For example, if you have 4.5678901 then it could happen that
the closest that a 'float' could get to representing that is
4.5678904875 which by the usual rounding rules would round down to
4.567890 and your remove-trailing-zeroes approach would output that as
4.56789 which would not be correct!
--
I was very young in those days, but I was also rather dim.
-- Christopher Priest
Nov 15 '05 #3
I guess the only way could be machine-dependent. In fact, what I want
is complete/precise conversion of float number to string, acting like
some sort of inverse function of atoi, atol, etc.

In Python, there is repr() that can well serve the above job.

You are right. It seems no way to rely on standard library or some
'standard' method in C.
Walter Roberson wrote:
In article <11**********************@g14g2000cwa.googlegroups .com>,
A. L. <ba*****@gmail.com> wrote:
My question is that how to get the complete/precise string
representation of a float number using C standard library, but not in a
style of printf in which the output precision should be specified as a
great number (it will bring out superfluous trailing zeros).

Could somebody here give me some advices?


There is no way to do that with the standard library. It requires
extra information about the "real" precision of a number is.

Your Pi example dealt with stripping off trailing 0's, but
if I use const double Foo = 1.234567890; then the 0 *is* part of the
precision and stripping it off would be incorrect.

If you do any arithmetic manipulation of numbers, such as Pi + Foo
or Pi / Foo then "something" needs to know how to figure out the
correct precision of the result. C doesn't do that for you.

Any discussion of the "precise" string representation of a float is
doomed to failure if you use the standard machine representations
instead of indefinite-precision libraries and numeric analysis to
figure out how many digits you ought to be calculating.

For example, if you have 4.5678901 then it could happen that
the closest that a 'float' could get to representing that is
4.5678904875 which by the usual rounding rules would round down to
4.567890 and your remove-trailing-zeroes approach would output that as
4.56789 which would not be correct!
--
I was very young in those days, but I was also rather dim.
-- Christopher Priest


Nov 15 '05 #4
A. L. wrote:
I guess the only way could be machine-dependent. In fact, what I want
is complete/precise conversion of float number to string, acting like
some sort of inverse function of atoi, atol, etc.

In Python, there is repr() that can well serve the above job.

You are right. It seems no way to rely on standard library or some
'standard' method in C.

[ much snipping ]

I have found after much testing that using the *printf functions, the
format string "%.8e" will represent 32-bit floats exactly. Also, the
format "%.16e" will represent 64-bit doubles exactly.

3.14159274e+00 is the float version and

3.1415926535897931e+00 is the double version.

The representations are as precise as the types allow. I have tested
this with gcc compiler on both Intel (little endian) and Sparc (big
endian) boxes.

I'll bet you money that either of these strings, if such they are, when
presented to any C implementation on any platform and evaluated by
strtod() will 'do the right thing' on that platform.

YMMV of course, but..
--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 15 '05 #5

"A. L." <ba*****@gmail.com> wrote

My question is that how to get the complete/precise string
representation of a float number using C standard library, but not in a
style of printf in which the output precision should be specified as a
great number (it will bring out superfluous trailing zeros).

As a general rule

printf("%.*g\n", DBL_DIG, x);

(Note the decimal point - we are not interested in the width but the
precison.) DBL_DIG, defined in limits.h, gives a platform-specific number of
digits precision. Note it is almost always 15 because almost everyone uses
the IEEE 64-bit format.

This should get rid of trailing zeroes, but if it doesn't and my Microsoft
docs are wrong simply call sprintf() and then chop them off by hnad.

Nov 15 '05 #6
Joe Wright <jw*****@comcast.net> writes:
A. L. wrote:
I guess the only way could be machine-dependent. In fact, what I want
is complete/precise conversion of float number to string, acting like
some sort of inverse function of atoi, atol, etc.
In Python, there is repr() that can well serve the above job.
You are right. It seems no way to rely on standard library or some
'standard' method in C.

[ much snipping ]

I have found after much testing that using the *printf functions, the
format string "%.8e" will represent 32-bit floats exactly. Also, the
format "%.16e" will represent 64-bit doubles exactly.

3.14159274e+00 is the float version and

3.1415926535897931e+00 is the double version.


To be precise, it won't represent the values *exactly*, but it will
(I presume) represent them with enough precision that you can get the
original value back.

--
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 15 '05 #7
Keith Thompson wrote:
Joe Wright <jw*****@comcast.net> writes:
A. L. wrote:
I guess the only way could be machine-dependent. In fact, what I want
is complete/precise conversion of float number to string, acting like
some sort of inverse function of atoi, atol, etc.
In Python, there is repr() that can well serve the above job.
You are right. It seems no way to rely on standard library or some
'standard' method in C.


[ much snipping ]

I have found after much testing that using the *printf functions, the
format string "%.8e" will represent 32-bit floats exactly. Also, the
format "%.16e" will represent 64-bit doubles exactly.

3.14159274e+00 is the float version and

3.1415926535897931e+00 is the double version.

To be precise, it won't represent the values *exactly*, but it will
(I presume) represent them with enough precision that you can get the
original value back.

That is *precisely* what I meant, *exactly*. :-)

Further, the precision of a floating point value is limited by the width
of its mantissa. A float on my system here has a 24-bit mantissa. That
means its precision about 1 in 16 million. Please note precision is not
about value range or computational accuracy. Representing a float value
beyond nine significant decimal digits is meaningless.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 15 '05 #8

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

Similar topics

5
by: Alexander Eisenhuth | last post by:
Hallo all there, maby I don't see the forest because of all that trees, but : from struct import * # how can I convert f = float(0.5)
13
by: Sebastian | last post by:
Hi, yesterday I discovered something that won't come as a surprise to more experienced C++ - programmers - but I was genuinely amazed. I had this line of code: float x = 0.9f; and when I had...
4
by: sunshinenewton | last post by:
Hi, I want to know is there any function in JS the same as "sprintf" in C language? Thanks a lot !
2
by: Shi Jin | last post by:
Hi there, I have been thinking this for quite a while: if we are considering the number of different representations of one type,say int and float, is there any difference as long as they are...
4
by: Dennis Myrén | last post by:
Hi. Is there a way to utilize the great primitive data type formatting routines available in .NET without working with strings? I want a byte directly rather than a string. I think it is...
10
by: 63q2o4i02 | last post by:
Hi, I'm using python to run some lab equipment using PyVisa. When I read a list of values from the equipment, one of the fields is 32 bits of flags, but the value is returned as a floating...
6
by: comp.lang.php | last post by:
I'm involved in a rather nasty debate involving a strange issue (whereby the exasperated tell me to RTFM even after my having done so), where this is insanely possible: print_r(is_int('1'));...
3
by: SharpCoderMP | last post by:
i've run into some trouble using data from xml inside my app. the scenario is simple. input data looks more or less like this: <item> <name>MyName</name> <somefloat>11.5</somefloat> </item> ...
15
by: khan | last post by:
Hi, I read that pointer representation can non-zero bit pattern, machine specific.Compiler when comes accross value '0' in pointer context, converts it to machine specific null pointer...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...

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.