简体   繁体   English

将 const unsigned char 除以加倍?

[英]Dividing const unsigned char to double?

I'm working on an assignment that requires me to use "const unsigned char &fret" as input for a method.我正在处理一项要求我使用“const unsigned char &fret”作为方法输入的任务。 I have我有

void fretThing(const unsigned char &fret)
{
    char div = fret / 12;
    printf("%d\n", div);
}

but when I run the program, div = 0. I believe this is because char converts the number into an int, but when i try to cast to a double, it still does not work.但是当我运行程序时,div = 0。我相信这是因为 char 将数字转换为 int,但是当我尝试转换为 double 时,它仍然不起作用。

Is there any way to convert char to double?有没有办法将char转换为double?

when fret = 12, div =1.当fret = 12时,div = 1。 but when fret is not a multiple of 12, it returns 0.但当 fret 不是 12 的倍数时,它返回 0。

char div = added / 12;

depending on the type of added this probably is an integer divison, since 12 is an literal of type int .根据added的类型,这可能是 integer 分区,因为 12 是int类型的文字。 You should use a double -literal 12.0 here.您应该在这里使用double文字12.0 But the assignement to char div would truncate the result anyway, so change this to a double .但是对char div的赋值无论如何都会截断结果,因此将其更改为double Lastly you want to print this with the correct format specifier %f .最后,您想使用正确的格式说明符%f打印它。 So you get something like this:所以你会得到这样的东西:

void fretThing(const unsigned char &fret)
{
    double div = fret / 12.0;
    printf("%f\n", div);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM