View previous topic :: View next topic |
Author |
Message |
aaronik19
Joined: 25 Apr 2011 Posts: 297
|
float variables issue |
Posted: Thu Mar 12, 2020 7:54 am |
|
|
Dear All,
I have a problem on the c code. I am using a moisture of float and integers variables. The strange part is this:
Code: |
void dectotime() //convert the sorted second back to time in the AlarmSort Array
{
float AlarmSorted_Hours, AlarmSorted_Minutes, AlarmSorted_Seconds;
int Alarm1Array_Index, AlarmSecondsSort_Index;
Alarm1Array_Index = 0;
for (AlarmSecondsSort_Index=0; AlarmSecondsSort_Index<=4; AlarmSecondsSort_Index++)
{
AlarmSorted_Hours = (AlarmSecondsSort[AlarmSecondsSort_Index]);
AlarmSorted_Hours = (AlarmSecondsSort[AlarmSecondsSort_Index]/3600);
printf("%f\n", (float)(AlarmSecondsSort[AlarmSecondsSort_Index])/3600);
Alarm1[Alarm1Array_Index]=floor(AlarmSorted_Hours);
Alarm1Array_Index++;
AlarmSorted_Minutes = (float)(AlarmSorted_Hours - floor(AlarmSorted_Hours));
AlarmSorted_Minutes = AlarmSorted_Minutes * 60;
Alarm1[Alarm1Array_Index] = floor(AlarmSorted_Minutes);
Alarm1Array_Index++;
AlarmSorted_Seconds = (float)(AlarmSorted_Minutes - floor(AlarmSorted_Minutes)) * 60;
Alarm1[Alarm1Array_Index]=AlarmSorted_Seconds;
Alarm1Array_Index++;
}
} |
this is part of the code... there are no errors during compiling, but for example in the first part when the
Code: | AlarmSorted_Hours = (AlarmSecondsSort[AlarmSecondsSort_Index]/3600); |
AlarmSecondsSort[AlarmSecondsSort_Index] is 85230 and is divided by 3600 the answer must be 23.675, but the compiler returns only 23.00. The array AlarmSecondsSort is declared as integer since it only holds whole numbers.
What could be the problem please? Let me know if you need more information. |
|
|
newguy
Joined: 24 Jun 2004 Posts: 1907
|
Re: float variables issue |
Posted: Thu Mar 12, 2020 8:22 am |
|
|
aaronik19 wrote: | Code: | AlarmSorted_Hours = ((float)AlarmSecondsSort[AlarmSecondsSort_Index]/3600); |
|
Try this. |
|
|
aaronik19
Joined: 25 Apr 2011 Posts: 297
|
|
Posted: Thu Mar 12, 2020 8:55 am |
|
|
perfect!! thanks |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19496
|
|
Posted: Thu Mar 12, 2020 10:26 am |
|
|
Understand the reason.
When you perform:
AlarmSorted_Hours = (AlarmSecondsSort[AlarmSecondsSort_Index]/3600);
Because the values on the right are both integers, inreger maths is used.
You can either tell the compiler to convert the first value to float with the
cast (float), or make the second value a float:
AlarmSorted_Hours =
(AlarmSecondsSort[AlarmSecondsSort_Index]/3600.0);
Adding the .0 makes the divisor a float value.
As soon as either value is a float, float arithmetic is used. |
|
|
aaronik19
Joined: 25 Apr 2011 Posts: 297
|
|
Posted: Thu Mar 12, 2020 12:09 pm |
|
|
Thanks my friends....tested both methods and worked perfectly! |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19496
|
|
Posted: Sat Mar 14, 2020 1:32 am |
|
|
Good.
It might be worth your while testing which gives the smaller and faster code.
I've had reason to do this in the past and when I did it found that it was
'better' to declare the divisor as a float constant, giving slightly smaller
code, and a little bit faster results. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9221 Location: Greensville,Ontario
|
|
Posted: Sat Mar 14, 2020 4:53 am |
|
|
Since this is about 'time', I'm wondering why floats have to be used? Whenever 'floats' are used they cost a lot of codespace and execution time.
There should be faster integer based math 'somewhere' on the internet ? |
|
|
|