You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Virtual TMB运行浮点异常调试代码遇未使用变量警告求助

Fixing "Unused Variable" Warnings in TMB Floating-Point Debugging Example

Those unused variable warnings you're seeing are totally harmless—they're just the compiler pointing out that you declared lambda (a data scalar) and x (a parameter) in your code, but never actually used them anywhere in the objective function. Since this is a minimal debugging example focused on triggering floating-point exceptions, those variables are just leftover from template code, but we can eliminate the warnings easily with a few approaches:

Option 1: "Use" the variables (most clean for example code)

Even a trivial, no-op usage will satisfy the compiler. Just add a line that references both variables somewhere in your objective function—this doesn't change the behavior of your floating-point test, but tells the compiler they're intentional:

Modified C++ code:

// Illustrates how to make the debugger catch a floating point error.
#include <TMB.hpp>
#include <fenv.h> // Extra line needed

template<class Type>
Type objective_function<Type>::operator() () {
  feenableexcept(FE_INVALID | FE_OVERFLOW | FE_DIVBYZERO | FE_UNDERFLOW); // Extra line needed
  DATA_SCALAR(lambda);
  PARAMETER(x);
  
  // Trivial usage to eliminate unused warnings
  lambda += Type(0);
  x += Type(0);
  
  Type f;
  f = sqrt(-1.); // FE_INVALID ( sqrt(-1.) returns NaN )
  //f = 1./0.; // FE_DIVBYZERO ( division by zero )
  //f = exp(100000.); // FE_OVERFLOW ( exp(100000.) returns Inf ) [Does not work on all platforms]
  //f = exp(-100000.); // FE_UNDERFLOW ( exp(-100000.) returns 0 )
  return f;
}

Option 2: Disable the warning during compilation

If you don't want to modify the C++ code, you can add a compiler flag to suppress unused variable warnings directly in your R compile() call:

Modified R code:

data <- list(lambda = 25)
parameters <- list(x=1)
require(TMB)
compile('nan.cpp','-fno-gnu-unique -O0 -Wall -Wno-unused-variable')
dyn.load(dynlib('nan'))
model <- MakeADFun(data, parameters)
fit <- nlminb(model$par, model$fn, model$gr)
rep <- sdreport(model)
print(rep)

The -Wno-unused-variable flag tells GCC to skip reporting this specific warning type.

Option 3: Remove unused declarations (simplest for debugging)

Since your goal is just to test floating-point exception handling, you can completely remove the unused DATA_SCALAR(lambda) and PARAMETER(x) lines, along with their corresponding entries in the R data and parameters lists. This simplifies the code to exactly what you need for debugging:

Simplified C++ code:

// Illustrates how to make the debugger catch a floating point error.
#include <TMB.hpp>
#include <fenv.h> // Extra line needed

template<class Type>
Type objective_function<Type>::operator() () {
  feenableexcept(FE_INVALID | FE_OVERFLOW | FE_DIVBYZERO | FE_UNDERFLOW); // Extra line needed
  
  Type f;
  f = sqrt(-1.); // FE_INVALID ( sqrt(-1.) returns NaN )
  //f = 1./0.; // FE_DIVBYZERO ( division by zero )
  //f = exp(100000.); // FE_OVERFLOW ( exp(100000.) returns Inf ) [Does not work on all platforms]
  //f = exp(-100000.); // FE_UNDERFLOW ( exp(-100000.) returns 0 )
  return f;
}

Simplified R code:

require(TMB)
compile('nan.cpp','-fno-gnu-unique -O0 -Wall')
dyn.load(dynlib('nan'))
model <- MakeADFun(list(), list()) # No data/parameters needed
fit <- nlminb(model$par, model$fn, model$gr)
rep <- sdreport(model)
print(rep)

All three approaches will get rid of those warnings. For a debugging example, Option 3 is probably the cleanest, but Option 1 is useful if you want to keep the template structure intact for later modifications.

内容的提问来源于stack exchange,提问作者Ellie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 07:13:27