fork download
  1. #include <stdio.h>
  2. #include <stdint.h>
  3.  
  4. #define INT_MAX 0x7fffffff
  5. #define SCALED_ADC_AT_ZERO_VOLTS (65536 * 390 / (390 + 10))
  6.  
  7. int32_t hv_volts_to_adc(float v)
  8. {
  9. return (int32_t)(v * 65536 / 3.3 * 10 / (390 + 10)) +
  10. (65536 * 390.0 / (390 + 10));
  11. }
  12.  
  13. int main(void) {
  14. // your code goes here
  15. uint32_t hvTargetAdcReal = hv_volts_to_adc(-55);
  16. printf("Before clamp=%u\n", hvTargetAdcReal);
  17. if (hvTargetAdcReal > INT_MAX)
  18. {
  19. hvTargetAdcReal = INT_MAX;
  20. }
  21. printf("INT_MAX=%u", INT_MAX);
  22. printf("-INT_MAX=%u", -INT_MAX);
  23. printf("After clamp=%u\n", hvTargetAdcReal);
  24. int32_t adcAtZero = SCALED_ADC_AT_ZERO_VOLTS;
  25. int32_t result = (adcAtZero - (int32_t)hvTargetAdcReal);
  26. int32_t result2 = (int32_t)(adcAtZero - hvTargetAdcReal);
  27.  
  28. return 0;
  29. }
  30.  
Success #stdin #stdout 0s 5280KB
stdin
Standard input is empty
stdout
Before clamp=36591
INT_MAX=2147483647-INT_MAX=2147483649After clamp=36591