473,397 Members | 1,961 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,397 software developers and data experts.

Extra Digits of Precision

emaghero
Does anyone know how to ouput extra digits of precision in C++?

I am using Microsft Visual Studio to write my C++ code and I want to get at least 20 - 30 digits of precision on screen and for outputting to a txt file.

At the moment I use printf("%16.24lf\n", number);

I would like to know if there is a C++ equivalent?

emaghero.
Oct 23 '06 #1
5 2866
Well there is member function with the cout object called a precision which actually set or gets the precision. The following shows u on how to use it

Expand|Select|Wrap|Line Numbers
  1. cout.precision(5); // Sets the precsion to 5
  2.  
some sample code using the precision

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <cstdlib>
  3.  
  4. using namespace std;
  5.  
  6. int _tmain(int argc, _TCHAR* argv[])
  7. {
  8.     double value = 1.23652458759653;
  9.  
  10.     cout.precision(20);
  11.     cout<<"The double printed with 5 precision -> "<<value<<endl;
  12.  
  13.     cin.get();
  14.     return 0;
  15. }
  16.  
  17. /*my output
  18. The double printed with 5 precision -> 1.2365245875965301
  19. */
  20.  
ssharish
Oct 23 '06 #2
That looks like it will do the business.

Thanks very much

emaghero
Oct 23 '06 #3
vninja
40
if you want to be lazier you could just use
setprecition(N)
before the value eg

cout << "the value " << setprecition(N) << value;

use N as a global const or as a var to make it easier on yourself
Oct 24 '06 #4
if you want to be lazier you could just use
setprecition(N)
before the value eg

cout << "the value " << setprecition(N) << value;

use N as a global const or as a var to make it easier on yourself
Thanks very much. I think I like that one better.
Oct 25 '06 #5
Banfa
9,065 Expert Mod 8TB
I am using Microsft Visual Studio to write my C++ code and I want to get at least 20 - 30 digits of precision on screen and for outputting to a txt file.
There is not much point specifying 20-30 bit precision when the double type (which is the most precise type that MSVC++ supports) only holds 15 bits of precision. You will just end up with 5 - 15 0s at the end of your number.
Oct 25 '06 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Peter Blatt | last post by:
Does 5 represent the total numer of digits (including the fractional portion) or only the number of places BEFORE the decimal point? Moreover does the number include the decimal point? Are there...
6
by: Peter Blatt | last post by:
Does 5 represent the total numer of digits (including the fractional portion) or only the number of places BEFORE the decimal point? Moreover does the number include the decimal point? Are there...
1
by: Shreyas Kulkarni | last post by:
hi there, recently i have got a problem regarding calculation of sum of digits in a floating point or precision number. the weird behaviour of compiler/language is preventing me from calculating...
6
by: Peter Blatt | last post by:
Does 5 represent the total numer of digits (including the fractional portion) or only the number of places BEFORE the decimal point? Moreover does the number include the decimal point? Are there...
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?
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
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
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
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...
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
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.