473,795 Members | 3,358 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

loss of precision in doing file I/O

Hi all,

I have a doubt, I'll try to expose it to you as clearly as I can, maybe
it is not completely in topic, sorry about that.

I have a few vectors of doubles (output of some calculations) and I
check the norm and the dot product between couples of them before
storing them in a txt file, obtaining the expected results.

The vectors are orthonormal so the norm=1 and the dot product gives
something like 1.0e-18 that I suppose is quite close to zero...

When I read the vectors again from the txt file, I check the same things
and I find that the dot product is now around 1.0e-8.

Is this normal? Is there a way to avoid this loss of precision?

I am programming in c++ using visual studio 6 on win2000.

Thanks for your precious help.

--
there are no numbers in my email address
Jun 2 '06 #1
15 2938
"giff" writes:
I have a doubt, I'll try to expose it to you as clearly as I can, maybe it
is not completely in topic, sorry about that.

I have a few vectors of doubles (output of some calculations) and I check
the norm and the dot product between couples of them before storing them
in a txt file, obtaining the expected results.

The vectors are orthonormal so the norm=1 and the dot product gives
something like 1.0e-18 that I suppose is quite close to zero...

When I read the vectors again from the txt file, I check the same things
and I find that the dot product is now around 1.0e-8.

Is this normal? Is there a way to avoid this loss of precision?

I am programming in c++ using visual studio 6 on win2000.


If you read and write to the file using >> and << the data will be converted
to a character representation and the default precision will be used.
1.0e-8 sounds about like what I would expect. You could change the
precision [see ios::precision( ) ] or save the file in the binary form [see
read() and write()].
Jun 2 '06 #2
giff wrote:
Hi all,

I have a doubt, I'll try to expose it to you as clearly as I can, maybe
it is not completely in topic, sorry about that.

I have a few vectors of doubles (output of some calculations) and I
check the norm and the dot product between couples of them before
storing them in a txt file, obtaining the expected results.

The vectors are orthonormal so the norm=1 and the dot product gives
something like 1.0e-18 that I suppose is quite close to zero...

When I read the vectors again from the txt file, I check the same things
and I find that the dot product is now around 1.0e-8.

Is this normal? Is there a way to avoid this loss of precision?
See this FAQ and those following:

http://www.parashift.com/c++-faq-lit...html#faq-29.16

Can you show us a *complete* but *minimal* code snippet that we can cut
and paste into our editors to try it? It may be user error.
I am programming in c++ using visual studio 6 on win2000.


Irrelevant. (If it is relevant, you're in the wrong newsgroup.)

Cheers! --M

Jun 2 '06 #3
giff wrote:
I have a few vectors of doubles (output of some calculations) and I
check the norm and the dot product between couples of them before
storing them in a txt file, obtaining the expected results.

The vectors are orthonormal so the norm=1 and the dot product gives
something like 1.0e-18 that I suppose is quite close to zero...

When I read the vectors again from the txt file, I check the same
things and I find that the dot product is now around 1.0e-8.

Is this normal? Is there a way to avoid this loss of precision?
Write more digits to the output.
I am programming in c++ using visual studio 6 on win2000.


That shoulnd't matter.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 2 '06 #4
osmium ha scritto:

If you read and write to the file using >> and << the data will be converted
to a character representation and the default precision will be used.
that's probably what happens
1.0e-8 sounds about like what I would expect. You could change the
precision [see ios::precision( ) ] or save the file in the binary form [see
read() and write()].


thanks for the hint
--
there are no numbers in my email address
Jun 2 '06 #5
mlimber ha scritto:
Is this normal? Is there a way to avoid this loss of precision?
See this FAQ and those following:

http://www.parashift.com/c++-faq-lit...html#faq-29.16


thanks

Can you show us a *complete* but *minimal* code snippet that we can cut
and paste into our editors to try it? It may be user error.


std::ofstream os("filename.tx t" , ios_base::trunc |ios_base::out );

for(int n=0; n<nP; n++){

for(int i=0; i<nV; i++){

os << ((double*)(shap eV2d[n]->data.ptr))[i * 2] << " "
<< ((double*)(shap eV2d[n]->data.ptr))[i*2+1] << endl;

}
}

I write nP vectors stored as CvMat arrays (I am using the opencv libraries)

here is the reading routine:

std::ifstream is( filename.txt );

for(int n=0; n<nP; n++ ) {

for(int v=0; v<nV; v++){

is >> x >> y;
((double*)(shap eV2d[n]->data.ptr))[2 * v] = x;
((double*)(shap eV2d[n]->data.ptr))[2*v+1] = y;

}
}
Anyway I don't think this could be a huge problem for my project, I mean
1.0e-8 is still quite small and comparable to zero...

thanks

--
there are no numbers in my email address
Jun 2 '06 #6
Victor Bazarov ha scritto:

Write more digits to the output.


I am using <<, do you mean I should use a printf instead?
--
there are no numbers in my email address
Jun 2 '06 #7
giff <gi*********@44 4gmail.com> wrote:
I have a few vectors of doubles (output of some calculations) and I
check the norm and the dot product between couples of them before
storing them in a txt file, obtaining the expected results.

The vectors are orthonormal so the norm=1 and the dot product gives
something like 1.0e-18 that I suppose is quite close to zero...

When I read the vectors again from the txt file, I check the same things
and I find that the dot product is now around 1.0e-8.
I ran into something similar before where I would take a dot product of
a couple different (mathematical) vectors and then normalize them. When
the dot product was 1e-18 or whatever, the algorithm normalized this
which obviously threw all my calculations out the window. I had to
explicitly set the result to 0 in order for things to work correctly.

For example,

const double Epsilon = 1e-10; // or whatever is appropriate

// ...

double result = /* compute dot product */;

// need #include <cmath> for std::abs
if (std::abs(resul t) < Epsilon) {
result = 0;
}
Is this normal? Is there a way to avoid this loss of precision?


The above are symptoms of the limitations of floating-point type numbers
in general. You can search the FAQ for more info. In particular, the
FAQ has a link to David Goldberg's paper "What Every Computer-Scientist
Should Know About Floating Point Arithmetic".

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Jun 2 '06 #8
giff wrote:
Victor Bazarov ha scritto:

Write more digits to the output.


I am using <<, do you mean I should use a printf instead?


No, I don't mean that. RTFM about stream manipulators and
specifically about 'setprecision'.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 2 '06 #9
Victor Bazarov wrote:
giff wrote:
I have a few vectors of doubles (output of some calculations) and I
check the norm and the dot product between couples of them before
storing them in a txt file, obtaining the expected results.

The vectors are orthonormal so the norm=1 and the dot product gives
something like 1.0e-18 that I suppose is quite close to zero...

When I read the vectors again from the txt file, I check the same
things and I find that the dot product is now around 1.0e-8.

Is this normal? Is there a way to avoid this loss of precision?


Write more digits to the output.
I am programming in c++ using visual studio 6 on win2000.


That shoulnd't matter.


Aah, but it does. There have been recent discussions on the boost mailing
list concerning roundtripping doubles/floats to/from text files with many VC
versions losing 1-2 bits precision for certain numeric values. These have
been posted on MS website as bugs, but have apparently been refuted by MS as
a design feature.

Jeff Flinn
Jun 2 '06 #10

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

Similar topics

4
7852
by: Roger Leigh | last post by:
Hello, I'm writing a fixed-precision floating point class, based on the ideas in the example fixed_pt class in the "Practical C++ Programming" book by Steve Oualline (O' Reilly). This uses a long int to store the value, and the precision (number of decimal points) is variable (it's a templated class): template <size_t _decimal_places = 4> class FixedFloat {
16
6267
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?
11
4646
by: Marcin | last post by:
Hello! How I can make SQRT(2) with 20 digits precision (after float point)?
4
1195
by: hunt n peck | last post by:
does anyone know why this expression: str(csng(18333.332)) equals: "18333.33" ? why am i losing precision from this operation?
8
10422
by: Grant Edwards | last post by:
I'm pretty sure the answer is "no", but before I give up on the idea, I thought I'd ask... Is there any way to do single-precision floating point calculations in Python? I know the various array modules generally support arrays of single-precision floats. I suppose I could turn all my variables into single-element arrays, but that would be way ugly...
1
7368
by: jx2 | last post by:
1: where do i lose precision? everythink is long i dont multiply so its not posible to get enything different then long 2: how to fix it? long n,i; long numbers = new long; n=1; i = 1; numbers = i;
1
17064
by: dissectcode | last post by:
Hello - I am looking at a scanf function that takes in a 16 bit unsigned int(UINT16), but its format specifier is %hd. According to http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=/com.ibm.xlcpp8a.doc/standlib/ref/printconversionspecifiers.htm that specifier is for a short int, so won't I lose precision here? Also - %hd is for base 8. I am confused about bases. Is UInt16 base 16 or how does that work?? ...
0
9519
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,...
0
10435
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10213
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10000
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
9037
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7538
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6779
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
5436
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...
2
3721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.