0
library(Matrix)
A <- matrix(c(3, 0, 0, 5, 1, -1, -2, 2, 4), ncol = 3, nrow = 3, byrow = T)
d <- as.integer(det(A))
p <- as.integer(prod(eigen(A)$values))
d == p
[1] FALSE

When I run the class function they both are 'numeric' and both equal 18. Yet R says that they are not equal. What's going on?

Travasaurus
  • 601
  • 1
  • 8
  • 26
  • 1
    Compare `sprintf("%0.54f", det(A)); sprintf("%0.54f", prod(eigen(A)$values))` (the hex representation of the floating point numbers). To the computer, they are not the same. It's one of the things you need to be aware of when using a computer program that uses floating point numbers (as most do). – MrFlick Aug 24 '18 at 21:53
  • 2
    Perhaps another minor source of confusion, `as.integer()` truncates, it does not round. (When I run this, `d` is printed as 17.) – joran Aug 24 '18 at 21:54
  • interesting It responded with: [1] "0x1.1ffffffffffffp+4" [1] "0x1.2p+4" Why does 'as.integer' not fix this? – Travasaurus Aug 24 '18 at 21:55

0 Answers0