473,406 Members | 2,343 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,406 software developers and data experts.

precision of a float

#include <iostream>
using namespace std;

void convert(const double& d)
{
cout << "before=" << d << " after=";
cout.precision(15);
cout << d << endl;
}

int main()
{
convert(0.1234567890123456789);
convert(1.06144e-305);
return 0;
}

hi,
i'm using the above code to demonstrate a problem we've got. the
first call to convert does what i expect, however the second call
doesnt -- it just looks like it didnt even bother. below is the
ouptput:

before=0.123457 after=0.123456789012346
before=1.06144e-305 after=1.06144e-305

can you tell me what i'm doing wrong? been using Sun's Forte6
compiler
thanks
ray

Oct 10 '07 #1
2 2647

<wh************@yahoo.co.ukwrote in message...
#include <iostream>
using namespace std;

void convert(const double& d){
cout << "before=" << d << " after=";
cout.precision(15);
cout << d << endl;
}

int main(){
convert(0.1234567890123456789);
convert(1.06144e-305);
return 0;
}

hi,
i'm using the above code to demonstrate a problem we've got. the
first call to convert does what i expect, however the second call
doesnt -- it just looks like it didnt even bother. below is the
ouptput:

before=0.123457 after=0.123456789012346
before=1.06144e-305 after=1.06144e-305

can you tell me what i'm doing wrong? been using Sun's Forte6
compiler
thanks
ray
What did you expect ( what you consider "wrong" )?
Perhaps you want to do ( inside "convert()" ):

cout.setf( std::ios_base::fixed );

..... or set some of the other flags.

--
Bob R
POVrookie
Oct 10 '07 #2
whatdoineed...@yahoo.co.uk wrote:
#include <iostream>
using namespace std;
void convert(const double& d)
{
cout << "before=" << d << " after=";
cout.precision(15);
}
int main()
{
convert(0.1234567890123456789);
convert(1.06144e-305);
return 0;
}
i'm using the above code to demonstrate a problem we've got. the
first call to convert does what i expect, however the second call
doesnt -- it just looks like it didnt even bother. below is the
ouptput:
before=0.123457 after=0.123456789012346
before=1.06144e-305 after=1.06144e-305
That's what you asked for:-). C++ formatting is defined in
terms of C's printf format specifiers: by default, floating
point values are formatted as "%g" (or more correctly, as
"%.<prec>g", where <precis the specified precision). And
according to the C standard, the "%g" format does not output
trailing 0's, regardless. If you output in scientific or fixed
format (corresponding to "%e" or "%f"), you should see them (but
I would not recommend fixed format for numbers like the second,
above). Otherwise (and you're going to love this), set the
showpoint formatting flag, e.g.:
std::cout.setf( std::ios::showpoint ) ;
The reason (for the name) is simple: C++ formatting is defined
in terms of C's printf, and the showpoint flag corresponds to
the '#' flag in printf: use alternative format. In the case of
"%#.0f", this means to output the point, whence the name. In
the case of "%#.<prec>g", it means to output the point AND any
trailing 0's. (Try outputting something like 12.0, and you'll
see the difference immediately: "12" without showpoint,
"12.0000" with, using the default precision of 6.)

--
James Kanze (GABI Software) mailto:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Oct 11 '07 #3

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

Similar topics

15
by: Ladvánszky Károly | last post by:
Entering 3.4 in Python yields 3.3999999999999999. I know it is due to the fact that 3.4 can not be precisely expressed by the powers of 2. Can the float handling rules of the underlying layers be...
2
by: Brian van den Broek | last post by:
Hi all, I guess it is more of a maths question than a programming one, but it involves use of the decimal module, so here goes: As a self-directed learning exercise I've been working on a...
5
by: Bryan R. Meyer | last post by:
I am a relatively new C++ programmer and am attempting to write a function that takes a number of type float and adds commas to it in the appropriate places. In order to manipulate the number to...
16
by: BigMan | last post by:
How can I check if assignment of a float to a double (or vice versa) will result in loss of precision?
15
by: michael.mcgarry | last post by:
Hi, I have a question about floating point precision in C. What is the minimum distinguishable difference between 2 floating point numbers? Does this differ for various computers? Is this...
4
by: Mahesh S | last post by:
Hi I am writing a sql query (added below) to calulate percentage. Its returning an integer value whereas I would like to get a float value (with 2 decimal precision). The query is: ...
6
by: R.Biloti | last post by:
Hi folks I wrote the naive program to show up the unit roundoff (machine precision) for single and double precision: #include <stdio.h> int main (void) { double x;
5
by: artifact.one | last post by:
Hello. Is there any reliable way to determine the number of digits of precision given by the float/double type? I read somewhere that the C99 standard guarantees six digits of precision with...
6
by: Enrique Cruiz | last post by:
Hi guys, I use LUTs to accelerate certain parts of my app. The LUTs contain float numbers, and I generated the numbers very precisely, up to 10 decimals. All seems fine, however, when I printf...
6
by: Matthew | last post by:
Hi, I want to change the precision level of floating point variables and calculations which is done in php.ini. However the server I rent for my domain does not give me access to php.ini,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
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...

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.