question of double/ float

J

Joakim Hove

Hello,

consider a function:

double some_func(double arg) {
/*

*/
}

When this function is called with a float argument, and the result
assigned to a float variable like this:

float float_arg;
float float_result;

float_result = some_func(float_arg);

Are the necessary conversions guaranteed tok place automatically? And
if this is autoamtically taken care of, why does the standard (C99)
require functions like logf and sinf. Is it just to gain speed by
avoiding conversions, and possible using a 'cruder' algorithm?

Regards

Joakim
 
R

Richard Tobin

Joakim Hove said:
consider a function:

double some_func(double arg) {
/*

*/
}

When this function is called with a float argument, and the result
assigned to a float variable like this:

float float_arg;
float float_result;

float_result = some_func(float_arg);

Are the necessary conversions guaranteed tok place automatically?

Yes. The prototype tells the compiler the required types, and it
does the conversion. You could even use ints.
And
if this is autoamtically taken care of, why does the standard (C99)
require functions like logf and sinf. Is it just to gain speed by
avoiding conversions, and possible using a 'cruder' algorithm?

Yes. Many processors have built-in instructions for these operations
in a variety of sizes.

-- Richard
 
J

Joakim Hove

Thanks for answering :)
Yes. The prototype tells the compiler the required types, and it
does the conversion.

But, the compiler can only do this in terms of literals, otherwise it
must insert a call to a double -> float conversion function?
Yes. Many processors have built-in instructions for these operations
in a variety of sizes.

Good - then I will stick to one function implementation for both float
and double.

Joakim
 
R

Richard Tobin

Yes. The prototype tells the compiler the required types, and it
does the conversion.
[/QUOTE]
But, the compiler can only do this in terms of literals, otherwise it
must insert a call to a double -> float conversion function?

Yes, I didn't mean "do the conversion at compile time", I meant "arrange
for the conversion to be done".

-- Richard
 
C

Chris Dollin

Joakim said:
Thanks for answering :)


But, the compiler can only do this in terms of literals,

No. The compiler can -- must -- arrange for the conversion of
any legal value to the required type.
otherwise it
must insert a call to a double -> float conversion function?

No.

There's no "must". If there's an instruction to hand, it can -- one
might say should -- use that.
Good - then I will stick to one function implementation for both float
and double.

Was your concern correctness or performance?
 
R

Rg

[...]

float float_arg;
float float_result;

float_result = some_func(float_arg);

Are the necessary conversions guaranteed tok place automatically? [...]

Yes, but keep in mind that conversions from "big" types to "smaller"
types (such as double to float) may incur in loss of precision or of
significance.
 
K

Keith Thompson

Rg said:
[...]

float float_arg;
float float_result;

float_result = some_func(float_arg);

Are the necessary conversions guaranteed tok place automatically? [...]

Yes, but keep in mind that conversions from "big" types to "smaller"
types (such as double to float) may incur in loss of precision or of
significance.

Such a conversion can lose precision, *or* it can overflow if the
double value is too big to be represented as a float. In that case,
the behavior is undefined.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,828
Latest member
LauraCastr

Latest Threads

Top