User Guide
codegen_utils.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2023 Blue Brain Project, EPFL.
3  * See the top-level LICENSE file for details.
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
9 
11 
12 namespace nmodl {
13 namespace codegen {
14 namespace utils {
15 /**
16  * \details We can directly print value but if user specify value as integer then
17  * then it gets printed as an integer. To avoid this, we use below wrappers.
18  * If user has provided integer then it gets printed as 1.0 (similar to mod2c
19  * and neuron where ".0" is appended). Otherwise we print double variables as
20  * they are represented in the mod file by user. If the value is in scientific
21  * representation (1e+20, 1E-15) then keep it as it is.
22  */
23 std::string format_double_string(const std::string& s_value) {
24  double value = std::stod(s_value);
25  if (std::ceil(value) == value && s_value.find_first_of("eE") == std::string::npos) {
26  return fmt::format("{:.1f}", value);
27  }
28  return s_value;
29 }
30 
31 
32 std::string format_float_string(const std::string& s_value) {
33  float value = std::stof(s_value);
34  if (std::ceil(value) == value && s_value.find_first_of("eE") == std::string::npos) {
35  return fmt::format("{:.1f}", value);
36  }
37  return s_value;
38 }
39 } // namespace utils
40 } // namespace codegen
41 } // namespace nmodl
nmodl
encapsulates code generation backend implementations
Definition: ast_common.hpp:26
nmodl::codegen::utils::format_float_string
std::string format_float_string(const std::string &s_value)
Handles the float constants format being printed in the generated code.
Definition: codegen_utils.cpp:32
codegen_cpp_visitor.hpp
Visitor for printing C++ code compatible with legacy api of CoreNEURON
codegen_utils.hpp
Implement utility functions for codegen visitors.
nmodl::codegen::utils::format_double_string
std::string format_double_string(const std::string &s_value)
Handles the double constants format being printed in the generated code.
Definition: codegen_utils.cpp:23