473,386 Members | 1,715 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.

double precision file streaming

What is the value for 'setprecision' that will cause doubles to be saved
'perfectly' (i.e., what goes in comes out with the EXACT same value)? Or,
what can I do to accomplish this if 'setprecision' is not the way to go?

Thanks in advance! : )

[==Peteroid==]
Nov 17 '05 #1
4 1005
Actually, through experimentation, I believe I've discovered the 'magic
number' is 18. Can someone confirm this? : )

[==Peteroid==]

"Peteroid" <pe************@msn.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
What is the value for 'setprecision' that will cause doubles to be saved
'perfectly' (i.e., what goes in comes out with the EXACT same value)? Or,
what can I do to accomplish this if 'setprecision' is not the way to go?

Thanks in advance! : )

[==Peteroid==]

Nov 17 '05 #2
Peteroid wrote:
Actually, through experimentation, I believe I've discovered the
'magic number' is 18. Can someone confirm this? : )


Actually, I don't believe there's any number that's guaranteed to work for
all cases. If there is such a number, it's 18 or possibly 19.

-cd
Nov 17 '05 #3
Carl Daniel [VC++ MVP] wrote:
Peteroid wrote:
Actually, through experimentation, I believe I've discovered the
'magic number' is 18. Can someone confirm this? : )

Actually, I don't believe there's any number that's guaranteed to work for
all cases. If there is such a number, it's 18 or possibly 19.

-cd

There is no such number. Save the binary data to be guaranteed identical
result. Base 10 fractional numbers can't be perfectly expressed in base
2 mantissas and we don't guarantee that the text representation can
round trip perfectly.

Ronald Laeremans
Visual C++ team
Nov 17 '05 #4
Peteroid wrote:
What is the value for 'setprecision' that will cause doubles to be saved
'perfectly' (i.e., what goes in comes out with the EXACT same value)? Or,
what can I do to accomplish this if 'setprecision' is not the way to go?


If the data is to be read on the same platform and executable that it
was written out with, you can use a simple binary write:

os.write(reinterpret_cast<char*>(&adouble), sizeof adouble);
and to read it:
is.read(reinterpret_cast<char*>(&adouble), sizeof adouble);

Remember to use std::ios_base::binary when opening the streams!

If you need the format to be portable in any way, you'll have to use a
portable binary format which will involve a bit more work to assemble
and disassemble your doubles to the stream.

Tom
Nov 17 '05 #5

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

Similar topics

5
by: DAVID SCHULMAN | last post by:
I've been trying to perform a calculation that has been running into an underflow (insufficient precision) problem in Microsoft Excel, which calculates using at most 15 significant digits. For this...
31
by: Bjørn Augestad | last post by:
Below is a program which converts a double to an integer in two different ways, giving me two different values for the int. The basic expression is 1.0 / (1.0 * 365.0) which should be 365, but one...
10
by: Bryan Parkoff | last post by:
The guideline says to use %f in printf() function using the keyword float and double. For example float a = 1.2345; double b = 5.166666667; printf("%.2f\n %f\n", a, b);
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;
67
by: lcw1964 | last post by:
This may be in the category of bush-league rudimentary, but I am quite perplexed on this and diligent Googling has not provided me with a clear straight answer--perhaps I don't know how to ask the...
60
by: Erick-> | last post by:
hi all... I've readed some lines about the difference between float and double data types... but, in the real world, which is the best? when should we use float or double?? thanks Erick
9
by: richard_lavoie | last post by:
Hi, I have something like this: vector<floatvec1; and I want to cast it, so I use vector vec2<double= static_cast< vector<double(vec1); I always become a error: syntax error before `>'...
29
by: Virtual_X | last post by:
As in IEEE754 double consist of sign bit 11 bits for exponent 52 bits for fraction i write this code to print double parts as it explained in ieee754 i want to know if the code contain any...
248
by: md | last post by:
Hi Does any body know, how to round a double value with a specific number of digits after the decimal points? A function like this: RoundMyDouble (double &value, short numberOfPrecisions) ...
0
by: Charles Coldwell | last post by:
James Kanze <james.kanze@gmail.comwrites: True, with some additional considerations. The commonly used IEEE 754 floating point formats are single precision: 32 bits including 1 sign bit, 23...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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.