Why is this handmade square root function in Swift not returning the value, but throws the error?

Advertisements Just trying to create an analog of sqrt() in Swift, but it throws .noRoot in all the matched cases. Also I add the error .outOfBound, this is working correctly. import UIKit enum WrongNumber: Error { case outOfBounds case noRoot } func mySqrt(_ number: Int) throws -> Int { if number < 1 || number… Read More Why is this handmade square root function in Swift not returning the value, but throws the error?

Why is abs(x)+abs(y) different from sqrt(x*x + y*y)

Advertisements I was making a Physics Engine in C++ when I noticed that when I replace: float absDistance = sqrt(vectorDistance.x * vectorDistance.x + vectorDistance.y * vectorDistance.y); With: float absDistance = abs(vectorDistance.x) + abs(vectorDistance.y) Different things happen. I’m using the library <cmath> I looked at a lot of stack exchanges but I couldn’t find anyone saying… Read More Why is abs(x)+abs(y) different from sqrt(x*x + y*y)

S3 template which can take different formulas and numeric vectors as arguments

Advertisements Please help me to make my code work. Here I’m trying to create an S3 template which can take different formulas and numeric vectors as arguments. Also I want to add a plot method, drawing the resulting figure. For now it throws an error: Error in do.call(f, list(x, y)) : ‘what’ must be a… Read More S3 template which can take different formulas and numeric vectors as arguments

Introduce two strings in a "for" loop

Advertisements I have this dataframe: a <- c(2,5,90,77,56,65,85,75,12,24,52,32) b <- c(45,78,98,55,63,12,23,38,75,68,99,73) c <- c(77,85,3,22,4,69,86,39,78,36,96,11) d <- c(52,68,4,25,79,120,97,20,7,19,37,67) e <- c(14,73,91,87,94,38,1,685,47,102,666,74) df <- data.frame(a,b,c,d,e) and this script: R <- Map(`+`, list(1:3), 0:9) cmin <- t(as.matrix(rep(NA, ncol(df)))) for (r in seq(R)) { for (f in seq(ncol(df))) { x <- df[R[[r]], f] y <- df[R[[r]], -f] dif_2 <-… Read More Introduce two strings in a "for" loop