Time problem 
Author Message
 Time problem

Hi,
I want to calculatie with time, for that I convert the time into
seconds. But If I want to transfer the seconds back to the time I have
a little problem.
But when I am at 19 for the hour's the number in temp is 859 and it
shold be -64677. How is this possible?

The program:

var total         : longint;
    i              : integer;
    h, m, s        : integer;
    temp           : longint;

begin
  h := 1;
  m := 2;
  s := 3;
  total := (h*3600)+(m*60)+s;
  writeln;
  writeln(total);
  writeln(h, ':', m, ':', s);
  h := 0;
  m := 0;
  s := 0;
  for i := 24 downto 1 do
  begin
    temp := total-i*3600;                               <- HERE IT IS!
    if (total-i*3600) >= 1 then
    begin
      h := i;
      total := total - (h*3600);
    end;
  end;
  for i := 60 downto 1 do
  begin
    if (total-(i*60)) >= 1 then
    begin
      m := i;
      total := total - (m*60);
    end;
  end;
  s := total;
  writeln(h, ':', m, ':', s);
end.



Wed, 18 Jun 1902 08:00:00 GMT  
 Time problem

Quote:

>     temp := total-i*3600;                               <- HERE IT IS!

i is an integer, as is 3600. The result of the calculation might overflow.
Either cast 3600 to a longint of make i a longint.

M.

--
Martin Larsson, author of several unknown utilities for DOS and Windows.

http://www.delfi.infonet.no
X.400:G=martin;S=larsson;O=delfi-data;P=msmail;A=telemax;C=no



Wed, 18 Jun 1902 08:00:00 GMT  
 Time problem

Quote:

>Hi,
>I want to calculatie with time, for that I convert the time into
>seconds. But If I want to transfer the seconds back to the time I have
>a little problem.
>But when I am at 19 for the hour's the number in temp is 859 and it
>shold be -64677. How is this possible?
>The program:
>var total         : longint;
>    i              : integer;
>    h, m, s        : integer;
>    temp           : longint;
>begin
>  h := 1;
>  m := 2;
>  s := 3;
>  total := (h*3600)+(m*60)+s;
>  writeln;
>  writeln(total);
>  writeln(h, ':', m, ':', s);
>  h := 0;
>  m := 0;
>  s := 0;
>  for i := 24 downto 1 do
>  begin
>    temp := total-i*3600;                               <- HERE IT IS!

I suppose you have range checking turned off.

The problem is in the way TP evaluates variables.  A longint is 32 bits, but
an integer is only 16.

In the above (and below) equations where i is referenced, the expression
i*3600 is calculated as an integer.  When i=24, i*3600=86400, which doesn't
fit into an integer value (should cause a range error).

There are two possible solutions.  The simple one is to change i to a
longint, which will allow using it to evaluate larger numbers.  The only
problem I can think of with this is that you may sometimes bring out other
conflicts with this.

The other is to rewrite the references to i using a typecast.  The above
would be changed to:

  temp := total-longint(i)*3600;

Typecasting is similar to calling a function.  The result of doing this is
that i is still a 16-bit variable, but for the sake of the evaluation, it's
treated like a 32-bit longint.

Other changes along these lines would need to be changed.

- Show quoted text -

Quote:
>    if (total-i*3600) >= 1 then
>    begin
>      h := i;
>      total := total - (h*3600);
>    end;
>  end;
>  for i := 60 downto 1 do
>  begin
>    if (total-(i*60)) >= 1 then
>    begin
>      m := i;
>      total := total - (m*60);
>    end;
>  end;
>  s := total;
>  writeln(h, ':', m, ':', s);
>end.

Nevertheless, this algorithm isn't terribly efficient.  When

total := h*3600+m*60+s*;

then,

h := total mod 3600;
total := total div 3600;
m := total mod 60;
total := total div 60;
s := total;

These 5 lines should be a good replacement for your 20 or so.

--
Scott F. Earnest           | We now return you to our regularly scheduled



Wed, 18 Jun 1902 08:00:00 GMT  
 Time problem


Quote:

>Hi,
>I want to calculatie with time, for that I convert the time into
>seconds. But If I want to transfer the seconds back to the time I
>have a little problem.
>But when I am at 19 for the hour's the number in temp is 859 and
>it shold be -64677. How is this possible?

>The program:

>var total         : longint;
>    i              : integer;
>    h, m, s        : integer;
>    temp           : longint;

>begin
>  h := 1;
>  m := 2;
>  s := 3;
>  total := (h*3600)+(m*60)+s;

The variables h, m, and s are integersm therefore the the
expression is solved for integer.  The integer result is then
extended to longint and stored at Totle.

You need to force h to longint so that the expression will be
solved for a longint.

  total := (longint(h)*3600)+(m*60)+s;



Wed, 18 Jun 1902 08:00:00 GMT  
 Time problem

Quote:

> Hi,
> I want to calculatie with time, for that I convert the time into
> seconds. But If I want to transfer the seconds back to the time I have
> a little problem.
> But when I am at 19 for the hour's the number in temp is 859 and it
> shold be -64677. How is this possible?

> The program:

> var total         : longint;
>     i              : integer;
>     h, m, s        : integer;
>     temp           : longint;

> begin
>   h := 1;
>   m := 2;
>   s := 3;
>   total := (h*3600)+(m*60)+s;
>   writeln;
>   writeln(total);
>   writeln(h, ':', m, ':', s);
>   h := 0;
>   m := 0;
>   s := 0;
>   for i := 24 downto 1 do
>   begin
>     temp := total-i*3600;                               <- HERE IT IS!
>     if (total-i*3600) >= 1 then
>     begin
>       h := i;
>       total := total - (h*3600);

                          ^^^^^^-----this is the problem.

Quote:
>     end;
>   end;
>   for i := 60 downto 1 do
>   begin
>     if (total-(i*60)) >= 1 then
>     begin
>       m := i;
>       total := total - (m*60);
>     end;
>   end;
>   s := total;
>   writeln(h, ':', m, ':', s);
> end.

What's "h*3600"? Remember, h is an _integer_, therefore any calc
performed using it and a constant small enough to be an integer will be
stored in an integer. So if h is greater than 18, you overflow the
integer type!

The "quick fix" is to replace "h*3600" with "longint(h)*3600". Thgis
sort of typecasting is *required* if you want intermediate results to
come out right.

I think your code could use some work too. Check out the code below.

type
  t_type : record
    hour,
    min,
    sec : byte
  end;

var
  time    : t_type;
  total   : longint;
  h, m, s : integer;

function hms_to_sec(h,m,s:integer):longint;
begin
   hms_to_sec := longint(h)*3600 + m*60 + s
end

procedure sec_to_hms(s : longint;var hms:t_type);
begin
  hms.hour := s div 3600;
         s := s mod 3600;
   hms.min := s div 60;
   hms.sec := s mod 60
end;

begin
  h := 1;
  m := 2;
  s := 3;
  total := hms_to_sec(h,m,s);
  writeln;
  writeln(total);
  writeln(h, ':', m, ':', s);
  sec_to_hms(total,time);
  writeln(time.hour, ':', time.min, ':', time.sec);
end.

Leonard Erickson (aka Shadow)




Wed, 18 Jun 1902 08:00:00 GMT  
 Time problem


Quote:
>I want to calculatie with time, for that I convert the time into
>seconds. But If I want to transfer the seconds back to the time I have
>a little problem.
>But when I am at 19 for the hour's the number in temp is 859 and it
>shold be -64677. How is this possible?

Hi.

Try using your de{*filter*} when working with Turbo Pascal 5/6 or Borland Pascal 7.
Write a condition and set a mark. If the condition is true the program halts.
Then you're able to view all of your variables-contents at that moment.

I'm sure with this-you'll find the bug.

Bye.Jaro.

{It seemed to boring to kill myself the odd way - So i went to McDonalds}



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Setting SQL statements at run time - problems

2. Time problem with bp7 under NT

3. TP7 Timing problem

4. wm_close timing problem ?

5. Run-time problem with Datasets

6. TField creation at run time problems

7. ReportSmith Run Time problems

8. Problem with Delphi and Access settings (run-time)

9. still having problem using Free Pascal for the first time

10. Problems Accessing GFX Mode Second Time Round!

11. DBGrid Changing color of selected line and Time Display Problem

12. Filter Problem On Time Fields

 

 
Powered by phpBB® Forum Software