Cast float to int, or int to float?

C. Korb

I can define a constant as either a float or a 32-bit uint:

const float SecondsPerMinute = 60.0F;

or

const uint32 SecondsPerMinute = 60U;

The const is used in some equations that expect an int and some equations that expect a float. I want to make my compiler and static analysis tools happy so I will static_cast it to the appropriate type as necessary.

Is it better to define the const as a float and cast to an int, or define it as an int and cast it to a float? Does it make a difference, or is this more a matter of personal opinion?

Let's say the constant is used an equal number of times as an int and as a float.

Kerrek SB

How about a template:

template <typename T>
constexpr T SecondsPerMinute = T(60);

Usage:

std::printf("%d %f", SecondsPerMinute<int>, SecondsPerMinute<double>);

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Python float to int, and int to str

来自分类Dev

从'float'转换为'int'?

来自分类Dev

format()int为float

来自分类Dev

整个float与int OverflowError

来自分类Dev

基于int生成float

来自分类Dev

使用FParsec解析float或int * float

来自分类Dev

为什么 (int)==(float) 总是编译为 (float)==(float)

来自分类Dev

在float和int上>>的TypeError

来自分类Dev

(发布)Int vs float性能

来自分类Dev

将Float转换为Int

来自分类Dev

使用FParsec解析int或float

来自分类Dev

在C中将float与int相乘

来自分类Dev

在float和int上>>的TypeError

来自分类Dev

将float转换为int

来自分类Dev

(发布)Int vs float的性能

来自分类Dev

float(int)const的类型特征

来自分类Dev

使用int计算并输出float?

来自分类Dev

int float 与 double 的转换偏好

来自分类Dev

MIT Scheme Int VS Float

来自分类Dev

将float转换为int隐式vs显式(cast)差异

来自分类Dev

如果((float)(int_one && int_two)==(float)int_one &&(float)int_two)总是评估为“ false”

来自分类Dev

错误:无法将“ float(*)(int)”转换为“ float”

来自分类Dev

将pandas float系列转换为int

来自分类Dev

将int转换为float / double

来自分类Dev

将int位转换为float位

来自分类Dev

使用float_of_int进行类型转换

来自分类Dev

从int到float并返回时符号变化

来自分类Dev

错误:“ Int”不可转换为“ @lvalue Float”

来自分类Dev

将float *或int *分配给mxArray