エラーメッセージ
C++ の関数内で、例外を発生させるために stop() 関数を呼び出すことができます。関数 add_positive_numbers() を一部用意しました。例外を発生させるように、この関数を完成させてください。
この演習はコースの一部です
Rcpp で R コードを最適化する
演習の手順
xが負の場合、メッセージ "x is negative" で例外を発生させてください。yが負の場合、メッセージ "y is negative" で例外を発生させてください。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
cppFunction('
// adds x and y, but only if they are positive
int add_positive_numbers(int x, int y) {
// if x is negative, stop
if(___) stop("x is negative") ;
// if y is negative, stop
if(___) ___("y is negative") ;
return x + y ;
}
')
# Call the function with positive numbers
add_positive_numbers(2, 3)
# Call the function with a negative number
add_positive_numbers(-2, 3)