Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdint.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <math.h>
- #include <time.h>
- #define TICK_RATE 60 // 60 times per second
- uint32_t floatTest(uint32_t machine, uint32_t fuel);
- uint32_t intTest(uint32_t machine, uint32_t fuel);
- uint32_t int2Test(uint32_t machine, uint32_t fuel);
- int main(){
- uint32_t machine, fuel;
- uint32_t floatRet, intRet, int2Ret;
- srand(time(NULL));
- for(uint32_t i = 0; i < 500000; i++){
- machine = rand() * 1000; // Always multiples of 1kW
- fuel = rand() * 1000; // Always multiples of 1kJ
- floatRet = floatTest(machine, fuel);
- intRet = intTest(machine, fuel);
- int2Ret = int2Test(machine, fuel);
- if((floatRet != intRet) || (floatRet != int2Ret) || (intRet != int2Ret)){
- printf("#%u float/int/int2 ticks: %u/%u/%u; machine/fuel: %ukW, %ukJ\n", i, floatRet, intRet, int2Ret, machine / 1000, fuel / 1000);
- }
- }
- return 0;
- }
- uint32_t floatTest(uint32_t machine, uint32_t fuel){
- uint32_t ticks = 0;
- float fuelLeft = fuel;
- float consumptionPerTick = ((float)machine) / ((float)TICK_RATE);
- // Simulate ticks of a machine until the fuel runs out
- while(fuelLeft > 0){
- ticks++;
- fuelLeft -= consumptionPerTick;
- }
- return ticks;
- }
- uint32_t intTest(uint32_t machine, uint32_t fuel){
- uint32_t ticks = 0;
- uint32_t ticksLeft = ceil(((float)fuel) / (((float)machine) / ((float)TICK_RATE)));
- // uint32_t ticksLeft = ceil((((float)fuel) / ((float)machine)) * ((float)TICK_RATE));
- // Simulate ticks of a machine until the fuel runs out
- while(ticksLeft){
- ticks++;
- ticksLeft--;
- }
- return ticks;
- }
- uint32_t int2Test(uint32_t machine, uint32_t fuel){
- uint32_t ticks = 0;
- int64_t fuelLeft = fuel * TICK_RATE;
- uint64_t consumptionPerTick = machine;
- // Simulate ticks of a machine until the fuel runs out
- while(fuelLeft > 0){
- ticks++;
- fuelLeft -= consumptionPerTick;
- }
- return ticks;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement