[R] float and double precision with C code

2008-07-17 Thread JS Ubei
Hi all,

There is a mistake for wich I need your ligths :

///

I have a small C code :

SEXP testData()
{
SEXP result;
void * rans;
float * my_data; 
int my_data_length;

my_data_length = 2;
my_data = new float[my_data_length];

my_data[0] = 29.958334;
my_data[1] = 29.875;

PROTECT(result = allocVector(REALSXP, my_data_length));
rans = (void *)REAL(result);

for(int i=0; i  my_data_length; i++) ((double *)rans)[i] = 
(double)(my_data[i]);

Rprintf(C value #1 : %f\n, ((double *)rans)[0]);
Rprintf(C value #2 : %f\n, ((double *)rans)[1]);

delete(my_data);

UNPROTECT(1);
return(result);
}
///

And the R corresponding function :

testData - function()
{
result - .Call(testData, PACKAGE=my_package)
print(paste(R value #1 :, result[1]))
print(paste(R value #2 :, result[2]))
return(result)
}

///
And in R console, after compilation :

 my_result - testData()
C value #1 : 29.958334
C value #2 : 29.875000
[1] R value #1 : 29.9583339691162
[1] R value #2 : 29.875

 my_result[1] == 29.958334
[1] FALSE

???

How can I do to conserve my values ?

Regards,



  ___
e http://mail.yahoo.fr

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] Re : float and double precision with C code

2008-07-17 Thread JS Ubei
thank you for your quick answer,

I'm far of the digits capacity and my values are not the result of a 
computation. I'm developping a R package to acces a specific data source. And I 
need precision a few better. How can I do ? 

When I try this In R console, this is correct and  what I need :

 my_value - 29.958334
 my_value == 29.958334
[1] TRUE


But I need to do the first operation (my_value - 29.958334) in C


Regards,

- Message d'origine 
De : jim holtman [EMAIL PROTECTED]
À : JS Ubei [EMAIL PROTECTED]
Cc : r-help@r-project.org
Envoyé le : Jeudi, 17 Juillet 2008, 14h56mn 01s
Objet : Re: [R] float and double precision with C code

FAQ 7.31

Also read

What Every Computer Scientist Should Know About Floating-Point
Arithmetic, ACM Computing Surveys, 23/1, 5–48, also available via
http://docs.sun.com/source/806-3568/ncg_goldberg.html.


On Thu, Jul 17, 2008 at 8:47 AM, JS Ubei [EMAIL PROTECTED] wrote:
 Hi all,

 There is a mistake for wich I need your ligths :

 ///

 I have a small C code :

 SEXP testData()
 {
SEXP result;
void * rans;
float * my_data;
int my_data_length;

my_data_length = 2;
my_data = new float[my_data_length];

my_data[0] = 29.958334;
my_data[1] = 29.875;

PROTECT(result = allocVector(REALSXP, my_data_length));
rans = (void *)REAL(result);

for(int i=0; i  my_data_length; i++) ((double *)rans)[i] = 
 (double)(my_data[i]);

 Rprintf(C value #1 : %f\n, ((double *)rans)[0]);
 Rprintf(C value #2 : %f\n, ((double *)rans)[1]);

delete(my_data);

UNPROTECT(1);
return(result);
 }
 ///

 And the R corresponding function :

 testData - function()
 {
result - .Call(testData, PACKAGE=my_package)
 print(paste(R value #1 :, result[1]))
 print(paste(R value #2 :, result[2]))
return(result)
 }

 ///
 And in R console, after compilation :

 my_result - testData()
 C value #1 : 29.958334
 C value #2 : 29.875000
 [1] R value #1 : 29.9583339691162
 [1] R value #2 : 29.875

 my_result[1] == 29.958334
 [1] FALSE

 ???

 How can I do to conserve my values ?

 Regards,



  ___
 e http://mail.yahoo.fr

 __
 R-help@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.




-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?



  
_ 
Envo
__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] Re : Re : float and double precision with C code

2008-07-17 Thread JS Ubei
ok, sorry, my mistake was the C printf.
Thank you for your good answer

Regards



- Message d'origine 
De : JS Ubei [EMAIL PROTECTED]
À : jim holtman [EMAIL PROTECTED]
Cc : r-help@r-project.org
Envoyé le : Jeudi, 17 Juillet 2008, 15h25mn 07s
Objet : [R] Re :  float and double precision with C code

thank you for your quick answer,

I'm far of the digits capacity and my values are not the result of a 
computation. I'm developping a R package to acces a specific data source. And I 
need precision a few better. How can I do ? 

When I try this In R console, this is correct and  what I need :

 my_value - 29.958334
 my_value == 29.958334
[1] TRUE


But I need to do the first operation (my_value - 29.958334) in C


Regards,

- Message d'origine 
De : jim holtman [EMAIL PROTECTED]
À : JS Ubei [EMAIL PROTECTED]
Cc : r-help@r-project.org
Envoyé le : Jeudi, 17 Juillet 2008, 14h56mn 01s
Objet : Re: [R] float and double precision with C code

FAQ 7.31

Also read

What Every Computer Scientist Should Know About Floating-Point
Arithmetic, ACM Computing Surveys, 23/1, 5–48, also available via
http://docs.sun.com/source/806-3568/ncg_goldberg.html.


On Thu, Jul 17, 2008 at 8:47 AM, JS Ubei [EMAIL PROTECTED] wrote:
 Hi all,

 There is a mistake for wich I need your ligths :

 ///

 I have a small C code :

 SEXP testData()
 {
SEXP result;
void * rans;
float * my_data;
int my_data_length;

my_data_length = 2;
my_data = new float[my_data_length];

my_data[0] = 29.958334;
my_data[1] = 29.875;

PROTECT(result = allocVector(REALSXP, my_data_length));
rans = (void *)REAL(result);

for(int i=0; i  my_data_length; i++) ((double *)rans)[i] = 
 (double)(my_data[i]);

 Rprintf(C value #1 : %f\n, ((double *)rans)[0]);
 Rprintf(C value #2 : %f\n, ((double *)rans)[1]);

delete(my_data);

UNPROTECT(1);
return(result);
 }
 ///

 And the R corresponding function :

 testData - function()
 {
result - .Call(testData, PACKAGE=my_package)
 print(paste(R value #1 :, result[1]))
 print(paste(R value #2 :, result[2]))
return(result)
 }

 ///
 And in R console, after compilation :

 my_result - testData()
 C value #1 : 29.958334
 C value #2 : 29.875000
 [1] R value #1 : 29.9583339691162
 [1] R value #2 : 29.875

 my_result[1] == 29.958334
 [1] FALSE

 ???

 How can I do to conserve my values ?

 Regards,



  ___
 e http://mail.yahoo.fr

 __
 R-help@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.




-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?



  
_ 
Envo
__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



  _
ne boite mail plus intelligente http://mail.yahoo.fr

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.