PDA

View Full Version : Programming in C (overflow issue?)


Cuc Tu
09-09-2009, 09:05 PM
Never used C and my programming skills in general are at a novice level...

I'm working in a somewhat restricted programming environment and must use a variable defined either as a long or float, but I cannot loose precision in my calculation. Here is the line that I think is going to give me a problem:


unsigned long tune_word = (freq*pow(2,32)/clock);


In this example, freq is a long that can range from 1 to 99,999,999 and clock = 180,000,000

The problem is that the multiplication of 2^32 is already the max value that a long can hold, and multiplying the frequency overflows. If I try to use a float, I do not have enough precision and end up loosing resolution in the tune word (at least I think so, but have not really tested it).

Any ideas how to overcome this?

Also note that my program environment does not facilitate a double (or its implementation is the same as float).

Cuc Tu
09-10-2009, 02:53 PM
Well, I think it just cannot be done!

I can divide 2^32 by clock and round it to the longest float value as a constant, which results in a linearly increasing error with freq.

123456
07-06-2010, 12:11 PM
Mods informed.