-1

Using the GSL (GNU Scientific Library), I'm trying to understand why gsl_vector_view_array() returns a slighly modified value after assignment.

In the code below, I declare a vector_view 'qview_test' which is linked to table q_test[0]=0.0 and display its value which is 0.0. Then, I change the value of q_test[0]=1.12348 and expecting the same value for qview_test, but it gets alterated to qview_test=1.1234800000000000341771055900608189.

How do you explain such a result ? How to replicate the result without GSL ?

#include <iostream>
#include <gsl/gsl_blas.h>
using namespace std;

double q_test[1]={0.0};
gsl_vector_view qview_test;

int nb_variable = 1;

int main()
{

    qview_test=gsl_vector_view_array(q_test,nb_variable);

    cout.precision(35);
    cout << "qview before: " << gsl_vector_get(&qview_test.vector,0)<< endl;

    // Assign value
    q_test[0]=1.12348;

    cout << "qview after: " << gsl_vector_get(&qview_test.vector,0) << endl;

    return 0;
}

Thanks for any help,

H.Nam

H.Nam
  • 19
  • 4

1 Answers1

-1

This looks like floating point rounding to me. Basically any decimal number can only have a finite precision and all numbers in between get rounded to the nearest float.

I am not familiar with gsl so I don't know why it's displaying so many digits.

In other words, to get more precise give your number more bits (128 bit float or something like that) to be represented. This will give you more precision, however you most likely won't need it.

GRASBOCK
  • 631
  • 6
  • 16