typecast float 
Author Message
 typecast float

Hi, For arithmetic involving float, we were told to declare user input
as a  "int" datatype when possible.

When I tried it with this program, it didn't work.
I got a message saying program ended incorrectly.

I tested this program many times and get correct results.
I just want to confirm this is correct.

    void fahrenheit(void)
{

    float fahrenheit,total;

    printf("Temperature?   ");
    scanf("%f", &fahrenheit);
    total = (fahrenheit - 32) * (float) 5 / (float) 9;
    printf("Celsius conversion is:  %.2f\n\n", total);

Sent via Deja.com http://www.*-*-*.com/
Before you buy.



Sat, 24 Aug 2002 03:00:00 GMT  
 typecast float

Hi, For arithmetic involving float, we were told to declare user input
as a  "int" datatype when possible.

When I tried it with this program, it didn't work.
I got a message saying program ended incorrectly.

I tested this program many times and get correct results.
I just want to confirm this is correct.

    void fahrenheit(void)
{

    float fahrenheit,total;

    printf("Temperature?   ");
    scanf("%f", &fahrenheit);
    total = (fahrenheit - 32) * (float) 5 / (float) 9;
    printf("Celsius conversion is:  %.2f\n\n", total);

Sent via Deja.com http://www.deja.com/
Before you buy.



Sat, 24 Aug 2002 03:00:00 GMT  
 typecast float


Quote:

> Hi, For arithmetic involving float, we were told to declare user input
> as a  "int" datatype when possible.

> When I tried it with this program, it didn't work.
> I got a message saying program ended incorrectly.

> I tested this program many times and get correct results.
> I just want to confirm this is correct.

>     void fahrenheit(void)
> {

>     float fahrenheit,total;

>     printf("Temperature?   ");
>     scanf("%f", &fahrenheit);
>     total = (fahrenheit - 32) * (float) 5 / (float) 9;
>     printf("Celsius conversion is:  %.2f\n\n", total);

> Sent via Deja.com http://www.deja.com/
> Before you buy.

Thank you.

Bill

Sent via Deja.com http://www.deja.com/
Before you buy.



Sat, 24 Aug 2002 03:00:00 GMT  
 typecast float
First of all, I apologize for this repetative post. The previous
posts had too many spaces before message started so I was afraid
post would look "empty".

For arithmetic involving float, we were told to declare user input as a
 "int" datatype when possible.

 When I tried it with this program, it didn't work.
 I got a message saying program ended incorrectly.

 I tested this program many times and get correct results.
 I just want to confirm this is correct.

     void fahrenheit(void)
 {

     float fahrenheit,total;

     printf("Temperature?  ");
     scanf("%f", &fahrenheit);
     total = (fahrenheit - 32) * (float) 5 / (float) 9;
     printf("Celsius conversion is:  %.2f\n\n", total);

 Sent via Deja.com http://www.deja.com/
 Before you buy.

Sent via Deja.com http://www.deja.com/
Before you buy.



Sat, 24 Aug 2002 03:00:00 GMT  
 typecast float


Quote:
> First of all, I apologize for this repetative post. The previous
> posts had too many spaces before message started so I was afraid
> post would look "empty".

> For arithmetic involving float, we were told to declare user input as
a
>  "int" datatype when possible.

>  When I tried it with this program, it didn't work.
>  I got a message saying program ended incorrectly.

>  I tested this program many times and get correct results.
>  I just want to confirm this is correct.

>      void fahrenheit(void)
>  {

>      float fahrenheit,total;

>      printf("Temperature?  ");
>      scanf("%f", &fahrenheit);
>      total = (fahrenheit - 32) * (float) 5 / (float) 9;
>      printf("Celsius conversion is:  %.2f\n\n", total);

>  Sent via Deja.com http://www.deja.com/
>  Before you buy.

> Sent via Deja.com http://www.deja.com/
> Before you buy.

You can use scanf() and scan for a float, however, it's probably best
to use gets() or fgets() and then use sscanf().  This will allow you to
error-check your input.  You should atleast check the return value of
scanf(), it will return the number or successful assignments.

Sent via Deja.com http://www.deja.com/
Before you buy.



Sat, 24 Aug 2002 03:00:00 GMT  
 typecast float

Quote:

>  I tested this program many times and get correct results.
>  I just want to confirm this is correct.

>      void fahrenheit(void)
>  {

>      float fahrenheit,total;

>      printf("Temperature?  ");
>      scanf("%f", &fahrenheit);
>      total = (fahrenheit - 32) * (float) 5 / (float) 9;
>      printf("Celsius conversion is:  %.2f\n\n", total);

Yes. Although

        total = (fahrenheit-32)*5/9;

would have the same effect.

Regards
Horst



Sat, 24 Aug 2002 03:00:00 GMT  
 typecast float

Quote:

> You can use scanf() and scan for a float, however, it's probably best
> to use gets() or fgets() and then use sscanf().

_Never_ use gets() under any circumstances. Use fgets() instead. Then,
you could use sscanf(); in simple cases, strtod() might be preferable.

Richard



Sat, 24 Aug 2002 03:00:00 GMT  
 typecast float

Quote:


[...]
> >      float fahrenheit,total;
[...]
> >      total = (fahrenheit - 32) * (float) 5 / (float) 9;
[...]
> Yes. Although

>    total = (fahrenheit-32)*5/9;

> would have the same effect.

Yes, but the reasons for this are a bit obscure.  The way this
particular expression is organized, the fact that fahrenheit is
declared as float happens to propate through the rest of the
expression.  If instead you wrote

    total = (fahrenheit-32)*(5/9);

the 5/9 would be a separate subexpression that would evaluate to 0,
since both operands are of type int.

A better (i.e., clearer) way to write this would be:

    total = (fahrenheit - 32.0) * 5.0 / 9.0;

A minor quibble about this: floating-point constants (32.0, 5.0, and
9.0) are really of type double, not float; if you really need them to
be float, you can use 32.0f, 5.0f, and 9.0f.  In this case, it
shouldn't make much difference (constants are evaluated at compilation
time) -- and it probably makes more sense to declare fahrenheit and
total as double anyway.

--

San Diego Supercomputer Center           <*>  <http://www.sdsc.edu/~kst>
Welcome to the last year of the 20th century.



Sat, 24 Aug 2002 03:00:00 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Help - errors on typecasting float to unsigned long

2. Help - errors on typecasting float to unsigned long

3. Typecast float to string

4. float != float and floats as return types

5. Array typecasting!

6. Typecast returns null

7. function pointer typecasting

8. Pointer typecasts

9. TypeCast help

10. Typecasting NULL?

11. Address typecasting

12. Attribute Values / Typecasting

 

 
Powered by phpBB® Forum Software