0

I am asked to create a function to check if it forms a right angled triangle. My code only works if my input are integers, but once I start putting in non-integer values, it fails, eg, 1,1 and sqrt(2). How do I fix this? Following is my code.

right.angled.triangles=function(a,b,c){
x=sort(c(a,b,c))
if(any(x<0)){
"Invalid lengths"
}else {
if(x[1]^2+x[2]^2==x[3]^2){
"Right angled triangle"
}else{
"Not a right angled triangle"
}
}
}
  • Generally testing for floating point numbers being equal is problematic - all numbers are slightly rounded in memory. There's a detailed discussion [here](https://stackoverflow.com/questions/9508518/why-are-these-numbers-not-equal) with some solutions – Miff Mar 11 '21 at 10:26
  • @Miff Thank you for your reply. I just found the solution. –  Mar 11 '21 at 10:29

0 Answers0