473,657 Members | 2,392 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.12345 67890123456789) ;
convert(1.06144 e-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.1234567 89012346
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 2659

<wh************ @yahoo.co.ukwro te 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.12345 67890123456789) ;
convert(1.06144 e-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.1234567 89012346
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.12345 67890123456789) ;
convert(1.06144 e-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.1234567 89012346
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::showp oint ) ;
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 objektorientier ter Datenverarbeitu ng
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
5948
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 set from Python so that 3.4 yield 3.4? Thanks, Károly
2
2798
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 script to convert numbers to arbitrary bases. It aims to take any of whole numbers (python ints, longs, or Decimals), rational numbers (n / m n, m whole) and floating points (the best I can do for reals), and convert them to any base between 2 and...
5
7990
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 add the commas, I convert it to a string (specifically a char rather than an actual string object) using the gcvt function as shown below. char amt; gcvt(amount,50,amt);
16
6227
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
3922
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 the EPSILON? I know in float.h a FLT_EPSILON is defined to be 10^-5. Does this mean that the computer cannot distinguish between 2 numbers that differ by less than this epsilon?
4
28741
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: (SELECT (COUNT(*) * 100) / (SELECT COUNT(*) FROM HEALTHCAREDB.PATIENT_DIABETIC_TYPE)
6
5710
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
2392
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 the float type, although I can't seem to find this information now. A cursory search through the C99 pdf doesn't bring
6
2482
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 the numbers in the LUT, I do not get the same exact numbers. The first 6 decimals are ok, but it got beserk after that. To illustrate further: float myLUT = ;
6
17069
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, they say 'for security reasons'. Can the precision level be changed by PHP code as needed?
0
8411
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8323
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,...
1
8513
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
8613
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
5638
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4173
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2740
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
1732
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.