How to store variable's "exact" value?

Hey,

I hope I'm able to explain my programming issue clearly:

I have written a solver for differential equations.
Now I solved it, with different initial conditions, which are calculated within several loops; for instance:

1
2
3
4
5
for(double value_a = a1; a <= a2; a+=2.2E-20*a_steps){
    for(double value_b = b1; b <= b2; b+=2.2E-20*b_steps){
      function_to_solve_my_differential_equation(value_a,value_b,...);
    }
}


Then I store the data of the solved differential equation; with the remark which initial conditions I have used to solve it:


Solution with initial conditions:
a_value = 2.343234443243E-16 (e.g.)
 b_value = 2.876344242354E-13 (e.g.)
... huge amount of data ...
... huge amount of data ...
...........................



Suppose, I want to reproduce my data again: I solve my differential equation again with the initial conditions, which I had printed out in the data-file I produced before:

 
function_to_solve_my_differential_equation(2.343234443243E-16, 2.876344242354E-13,...)


But, surprise, then the "huge amount of data" I get is not _exactly_ the same as before!

I very, very strongly assume, that the problem is, that the printed initial conditions (a_value, b_value) are not "exactly" the "internal", "exact" ones, with which my differential equation is solved, although I printed them out with "precision(16)", cause double should be exact to - more or less - 16 decimals...
I think that's why after many loops, the numerical inacurracy of conversion between decimal to "internal" binary representation accumulates.

I hope I explained my problem clearly: I want to output the initial conditions produced by the two for-loops "exactly", so that i can call my differential-equation-solver again, to reproduce the data I got via going through the initial conditions within the loops.

I tried to store the initial values produced by the loops as binary data, but that is very, very unpracticable for my needs.





Last edited on
in visual studio long double is the same ad double.
also precision is not always the same.

not shore what what IDE are you using.
1
2
3
4
5
6
7
8
double x = 123.45;
//file opened in binary mode
file.write((const char*)&x, sizeof(double)); // people will flame me for the c-style cast. But just to show what I mean...
/*
...
*/
double y;
file.read((char*)&y, sizeof(double))// now y should be guaranteed to be exactly equal to x 
I'm using Eclipse under Ubuntu 10.04.
But I think my problem is IDE and platform-independent.

I also tried to output my initial conditions (a_value, b_value...) with a much higher precision, but then nothing changed, as expected, cause the additional decimals just contain rubbish.
> How to store variable's "exact" value?

You can not store "exact" values in a double - floating point is not an "exact" representation of real numbers.
http://en.wikipedia.org/wiki/Floating_point#Representable_numbers.2C_conversion_and_rounding.

For example addition is not commutative.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

int main()
{
    double a = 1.0e+20 ;
    double b = 1.0e-20 ;
    double c = - a ;
    std::cout << "a+b+c == " << a+b+c << '\n'
              << "a+c+b == " << a+c+b << '\n' ;

    double d = 1234567890.9876543210 ;
    double e = 1234567890.98765432219 ;
    std::cout << std::boolalpha << "d == e ? " << (d==e) << '\n' ;
}


Output on my implementation:
a+b+c == 0
a+c+b == 1e-020
d == e ? true


For precision that is limited only by available memory, use an arbitrary-precision arithmetic library; for example GMP http://en.wikipedia.org/wiki/GNU_Multi-Precision_Library
Last edited on
@JLBorges: Thx for your reply!! I'm aware of that, that the very exact cannot be stored in doubles. That's why I wrote "exact" in quotes. I just wanted to know, how to print out the values, with which a function is called, when the values themselves were calculated via several steps. Hereby, the problem is, that the values printed out (by e.g. cout << ... , write(...),...) are not the ones with which my program operates, because of "inaccurate" conversion between "internal" binary data and printed decimal data.
Thx for the link to the multi-precision library. I will consider this in my next project!!

@Caligulaminus: Thx too!! I was afraid, that this the only possibility. I have implemented that, and it works fine for one variable. I have to extend it, so that in one data file I can write and read tab-separated binary data, which seems to be not so trivial as with non-binary data.
Topic archived. No new replies allowed.