S
Sebastian Gibb
Hello,
a long time ago I had to use a foreign C application to generate some
numbers. This application saves the numbers as 32bit (float) values in a
file.
I had to use R (www.r-project.org) to read the files. It imports the
values as 64bit (double, R only knows doubles) and generates some pseudo
numbers at position after decimal point.
I want to show you an example C++ code, which does nearly the same:
#include <iostream>
#include <iomanip>
#include <limits>
using namespace std;
int main() {
float myFloat = 1234.56;
double myDouble = myFloat;
cout << setprecision(numeric_limits<float>::digits10) << "myFloat: " <<
myFloat << endl;
cout << setprecision(numeric_limits<double>::digits10) << "myDouble: " <<
myDouble << endl;
return 0;
}
output:
myFloat: 1234.56
myDouble: 1234.56005859375
You could see, the fifth position after decimal point becomes a pseudo
value.
Now I am able to replace the foreign C application by an own R application.
(The algorithm uses double values, too.)
For compatibility reasons I want to get the same values for R like the old C
ones. Until now I use an C-binding for R to do the following:
double precision32(double value) {
float x = value;
return (double)x;
}
I want to know what happens when I call "double myDouble=myFloat" and how
can I simulate sth. like that with only using double values?
Kind regards,
Sebastian
a long time ago I had to use a foreign C application to generate some
numbers. This application saves the numbers as 32bit (float) values in a
file.
I had to use R (www.r-project.org) to read the files. It imports the
values as 64bit (double, R only knows doubles) and generates some pseudo
numbers at position after decimal point.
I want to show you an example C++ code, which does nearly the same:
#include <iostream>
#include <iomanip>
#include <limits>
using namespace std;
int main() {
float myFloat = 1234.56;
double myDouble = myFloat;
cout << setprecision(numeric_limits<float>::digits10) << "myFloat: " <<
myFloat << endl;
cout << setprecision(numeric_limits<double>::digits10) << "myDouble: " <<
myDouble << endl;
return 0;
}
output:
myFloat: 1234.56
myDouble: 1234.56005859375
You could see, the fifth position after decimal point becomes a pseudo
value.
Now I am able to replace the foreign C application by an own R application.
(The algorithm uses double values, too.)
For compatibility reasons I want to get the same values for R like the old C
ones. Until now I use an C-binding for R to do the following:
double precision32(double value) {
float x = value;
return (double)x;
}
I want to know what happens when I call "double myDouble=myFloat" and how
can I simulate sth. like that with only using double values?
Kind regards,
Sebastian