cprintftinyosnesc

nesC error to divide two integer when the partial result is minor of 1


This is a part of my code to explain my problem:

int64_t packet_tx=3;
int64_t packet_rx=5;
int64_t packet_loss;

printf("Packet_loss: %d",((packet_tx-packet_rx)/packet_tx)*100);

In this code is ever packet_tx>=packet_rx;

The result is an integer but the intermediate result is not an integer. How can i resolve my problem? I tried to do cast with double of the var packet_tx and packet_rx before the division. but it doesn't work.


Solution

  • Besides casting to double, you also need to use the format specifier for double, %lf, in printf().

    printf("Packet_loss: %lf", ((double)(packet_tx-packet_rx)/(double)packet_tx)*100.0);