CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

float variables issue

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
aaronik19



Joined: 25 Apr 2011
Posts: 297

View user's profile Send private message

float variables issue
PostPosted: Thu Mar 12, 2020 7:54 am     Reply with quote

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: 1909

View user's profile Send private message

Re: float variables issue
PostPosted: Thu Mar 12, 2020 8:22 am     Reply with quote

aaronik19 wrote:
Code:
AlarmSorted_Hours = ((float)AlarmSecondsSort[AlarmSecondsSort_Index]/3600);


Try this.
aaronik19



Joined: 25 Apr 2011
Posts: 297

View user's profile Send private message

PostPosted: Thu Mar 12, 2020 8:55 am     Reply with quote

perfect!! thanks Smile
Ttelmah



Joined: 11 Mar 2010
Posts: 19545

View user's profile Send private message

PostPosted: Thu Mar 12, 2020 10:26 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Mar 12, 2020 12:09 pm     Reply with quote

Thanks my friends....tested both methods and worked perfectly!
Ttelmah



Joined: 11 Mar 2010
Posts: 19545

View user's profile Send private message

PostPosted: Sat Mar 14, 2020 1:32 am     Reply with quote

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. Very Happy
temtronic



Joined: 01 Jul 2010
Posts: 9245
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sat Mar 14, 2020 4:53 am     Reply with quote

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 ?
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group