[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[avr-gcc-list] Rounded integral division in preprocessor

From: Johannes Bauer
Subject: [avr-gcc-list] Rounded integral division in preprocessor
Date: Fri, 05 Feb 2010 07:44:33 +0100
User-agent: Thunderbird (X11/20090817)

Dear list,

I'm trying to do rounded (opposed to truncated) integer division with avr-gcc with using only preprocessor magic. This proves to be difficult: The current code I'm using is

#define __IDIV(x, y)    (long)(((long)x + ((long)y / 2L)) / (long)y)
#define IDIV(x, y)      (__IDIV((x), (y)))

While this works like a charm inside the actual program:

void foo(void) {
        unsigned char x = IDIV(100, 3);
        PORTA = x;

It will not when used in preprocessor context:

#if IDIV(100, 3)
        #warning "Moo"

Because it chokes on the casts:

RS232.h:40:16: error: missing binary operator before token "("

Is there another way to do it?

Kind regards,

reply via email to

[Prev in Thread] Current Thread [Next in Thread]