Robby
2010-02-01 16:44:01 UTC
Hello,
Okay, this question has to do alot with my previous post that I posted
yesterday. I don't understand why the answer (of type float) gets rounded off
the the lower number and looses its precision after the decimal. Here is the
code again.
====================================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.0/SYS_OSC) // 1 second divide by system clock
#define DELAY_LOOP_IC 7.0 // 7 instructions per while loop iteration (delay)
#define RATIO(a,b,c) (a/(b*c)) // a,b,c parameters
int main()
{
unsigned int us_delay=16;
float x;
x = (float)RATIO((float)MICRO_SEC, (float)OSC_PERIOD, (float)DELAY_LOOP_IC)
* (float)us_delay;
return 0;
}
====================================
In response to x, the representauon of the RATIO macro multiplied by
"us_delay" should be the following calculation:
24.06015038 = (0.000001 / (0.000000095/7.0)) * 16
but VC gets rid of the numbers after the decimal and gives:
24.000000 = (0.000001 / (0.000000095/7.0)) * 16
The thing is that I require x to be with numbers after the decimal... like
this:
24.06015038
I have typed casted all the values, I have even set the 7 as a 7.0 and I
fixed the divide by zero mistake which was pointed out in my previous post...
and I made "x" as a floating type and us_delay is typ casted too to a
float!!!!
I guess I am still missing something... ? Help!
Okay, this question has to do alot with my previous post that I posted
yesterday. I don't understand why the answer (of type float) gets rounded off
the the lower number and looses its precision after the decimal. Here is the
code again.
====================================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.0/SYS_OSC) // 1 second divide by system clock
#define DELAY_LOOP_IC 7.0 // 7 instructions per while loop iteration (delay)
#define RATIO(a,b,c) (a/(b*c)) // a,b,c parameters
int main()
{
unsigned int us_delay=16;
float x;
x = (float)RATIO((float)MICRO_SEC, (float)OSC_PERIOD, (float)DELAY_LOOP_IC)
* (float)us_delay;
return 0;
}
====================================
In response to x, the representauon of the RATIO macro multiplied by
"us_delay" should be the following calculation:
24.06015038 = (0.000001 / (0.000000095/7.0)) * 16
but VC gets rid of the numbers after the decimal and gives:
24.000000 = (0.000001 / (0.000000095/7.0)) * 16
The thing is that I require x to be with numbers after the decimal... like
this:
24.06015038
I have typed casted all the values, I have even set the 7 as a 7.0 and I
fixed the divide by zero mistake which was pointed out in my previous post...
and I made "x" as a floating type and us_delay is typ casted too to a
float!!!!
I guess I am still missing something... ? Help!
--
Best regards
Roberto
Best regards
Roberto