473,320 Members | 1,853 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

question of double/ float

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
6 1560
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: ma740988 | last post by:
I've got an unpacker that unpacks a 32 bit word into 3-10 bits samples. Bits 0 and 1 are dont cares. For the purposes of perfoming an FFT and an inverse FFT, I cast the 10 bit values into doubles....
5
by: jchludzinski | last post by:
I'm using strtok() to parse thru a line and read different numbers: float value; char *token; token = strtok( line, " " ); .... sscanf( token, "%f", &value ); These results are less...
3
by: Ken H | last post by:
Hi I have a question about architecting solutions.. I have a part of a project which requires me to track person details (name, addresses, etc... Should I be creating Person objects, Address...
6
by: Martin Bootsma | last post by:
I have a C question, which looks very easy, but no one here seems to know an easy answer. I have a function "powell" (from Numerical Recipes) which takes an argument of the type "double...
3
by: hantechs | last post by:
<html> <body> <p style="width:30%;">text1</p> <p style="float:left;">text2</p> </body> </html> The effect of this html code is : text1 and text2 each is on a line. My question is: Why text2...
1
by: liuhaoran | last post by:
HI. i have a question about memory error. when i change double variable to float variable ,for example: int curGen = 0; double sum = 0; // m_iPopSize is int variable ,NewPop is a vector...
11
by: Bartholomew Simpson | last post by:
I need to store two float values (each of which requires less 16 bit storage), in a double data type. Can anyone give me some macros to retrive/store values in the double ? something along the...
12
by: kostas | last post by:
Hi I was asked to propose an interview question for C/C++ programmers (CAD/CAE software) I came up with the following...
32
by: alex.j.k2 | last post by:
Hello all, I have "PRECISION" defined in the preprocessor code and it could be int, float or double, but I do not know in the code what it is. Now if I want to assign zero to a "PRECISION"...
22
by: Bill Reid | last post by:
I just noticed that my "improved" version of sscanf() doesn't assign floating point numbers properly if the variable assigned to is declared as a "float" rather than a "double". (This never...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.