c++ - Getting same constants to work for floats or doubles -


i enabling large code base alternate between single , double floating point precision via single typedef. @ present, code double-oriented:

double foo(double input) {     return 1.0/input; } 

a naive switch programmer-specified precision looks this:

#ifdef _use_doubles typedef double fptype; #else typedef float fptype; #endif  fptype foo(fptype input) {     return 1.0/input; } 

obviously, "1.0" causes compiler warning. best solution far treat every constant thus:

fptype foo(fptype input) {     return fptype(1.0)/input; } 

is there possibility explicit pod constructor invocation performed @ runtime, charging me speed penalty solving compiler warnings? suspect not, since compile time rewrite "ftype(constant)" "constantf" seems trivial. want make absolutely sure. use vc, want know c++ compilers in general.

also, there more elegant solution mine? fptype() invocations ugly when lots of constants feature in 1 expression:

fptype someval = fptype(1.0)/(someotherval+fptype(0.5))*(someval       /fptype(7.66))*fptype(43.33); 

i expect there compiler-specific approaches problem, seek compiler-agnostic one, if exists. naturally, warning suppression last resort, perhaps not @ all.

[edit] wrote question misunderstanding "direct initialization", explained @ constructor initialization of primitive data types in cpp.

for literal constants, use float type. conversion double happen without compiler warnings.

fptype foo(fptype input) {     return 1.0f/input; } 

Comments

Popular posts from this blog

user interface - How to replace the Python logo in a Tkinter-based Python GUI app? -

objective c - Greedy NSProgressIndicator Allocation -

how to set an OCR language in Google Drive -