By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,310 Members | 1,944 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,310 IT Pros & Developers. It's quick & easy.

question of double/ float

P: n/a
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

May 15 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
In article <11**********************@e51g2000hsg.googlegroups .com>,
Joakim Hove <jo*********@gmail.comwrote:
>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
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
May 15 '07 #2

P: n/a
Thanks for answering :-)
Are the necessary conversions guaranteed tok place automatically?

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

May 15 '07 #3

P: n/a
In article <11**********************@o5g2000hsb.googlegroups. com>,
Joakim Hove <jo*********@gmail.comwrote:
>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, I didn't mean "do the conversion at compile time", I meant "arrange
for the conversion to be done".

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
May 15 '07 #4

P: n/a
Joakim Hove wrote:
Thanks for answering :-)
>Are the necessary conversions guaranteed tok place automatically?

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,
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.
>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.
Was your concern correctness or performance?

--
"The path to the web becomes deeper and wider." - October Project

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England

May 15 '07 #5

P: n/a
Rg
On 15 maio, 07:11, Joakim Hove <joakim.h...@gmail.comwrote:
>
[...]

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.

-----
rg

May 15 '07 #6

P: n/a
Rg <rg*****@gmail.comwrites:
On 15 maio, 07:11, Joakim Hove <joakim.h...@gmail.comwrote:
>[...]

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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
May 15 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.