Discussion:
"PORTING C"> Divide by 0 error in define macro ???
(too old to reply)
Robby
2010-02-01 01:52:01 UTC
Permalink
Hello,

Can someone tell me as to why I am getting a divide by zero error in the
following fragment of code.

===========================================main.c
#include <stdio.h>

#define SYS_OSC 10500000 // Current system clock 10.5 MHZ
#define MICRO_SEC 0.000001 // 1 micro second
#define OSC_PERIOD (1/SYS_OSC) // 1 second divide by system clock
#define OVERHEAD_IC 58 // 58 overhead instructions
#define DELAY_LOOP_IC 7 // 7 instructions per while loop
iteration
#define RATIO(a,b,c) (a/(b*c)) // a, b, c macro parameters

int main()
{
unsigned int us_delay;
float x;

x = (float)RATIO((float)MICRO_SEC, (float)OSC_PERIOD, (float)DELAY_LOOP_IC)
* (float)us_delay;

return 0;
}
===============================================

I don't think I am dividing by zero !

b*c = 0.665 us

and

a = 1us

so this is what I expect no?

1us/0.665us = 1.503759

Unless the compiler sees 0.000000665 as a "0" ?????
Confused?

All help is sincerely appreciated. Thanks!
--
Best regards
Rob
Random
2010-02-01 02:20:21 UTC
Permalink
#define SYS_OSC      10500000         // Current system clock 10.5 MHZ
#define OSC_PERIOD             (1/SYS_OSC)  // 1 second divide by system clock
OSC_PERIOD will evaluate to zero since both types in the expression
are ints. Promote one to a double or float if you want a floating
point result.
David Wilkinson
2010-02-01 02:20:31 UTC
Permalink
Post by Robby
Hello,
Can someone tell me as to why I am getting a divide by zero error in the
following fragment of code.
===========================================main.c
#include <stdio.h>
#define SYS_OSC 10500000 // Current system clock 10.5 MHZ
#define MICRO_SEC 0.000001 // 1 micro second
#define OSC_PERIOD (1/SYS_OSC) // 1 second divide by system clock
#define OVERHEAD_IC 58 // 58 overhead instructions
#define DELAY_LOOP_IC 7 // 7 instructions per while loop
iteration
#define RATIO(a,b,c) (a/(b*c)) // a, b, c macro parameters
int main()
{
unsigned int us_delay;
float x;
x = (float)RATIO((float)MICRO_SEC, (float)OSC_PERIOD, (float)DELAY_LOOP_IC)
* (float)us_delay;
return 0;
}
===============================================
I don't think I am dividing by zero !
b*c = 0.665 us
and
a = 1us
so this is what I expect no?
1us/0.665us = 1.503759
Unless the compiler sees 0.000000665 as a "0" ?????
Confused?
All help is sincerely appreciated. Thanks!
Your definition of OSC_PERIOD uses integer division. Try

#define OSC_PERIOD (1.0/SYS_OSC)
--
David Wilkinson
Visual C++ MVP
Robby
2010-02-01 15:51:01 UTC
Permalink
Hello everyone,
10500000 is an integer. 1 is an integer. So, this division will be done
with integer division, and OSC_PERIOD will be 0.
A lesson I just learnt!

Thank you all for your help!
--
Best regards
Roberto
Post by Robby
Hello,
Can someone tell me as to why I am getting a divide by zero error in the
following fragment of code.
===========================================main.c
#include <stdio.h>
#define SYS_OSC 10500000 // Current system clock 10.5 MHZ
#define MICRO_SEC 0.000001 // 1 micro second
#define OSC_PERIOD (1/SYS_OSC) // 1 second divide by system clock
#define OVERHEAD_IC 58 // 58 overhead instructions
#define DELAY_LOOP_IC 7 // 7 instructions per while loop
iteration
#define RATIO(a,b,c) (a/(b*c)) // a, b, c macro parameters
int main()
{
unsigned int us_delay;
float x;
x = (float)RATIO((float)MICRO_SEC, (float)OSC_PERIOD, (float)DELAY_LOOP_IC)
* (float)us_delay;
return 0;
}
===============================================
I don't think I am dividing by zero !
b*c = 0.665 us
and
a = 1us
so this is what I expect no?
1us/0.665us = 1.503759
Unless the compiler sees 0.000000665 as a "0" ?????
Confused?
All help is sincerely appreciated. Thanks!
Your definition of OSC_PERIOD uses integer division. Try
#define OSC_PERIOD (1.0/SYS_OSC)
--
David Wilkinson
Visual C++ MVP
.
Tim Roberts
2010-02-01 02:44:32 UTC
Permalink
Post by Robby
Can someone tell me as to why I am getting a divide by zero error in the
following fragment of code.
Absolutely.
Post by Robby
===========================================main.c
#include <stdio.h>
#define SYS_OSC 10500000 // Current system clock 10.5 MHZ
#define MICRO_SEC 0.000001 // 1 micro second
#define OSC_PERIOD (1/SYS_OSC) // 1 second divide by system clock
10500000 is an integer. 1 is an integer. So, this division will be done
with integer division, and OSC_PERIOD will be 0.
Post by Robby
#define OVERHEAD_IC 58 // 58 overhead instructions
#define DELAY_LOOP_IC 7 // 7 instructions per while loop
iteration
#define RATIO(a,b,c) (a/(b*c)) // a, b, c macro parameters
int main()
{
unsigned int us_delay;
float x;
x = (float)RATIO((float)MICRO_SEC, (float)OSC_PERIOD, (float)DELAY_LOOP_IC)
* (float)us_delay;
The 0 in OSC_PERIOD gets converted to a float, multiplied by DELAY_LOOP_IC
which leaves it as a 0, then you use it as a divisor.
Post by Robby
I don't think I am dividing by zero !
b*c = 0.665 us
Nope. b*c is 0.
--
Tim Roberts, ***@probo.com
Providenza & Boekelheide, Inc.
Loading...