wizzard: (Default)
[personal profile] wizzard

#define R_PU_33 17890UL
#define R_PD_33 988UL
#define ADC_V_REF 2560UL

#define MV_TO_ADC(MV,  R_PU, R_PD) (((MV)*(unsigned long)1024UL*(R_PD))/((ADC_V_REF)*((R_PU)+(R_PD))))

int _tmain(int argc, _TCHAR* argv[])
{
    unsigned long i=0;
    i=MV_TO_ADC(33000UL, R_PU_33, R_PD_33);
    printf("%l", i);
    return 0;
}

Этот код печатает “68” из-за integer overflow в числителе, несмотря на то, что везде где можно воткнуты unsigned long, и правильный ответ 690.

Что делать? Вручную раскрывать макросы не хочется, их подобных много. Проверено на msvc, gcc и avr-gcc (ATMEL).

(will be screened)
(will be screened if not validated)
If you don't have an account you can create one now.
HTML doesn't work in the subject.
More info about formatting

If you are unable to use this captcha for any reason, please contact us by email at support@dreamwidth.org

Profile

wizzard: (Default)
wizzard

January 2019

S M T W T F S
  12 345
6789101112
1314 1516171819
202122 23242526
2728293031  

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Jan. 4th, 2026 05:52 pm
Powered by Dreamwidth Studios