[R] convert 32bit numbers to 64bit (or float to double)

Sebastian Gibb lists at sebastiangibb.de
Wed May 5 16:06:59 CEST 2010


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 an open source application to read the files. It imports the 
values as 64bit (double) 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;

myFloat: 1234.56
myDouble: 1234.56005859375

You could see, the fifth position after decimal point becomes a pseudo value.
Now I know how to calculate these values for my own in R.
If I compare the old (C++) values with my new (R) values the differ at 
position fifth after decimal point and all following.
For compatibility reasons I want to get the same values for R like the old C++ 
How can I simulate sth. like that?

Kind regards,


More information about the R-help mailing list