简体   繁体   中英

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. 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.

Is there any way to convert char to double?

when fret = 12, div =1. but when fret is not a multiple of 12, it returns 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 . You should use a double -literal 12.0 here. But the assignement to char div would truncate the result anyway, so change this to a double . Lastly you want to print this with the correct format specifier %f . So you get something like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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