0

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.

Dumbo
  • 13,555
  • 54
  • 184
  • 288
Nunzio Meli
  • 89
  • 2
  • 2
  • 8

1 Answers1

0

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);
timrau
  • 22,578
  • 4
  • 51
  • 64