Advertisement
Guest User

Untitled

a guest
Aug 25th, 2024
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. #include <stdint.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <math.h>
  5. #include <time.h>
  6.  
  7.  
  8. #define TICK_RATE 60 // 60 times per second
  9.  
  10.  
  11. uint32_t floatTest(uint32_t machine, uint32_t fuel);
  12. uint32_t intTest(uint32_t machine, uint32_t fuel);
  13. uint32_t int2Test(uint32_t machine, uint32_t fuel);
  14.  
  15.  
  16. int main(){
  17. uint32_t machine, fuel;
  18. uint32_t floatRet, intRet, int2Ret;
  19. srand(time(NULL));
  20.  
  21. for(uint32_t i = 0; i < 500000; i++){
  22. machine = rand() * 1000; // Always multiples of 1kW
  23. fuel = rand() * 1000; // Always multiples of 1kJ
  24.  
  25. floatRet = floatTest(machine, fuel);
  26. intRet = intTest(machine, fuel);
  27. int2Ret = int2Test(machine, fuel);
  28.  
  29. if((floatRet != intRet) || (floatRet != int2Ret) || (intRet != int2Ret)){
  30. printf("#%u float/int/int2 ticks: %u/%u/%u; machine/fuel: %ukW, %ukJ\n", i, floatRet, intRet, int2Ret, machine / 1000, fuel / 1000);
  31. }
  32. }
  33.  
  34. return 0;
  35. }
  36.  
  37.  
  38. uint32_t floatTest(uint32_t machine, uint32_t fuel){
  39. uint32_t ticks = 0;
  40. float fuelLeft = fuel;
  41. float consumptionPerTick = ((float)machine) / ((float)TICK_RATE);
  42.  
  43. // Simulate ticks of a machine until the fuel runs out
  44. while(fuelLeft > 0){
  45. ticks++;
  46. fuelLeft -= consumptionPerTick;
  47. }
  48.  
  49. return ticks;
  50. }
  51.  
  52. uint32_t intTest(uint32_t machine, uint32_t fuel){
  53. uint32_t ticks = 0;
  54. uint32_t ticksLeft = ceil(((float)fuel) / (((float)machine) / ((float)TICK_RATE)));
  55. // uint32_t ticksLeft = ceil((((float)fuel) / ((float)machine)) * ((float)TICK_RATE));
  56.  
  57. // Simulate ticks of a machine until the fuel runs out
  58. while(ticksLeft){
  59. ticks++;
  60. ticksLeft--;
  61. }
  62.  
  63. return ticks;
  64. }
  65.  
  66. uint32_t int2Test(uint32_t machine, uint32_t fuel){
  67. uint32_t ticks = 0;
  68. int64_t fuelLeft = fuel * TICK_RATE;
  69. uint64_t consumptionPerTick = machine;
  70.  
  71. // Simulate ticks of a machine until the fuel runs out
  72. while(fuelLeft > 0){
  73. ticks++;
  74. fuelLeft -= consumptionPerTick;
  75. }
  76. return ticks;
  77. }
  78.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement