473,698 Members | 2,186 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

question of double/ float

Hello,

consider a function:

double some_func(doubl e 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 1578
In article <11************ **********@e51g 2000hsg.googleg roups.com>,
Joakim Hove <jo*********@gm ail.comwrote:
>consider a function:

double some_func(doubl e 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
--
"Considerat ion 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************ **********@o5g2 000hsb.googlegr oups.com>,
Joakim Hove <jo*********@gm ail.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
--
"Considerat ion 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...@gm ail.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...@gm ail.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_Keit h) 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
1856
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. I'm told: "floats and doubles have bits for mantissa and exponent. (I knew that). If you are subtract bits that partly belong to mantissa and partly belong to exponent, that hardly makes sense. Anyway, looking on bits only, a float has 32...
5
6401
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 precise than I had expected:
3
1533
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 objects etc as Business layer classes and then have an data access layer which actually save / updates / delete to and from the DB.. If so, and bearing in mind the 'easiest' way to pass data between the layers would be with a dataset - should my BL...
6
2291
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 (*f)(float)" But I want to be able to pass a
3
1765
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 is positioned on the right of text1? Because the CSS2.1 said: A floating element must be placed as high as possible; A left-floating element must be put as far to the left as possible; A
1
1396
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 int NumToAdd = m_iPopSize - NewPop.size();
11
1979
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 lines of STORE_LOWWORD(storage, value) GET_LOWWORD(storage) /* returns value */
12
4025
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 ---------------------------------------------------------------------------------------- float fun(float value) { float f1 =0., f2 = value; float tol = value/1000.; float result,tmp;
32
2376
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" variable, which of the following lines are correct:
22
2763
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 cropped up before, since I rarely use "float"s for anything, and hardly ever use the function for floating-point numbers in the first place; I just was messing around testing it for all cases and noticed a problem.) Anyway, it is declared and I...
0
8674
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9157
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9028
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8861
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7728
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6518
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3046
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.