[R] Determinant

David Winsemius dwinsemius at comcast.net
Thu May 14 21:50:26 CEST 2015

On May 14, 2015, at 1:44 AM, chasiotisv at math.auth.gr wrote:

> Hello,
> I am Vasilis Chasiotis and I am a candidate Ph.D. student in Aristotle University of Thessaloniki in Greece.
> I have the following problem.
> I want to check if the square of a number ( for example the square of 1.677722e+29 ) is an integer.

The square of 1.677722e+29 is almost certainly an integer since the power of 10  (+29)  exceeds the number of digits. That implies that the number in non-scientific notation has many 0's on the righthand side.

I'm guessing that you may be asking whether the square-root is an integer.

> The problem is that this number is the calculation of the determinant (so the number should be an integer) of a matrix 22x22, which means it has an approximation ( the "real" number is 1.6777216e+29 but R gives to me  1.6777215999999849e+29 ), because R use LU-decomposition to calculate the determinant.
> That means that the radical of the number 1.6777215999999849e+29 is not an integer, but it should be.

Again guessing that by 'radical' you mean the square-root. I am also confused about  what test you were applying to that result to determine that it was not an integer. 

At any rate, I'm guessing that the limitation of R's numerical accuracy may get in the way of determining whether the square-root is an integer. If it were integer then it should equal floor(n) :

 (1.6777215999999849e+29)^(1/2) - floor((1.6777215999999849e+29)^(1/2)) 
#[1] 0.125

 (1.6777216e+29)^(1/2) - floor( (1.6777216e+29)^(1/2) )
#[1] 0

That's because that number was the product of two perfect squares:

> 16777216^(1/2) - floor( 1.6777216^(1/2) )
[1] 4095

And 10^22 = 10^11*10^11

If you know how big the  original was you could round it to the correct precision but that seems too much to hope for.

 print( round(1.6777215999999849e+29, digits=10) , digits=10)
#[1] 1.6777216e+29

identical( (1.6777216e+29)^(1/2) , floor( (1.6777216e+29)^(1/2) ) )
#[1] TRUE

> How can we overcome this problem? 

There are a couple of packages that support exact math on really large numbers. You need to clarify what is being requested. You definitely need to review R-FAQ 7.31 and make sure you understand it and also review ?double and ?integer


David Winsemius
Alameda, CA, USA

More information about the R-help mailing list