Does C convert float to double internally ? 
Author Message
 Does C convert float to double internally ?

I was told that C compiler automatically converts "float" variable
internally to "double" for + - * / , etc operations. Is this true?

Can I force the C compiler not to do the conversion as I have an
assignment on finding out various computing errors of different
precisions?

My advice from the lecturer is to have temporary variable of
float to split a long equation to smaller step so that I can maximize
the impact of single precision number on the evaluation. Is this
feasible?

_/\/\/\/\/\___/\/\_________________________/\/\_________________________
_/\/\____/\/\__________/\/\/\/\___/\/\/\/\_/\/\_____/\/\/\_____/\/\/\/\_
_/\/\/\/\/\___/\/\___/\/\__/\/\_/\/\__/\/\_/\/\___/\/\/\/\/\_/\/\/\/\___
_/\/\____/\/\_/\/\_____/\/\/\/\___/\/\/\/\_/\/\___/\/\_____________/\/\_
_/\/\/\/\/\___/\/\/\_______/\/\_______/\/\_/\/\/\___/\/\/\/\_/\/\/\/\___
____________________/\/\/\/\___/\/\/\/\_________________________________





Sun, 20 Sep 1998 03:00:00 GMT  
 Does C convert float to double internally ?

Quote:

>I was told that C compiler automatically converts "float" variable
>internally to "double" for + - * / , etc operations. Is this true?
>Can I force the C compiler not to do the conversion as I have an
>assignment on finding out various computing errors of different
>precisions?
>My advice from the lecturer is to have temporary variable of
>float to split a long equation to smaller step so that I can maximize
>the impact of single precision number on the evaluation. Is this
>feasible?

If I were you I would write a quick set of routines that performs
arithmetic to a fixed number of decimal places by simulating rational
numbers with denominators of 100, or 1000 or 10000.

I tried once to give my students a project on numerical inaccuracy and
found that it was hard to make any sensible sized examples show really
bad behaviour, without limiting the number of decimal points to 2 or 3.

gordon
--

Visit http://www.cs.uwa.edu.au/~gordon
--



Sun, 20 Sep 1998 03:00:00 GMT  
 Does C convert float to double internally ?

 >I was told that C compiler automatically converts "float" variable
 >internally to "double" for + - * / , etc operations. Is this true?

ANSI allows the compiler to use float precision for operations between two
float operands. If you want the operation to be done in a higher precision, you
cast both operands to double.

float x, y, z;

        z   = (double) x / (double) y;

 >Can I force the C compiler not to do the conversion as I have an
 >assignment on finding out various computing errors of different
 >precisions?

I'm not sure about this one. I'd have to check whether the standard compliant
implementation _must_ use float precision. As far as I know, it is _free_ to
use the smaller precision to do the operation, but is not required to.

If the implementation insists on using double precision math internally (or
even something more precise---many floating point units will use extra
precision) there is little you can do.

 >My advice from the lecturer is to have temporary variable of
 >float to split a long equation to smaller step so that I can maximize
 >the impact of single precision number on the evaluation. Is this
 >feasible?

Sure, but it won't affect a particular operation like / if the compiler insists
on doing it in a high precision. Converting to float  will only truncate the
_result_ to a lower precision.
--



Sun, 20 Sep 1998 03:00:00 GMT  
 Does C convert float to double internally ?

Quote:

>I was told that C compiler automatically converts "float" variable
>internally to "double" for + - * / , etc operations. Is this true?

The original K&R said so, but since K&R2 (and ANSI C) compilers
are allowed to use single-precision-only calculations when both
operands and the result are floats.  It entirely depends on
your compiler and the hardware it's running on what the program
will do.

Quote:
>Can I force the C compiler not to do the conversion as I have an
>assignment on finding out various computing errors of different
>precisions?
>My advice from the lecturer is to have temporary variable of
>float to split a long equation to smaller step so that I can maximize
>the impact of single precision number on the evaluation. Is this
>feasible?

Yes, you can use a temporary to force some subexpression to
single precision.  When you store a value in a float, you
are guaranteed to get exactly single precision; this is often
a 32-bit IEEE754 value, but of course your compiler may choose
another representation.

Frank
--
The famous GIICM now on line:  http://www.xs4all.nl/~falstaff/GIICM.html
------------------------------------------------------------------------



Sun, 20 Sep 1998 03:00:00 GMT  
 Does C convert float to double internally ?
: I was told that C compiler automatically converts "float" variable
: internally to "double" for + - * / , etc operations. Is this true?

After I have read some books on C/C++, they all say that K&R C used to
do _implicit_ conversion from float to double both in unary and binary
operations, while ANSI C has dropped the implicit conversion and happily
allows float to be float at all time.

Can I safely assume now that ALL current C/C++ compliers are following ANSI C
standard?

:
: Can I force the C compiler not to do the conversion as I have an
: assignment on finding out various computing errors of different
: precisions?
:

I have read also the Turbo C++ manual and it says "tcc -ff- " command
line option can force the command line compiler to follow STRICT ANSI C
rule on conversion, i.e. no implicit conversion.

Can I now be sure that if I use "tcc -ff- program.c", there is _no_
implicit conversion? But what about the IDE interface?

Can someone suggest a simple program to test if there is any implicit
conversion happening behind the curtain?

: My advice from the lecturer is to have temporary variable of
: float to split a long equation to smaller step so that I can maximize
: the impact of single precision number on the evaluation. Is this
: feasible?
:

If I am to find the single precision value of say, 12.3457 to the power
of 13 by multiplying 12.3456 thir{*filter*} times, does that mean I have to
store the result of every _single_ multiplication to a temporary variable
of float to enforce the single precision?

What if I have a very complicated mathematical expression like Taylor's
series plus sqrt plus more ... ? How many temporary variables I need and
when is the best time or where is the best spot to insert such
_nuisance_?

--

_/\/\/\/\/\___/\/\_________________________/\/\_________________________
_/\/\____/\/\__________/\/\/\/\___/\/\/\/\_/\/\_____/\/\/\_____/\/\/\/\_
_/\/\/\/\/\___/\/\___/\/\__/\/\_/\/\__/\/\_/\/\___/\/\/\/\/\_/\/\/\/\___
_/\/\____/\/\_/\/\_____/\/\/\/\___/\/\/\/\_/\/\___/\/\_____________/\/\_
_/\/\/\/\/\___/\/\/\_______/\/\_______/\/\_/\/\/\___/\/\/\/\_/\/\/\/\___
____________________/\/\/\/\___/\/\/\/\_________________________________





Sun, 20 Sep 1998 03:00:00 GMT  
 Does C convert float to double internally ?


Quote:

>: I was told that C compiler automatically converts "float" variable
>: internally to "double" for + - * / , etc operations. Is this true?

>After I have read some books on C/C++, they all say that K&R C used to
>do _implicit_ conversion from float to double both in unary and binary
>operations, while ANSI C has dropped the implicit conversion and happily
>allows float to be float at all time.

ANSI allows either approach, at least in effect.

6.1.2.5

"The values of floating point operands and the results of floating point
 expressions may be represented in greater precision and range than that
 required by the type; the types are not changed thereby"

In fact the calculation can be performed in any representation greater
than the one in question. In this case, float, it could be double,
long double, or a representation that doesn't correspond to any C type.

What you are guaranteed is:

6.2.1.4

"When a float is promoted to double or long double, or a double is promoted
 to long double, its value is unchanged"

i.e. the conversion doesn't introduce inaccuracies. I guess that doesn't
necessarily apply in this case since strictly no type conversion is happening,
just a change of representation.

Assignment and casting are guaranteed to convert to the correct representation
for the target type (or at least act as if they have).

Quote:
>Can I safely assume now that ALL current C/C++ compliers are following ANSI C
>standard?

>:
>: Can I force the C compiler not to do the conversion as I have an
>: assignment on finding out various computing errors of different
>: precisions?
>:

>I have read also the Turbo C++ manual and it says "tcc -ff- " command
>line option can force the command line compiler to follow STRICT ANSI C
>rule on conversion, i.e. no implicit conversion.

>Can I now be sure that if I use "tcc -ff- program.c", there is _no_
>implicit conversion? But what about the IDE interface?

Often the floating point registers on a system hold more precision for
accuracy considerations. You probably can't stop them using it. However the
result must still be a float as far as the type system is concerned.

Quote:
>Can someone suggest a simple program to test if there is any implicit
>conversion happening behind the curtain?

#include <stdio.h>

...

    float a, b;

    printf("%lu\n", (unsigned long)sizeof(a+b));

If float and double have different representations on your system then
the chances are that they will have different sizes. For a conforming
implementation this must print out the same value as sizeof(float). There's
nop guarantee that the expression a+b won't use more bits of precision
internally though.

Quote:
>If I am to find the single precision value of say, 12.3457 to the power
>of 13 by multiplying 12.3456 thir{*filter*} times, does that mean I have to
>store the result of every _single_ multiplication to a temporary variable
>of float to enforce the single precision?

You could cast the result of each operator in the expression. This is an
area where I wouldn't trust the compiler much though

Quote:
>What if I have a very complicated mathematical expression like Taylor's
>series plus sqrt plus more ... ? How many temporary variables I need and
>when is the best time or where is the best spot to insert such
>_nuisance_?

Typically the extra precision is an advantage, not a disadvantage.

--
-----------------------------------------


-----------------------------------------



Mon, 21 Sep 1998 03:00:00 GMT  
 Does C convert float to double internally ?

Quote:

>: I was told that C compiler automatically converts "float" variable
>: internally to "double" for + - * / , etc operations. Is this true?

>After I have read some books on C/C++, they all say that K&R C used to
>do _implicit_ conversion from float to double both in unary and binary
>operations, while ANSI C has dropped the implicit conversion and happily
>allows float to be float at all time.

The keyword here is "allows".  ANSI C doesn't _require_ an operation
involving only float operands to be carried on in single precision.

Quote:
>Can I safely assume now that ALL current C/C++ compliers are following ANSI C
>standard?

Almost, but this doesn't solve your problem.  See above.

Quote:
>:
>: Can I force the C compiler not to do the conversion as I have an
>: assignment on finding out various computing errors of different
>: precisions?
>:
>I have read also the Turbo C++ manual and it says "tcc -ff- " command
>line option can force the command line compiler to follow STRICT ANSI C
>rule on conversion, i.e. no implicit conversion.

There is no such rule in ANSI C (that I know of).  See above.

Quote:
>Can I now be sure that if I use "tcc -ff- program.c", there is _no_
>implicit conversion? But what about the IDE interface?

Compiler specific questions do _not_ belong to this newsgroup.  We are
discussing the language, not one compiler or another.  Try a platform
specific newsgroup where that compiler is relevant (e.g.
comp.os.msdos.programmer).

Quote:
>Can someone suggest a simple program to test if there is any implicit
>conversion happening behind the curtain?

Such a program cannot be written in a portable way.  An implementation
where float and double are identical is perfectly legal, as long as the
minimal requirements for precision and range for the type double are met.

Quote:

>: My advice from the lecturer is to have temporary variable of
>: float to split a long equation to smaller step so that I can maximize
>: the impact of single precision number on the evaluation. Is this
>: feasible?

>If I am to find the single precision value of say, 12.3457 to the power
>of 13 by multiplying 12.3456 thir{*filter*} times, does that mean I have to
>store the result of every _single_ multiplication to a temporary variable
>of float to enforce the single precision?

Yes.

Quote:
>What if I have a very complicated mathematical expression like Taylor's
>series plus sqrt plus more ... ? How many temporary variables I need and
>when is the best time or where is the best spot to insert such
>_nuisance_?

As many as needed.  The idea is to assign the result of _any_ operation
to a float, to be sure that no calculation is done with more bits of
precision than you actually want/need.

For most intents and purposes, there is no need to force the precision
of the intermediary results to float.  The idea behind the current
definition of the language is to allow the implementor the flexibility
to choose the most efficient way to perform the arithmetic on a given
platform.  There are processors which have only double precision FPU's
and forcing single precision for the intermediary results would mean
a lot of conversions, which would waste precious cpu cycles.

Quote:
>_/\/\/\/\/\___/\/\_________________________/\/\_________________________
>_/\/\____/\/\__________/\/\/\/\___/\/\/\/\_/\/\_____/\/\/\_____/\/\/\/\_
>_/\/\/\/\/\___/\/\___/\/\__/\/\_/\/\__/\/\_/\/\___/\/\/\/\/\_/\/\/\/\___
>_/\/\____/\/\_/\/\_____/\/\/\/\___/\/\/\/\_/\/\___/\/\_____________/\/\_
>_/\/\/\/\/\___/\/\/\_______/\/\_______/\/\_/\/\/\___/\/\/\/\_/\/\/\/\___
>____________________/\/\/\/\___/\/\/\/\_________________________________




Please trim your signature and retain only the last two lines.  The others
don't carry any bit of useful information and are a pure waste of
bandwidth.

Dan
--
Dan Pop
CERN, CN Division

Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland



Mon, 21 Sep 1998 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. floats internally represented as doubles?

2. Convert a String to float/double

3. Convert double to D-float format

4. Converting double to float

5. Need function to convert float/double/long to string using a specified format

6. converting int to float (or double)

7. float warning says converting from double

8. Huge rounding errors when converting floats to doubles

9. RE : Converting C floats/doubles to Pascal reals.

10. Precision problem when converting float to double

11. Converting CString to float, double

12. epsilon for float, double long double

 

 
Powered by phpBB® Forum Software