[英]How to define a constant by expression in C++?
I need some like this: 我需要这样的一些:
const float ratio = 1/60;
How to do this? 这该怎么做?
正如您所做的那样,但告诉编译器表达式中的值是带有“f”后缀的浮点数
const float ratio = 1.0f/60.0f;
你真的不需要constexpr
,这将在C
或C++
:
const float ratio = 1./60;
If you need a ratio, as opposed to the result of one, you can use std::ratio : 如果你需要一个比率,而不是一个比例的结果,你可以使用std :: ratio :
constexpr one_sixtieth = std::ratio<1, 60>();
constexpr auto n = one_sixtieth.num;
constexpr auto d = one_sixtieth.den;
It comes with a set of useful compile time operations . 它带有一组有用的编译时操作 。
You try this 你试试这个
const float numerator =1;
const float denominator =60;
const float ratio = numerator/denominator;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.