[comp.lang.c] Convert char to float

swijaya@gara.une.oz.au (Sastra Wijaya STMA) (12/21/90)

I need to know how to convert a char variable into a float. The problem 
arises from calculating the elapsed time. First I get dosstampped of the
beginning process and the end process. The time I got from _dos_gettime(&tm), 
the variable tm is a struct of char.

What I did is:

elapsed = (tm2.hour-tm.hour)*3600+(tm2.minute-tm.minute)*60+(tm2.second-tm.second) + (tm2.hsecond-tm.hsecond)/100;

I use MSC 5.10  


Thanks for all your help,

Best Regard,

Sastra,

eager@ringworld.Eng.Sun.COM (Michael J. Eager) (12/21/90)

In article <5051@gara.une.oz.au> swijaya@gara.une.oz.au (Sastra Wijaya STMA) writes:
>I need to know how to convert a char variable into a float. 


RTFM:
STRTOD(3)              C LIBRARY FUNCTIONS              STRTOD(3)

NAME
     strtod, atof - convert string to double-precision number

     ...

mgjmw@research.cc.flinders.oz (Jon Whellams) (12/21/90)

In article <5051@gara.une.oz.au> swijaya@gara.une.oz.au (Sastra Wijaya STMA) writes:
>I need to know how to convert a char variable into a float. The problem 

Look up atof()..this will convert a string to a double
Under MSC you'll have to #include "stdlib.h"

dkeisen@Gang-of-Four.Stanford.EDU (Dave Eisen) (12/22/90)

In article <5051@gara.une.oz.au> swijaya@gara.une.oz.au (Sastra Wijaya STMA) writes:
>I need to know how to convert a char variable into a float. The problem 

No you don't. You want to convert a time as stored in a struct tm into a
float. Let me say that (unless you are using a preexisting library
that expects dates in a floating point format) you probably shouldn't do
this. Floating point numbers are for things that really do have
floating decimal points (READ: Scientific computation) and should not
be used for things like money and dates that can be stored in a 
format more suited to their structure. I personally find a struct
tm to be a very convenient way of handling dates, we sometimes also use
a character string "YYMMDD.HHSS" or two ints -- yymmdd and hhss. I 
can't see what a float buys you.

>the variable tm is a struct of char.

No. It is a struct tm. It has nothing to do with characters.
>

>

>elapsed = (tm2.hour-tm.hour)*3600+(tm2.minute-tm.minute)*60+(tm2.second-tm.second) + (tm2.hsecond-tm.hsecond)/100;

You are using elapsed time in hundredths of seconds, this strikes as being
the same as working with money.

The best way to do this is to adopt a convention that time is measured
in hundredths of seconds. Then do

  int elapsed;

  elapsed = (tm2.hour - tm.hour) * 3600 * 100 + (tm2.minute - .....

If you ***really*** want to use floating point, your method seems as
good as any. 




--
Dave Eisen                      	    dkeisen@Gang-of-Four.Stanford.EDU
1447 N. Shoreline Blvd.
Mountain View, CA 94043           
(415) 967-5644

dkeisen@Gang-of-Four.Stanford.EDU (Dave Eisen) (12/22/90)

In article <1990Dec21.180826.24945@Neon.Stanford.EDU> dkeisen@Gang-of-Four.Stanford.EDU (Dave Eisen) writes:
>
>  int elapsed;
>
>  elapsed = (tm2.hour - tm.hour) * 3600 * 100 + (tm2.minute - .....
>

Ooops! I've been working on machines with 4 byte ints for too long.
That should be:

  
  long elapsed;

  elapsed = ....;



--
Dave Eisen                      	    dkeisen@Gang-of-Four.Stanford.EDU
1447 N. Shoreline Blvd.
Mountain View, CA 94043           
(415) 967-5644