Problem with float algorithm

Pages: 12
actually this is my course project and i stuck at reverse algorithm

"24h cycle format clock"
Here is my issue Prof. asking me a member function that permit data in both way:


*Hours/min/sec to float(1.5f hours like)
*Float to hours/min/sec



this is my algorithm for it

1st one -
float = hours+minutes/60+sseconds/3600

2nd one -
hours = f
minute =(60*f)%60
second = ? ? ? ? ? thats where i stucked ,i tryed many calculations on it couldnt reach to correct result.
Same as minutes. (60*60*f)%60. There are 60*60 seconds in an hour and the cycle is 60 secs.
Though if f is float, so is 60*60*f and you can't do % with floats. You'll have to cast it to an int.
I'm sorry but your question is not clear.

From what I can make you for the second question
float second = (f * 60 * 60) % 60;
Srry about question i should make it more clear
and tnx to all of you it worked i dont know how i didnt think reversing 1st method of mine it was Second/3600 ofc it is 60*60.Oh my my sometimes my brain stops


"hamsterman (1614) Link to this post Feb 15, 2011 at 11:25pm
Same as minutes. (60*60*f)%60. There are 60*60 seconds in an hour and the cycle is 60 secs.
Though if f is float, so is 60*60*f and you can't do % with floats. You'll have to cast it to an int."

didnt get it why i cant use moduler divide on floats and what do u mean by casting int(make it as paramater ? )
Last edited on
well, when I try to compile
1
2
3
4
5
int main(){
   float f = 7.5;
   float g = f*2 % 5;
   return 0;
}
I get "error C2296: '%' : illegal, left operand has type 'float' ". Don't you?
You then have to change float to int. That would be either g = int(f)*2%5; or g = int(f*2) % 5;. Conversion to int rounds down, so in the first version g = 7*2%5 = 4 and in the second one (7.5*2)%5 = 15%5 = 0 which is the correct answer.
Last edited on
let me show you full of mine program and the questions of Prof.I still dont believe im doin right :/(didnt compile it yet)

Create a class to operate on time and time intervals.Call the new class Time. All the three
Data components in this class for hours, minutes, seconds are integer variables. Minutes and seconds may contain values in the range 0…59. Include member functions and constructors to initialize Time to 0 and to initialize it to fixed values. A getdata() method should read validate data from standart input. A member function should display it 24 hour cycle format from 0:0:0 to 23:59:59. Include member function that permit data conversion in both directions :

• From real (float) value (1.5f hours ) to hours/min/sec (1h,30min,0sec)
• From hour/min/sec (1h,30min,0sec) to real (float) value (1.5f hours)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class Time
{
 Public:
int Hours;
int Minutes;
int Seconds;
}
void Time()
{
Hours=0;
Minute=0;
Seconds=0;
}
void Time(int h,int m,int s)
{
Hours=h;
Minute=m;
Seconds=s;
}
void getdata(int h,int m,int s)
{
if(h>=0&&h<24);
Hours<<h
if(m>=0&&m<60);
Minutes<<m
if(s>=0&&s<60);
Seconds<<s
}
Void timer(float f)
Hours=f;
Minutes=(60*f)%60;
Seconds=(60*60*f)%60;
}
Float Reverse(in h,int m,int s)
{
float=h+(m/60)+(s/3600);
}
Last edited on
You need to fix a bit if your doing this with classes:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class Time
{
Public: // reserved word is lowercase
  int Hours;
  int Minutes;
  int Seconds;
} // missing semicolon
void Time() // Error here (part of class, Time is constructor, no type)
{
  Hours=0;
  Minute=0;
  Seconds=0;
}
void Time(int h,int m,int s) // Error here (part of class, Time is constructor, no type)
{
  Hours=h;
  Minute=m;
  Seconds=s;
}
void getdata(int h,int m,int s) // Error here (part of class?)
{
  if(h>=0&&h<24); // Error here (semicolon?)
    Hours<<h // Error here (semicolon? Assignment)
  if(m>=0&&m<60); // Error here (semicolon?)
    Minutes<<m // Error here (semicolon? Assignment)
  if(s>=0&&s<60); // Error here (semicolon?)
    Seconds<<s // Error here (semicolon? Assignment)
}
Void timer(float f) // lowercase reserved word void, part of class?
  Hours=f;
  Minutes=(60*f)%60;
  Seconds=(60*60*f)%60;
}
Float Reverse(in h,int m,int s) // lowercase reserved word float, part of class?
{
  float=h+(m/60)+(s/3600);
}
how about " A member function should display it 24 hour cycle format from 0:0:0 to 23:59:59."
how im gonna do that.Thnks btw ill fix em
1
2
3
  Hours=f;
  Minutes=(60*f)%60;
  Seconds=(60*60*f)%60;

do this instead:
1
2
3
4
  f-=Hours=f;//subtracts integer component of f from f, simultaneously assigning it to Hours.
  Minutes=f*=60;// multiplied f by 60 and puts the integer part in Minutes
f-=Minutes;
  Seconds=f*60;

Last edited on
Yes. That's very readable. I can't believe you wasted a whole line for only one subtraction, instead of making line 2 f-=Minutes=f*=60; though..
That not defined, because you can't alter a variable twice in one statement.
tnx rocketboy9000
Last edited on
Are you sure? The order of evaluation seems clear to me. Can you think of two different ways to evaluate that line?
Yes.
You could do:
1
2
f-=Minutes=f*60;
f*=60;

Or:
1
2
f*=60;
f-=Minutes=f;
Expecting *= to return a value before performing the assignment is a bit silly.

Though if you need a more solid argument,
The standard wrote:
The result of the assignment operation is the value stored in the left operand after the assignment has taken place
edit: though now that I look at it, it too can be interpreted your way..
Last edited on
Yeah.. the reason it's so vague is because that way greater optimization can be done.
man it is way more complicated than it seems
Last edited on
only if you write it that way.
1
2
3
  Hours=f;
  Minutes=int(60*f)%60;
  Seconds=int(60*60*f)%60;
is much better than what rocketboy wrote. It may have more operations, but at least it's readable.
how about "A member function should display it 24 hour cycle format from 0:0:0 to 23:59:59"
i got an idea but still dunno how to code it(it will be in ma main?)
which one is the problem?
making a member function or printing three numbers separated with ':' ?
Pages: 12