1

This is my issue, i need to calculate how many clock cycles are in a ms and us for my pll waits on a TM4C123. However the values are coming out as zero on my double making my other values incorrect. Any help would be much appreciated.

#include <stdio.h>

int main()
{
    float ms_cycled;
    float us_cycled;
    int ms_cycle;
    int us_cycle;
    unsigned long MHz = 50;
    double TPC = 1/(MHz*10000000); //calculate time per clock cycle

    //calculate cycles for ms
    ms_cycled = 0.001/TPC;
    ms_cycle = ms_cycled;
    //calculate cycles for us
    us_cycled = 0.000001/TPC;
    us_cycle = us_cycled;
    printf("TPC = %.16f \n", TPC);
    printf("ms_cycled = %f \n", ms_cycled);
    printf("us_cycled = %f \n", us_cycled);
    printf("ms_cycle = %i \n", ms_cycle);
    printf("us_cycle = %i \n", us_cycle);
    return 0;
}

The outputs I get are:

TPC = 0.0000000000000000                                                                                                                             
ms_cycled = inf                                                                                                                                      
us_cycled = inf                                                                                                                                      
ms_cycle = -2147483648                                                                                                                               
us_cycle = -2147483648
iBug
  • 35,554
  • 7
  • 89
  • 134
R0ck
  • 27
  • 1
  • 4
  • That is the most inefficient way to calculate clocks per ms I have ever seen. If you have `MHz`, you have clocks per second. And clocks per ms is simple a factor 1/1000 of it. Same goes for clocks per µs which is again a factor of 1/1000. `ms_cycle = MHZ * 1000; us_cycle = MHz`. Your value for `TPC` is off by factor 10 as you multiply MHz with 10^7 instead of 10^6. – Gerhardh Nov 27 '17 at 08:37

2 Answers2

4
double TPC = 1/(MHz*10000000);

Note that you're using integer arithmetics. So you get 1/(50*10000000) = 1/500000000 = 0 (of type unsigned long). Casting zero from unsigned long to double only gives you 0.0, not the correct result.

Changing one of the operands to floating point arithmetics should solve the problem:

double TPC = 1.0/(MHz*10000000.0)
              ^^              ^^

You can alternatively change this for the same result:

double MHz = 50.0;
               ^^
iBug
  • 35,554
  • 7
  • 89
  • 134
  • 1
    Note that if `MHz` is changed to `double`, it is no longer crucial that the other numbers be converted to floating point as the fact that `MHz` is `double` ensures that the calculation is done in `double` arithmetic. – Jonathan Leffler Nov 27 '17 at 05:20
  • 2
    The question isn't whether you know it; it's whether the OP knows it. Type consistency isn't bad, but it isn't always necessary. – Jonathan Leffler Nov 27 '17 at 05:23
  • @JonathanLeffler Got it. I re-worded my answer now. – iBug Nov 27 '17 at 05:28
  • Hello, please, can you also tell what's the difference between 1.0 and 1.0f please? I know that you can write those two ways but I don't know the difference – Cjdcoy Nov 27 '17 at 05:37
  • @Cjdcoy `1.0` is of type **double**, while `1.0f` is of type **float**. – iBug Nov 27 '17 at 06:29
  • fantastic, thank you so much! – R0ck Nov 27 '17 at 06:32
1

The expression 1/(MHz*10000000) has type unsigned long and value 0. Storing the result in a double does not make the result any less zero. Use 1.0/(MHz*10000000) instead.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711