when does operator promotion happen? Is it expensive?

K

KOFKS

I'm trying to do some image processing work, and the result is
floating point number. Using "float" type will comsume less memory,
while lots of functions in math.h and some other libs return a
"double" type. So I want to know whether the operator promotion from
"float" to "double", "int" to "float“ or "int" to "double" cause
efficiency loss. When does operator promotion happen? preprocessing,
compiling or running?
I try to get information from FAQ or searching here, but get
inadequate explaination.
May someone help?
TIA~
 
J

James Kuyper

KOFKS said:
I'm trying to do some image processing work, and the result is
floating point number. Using "float" type will comsume less memory,
while lots of functions in math.h and some other libs return a
"double" type. So I want to know whether the operator promotion from
"float" to "double", "int" to "float“ or "int" to "double" cause
efficiency loss. When does operator promotion happen? preprocessing,
compiling or running?
I try to get information from FAQ or searching here, but get
inadequate explaination.

That's probably the case because "operator promotion" is a meaningless
phrase, so you won't be able to find it any FAQ. My best guess is that
you are asking about type conversions, and the following answer is based
upon that guess:

All type conversions that actually do anything take a certain amount of
time, and therefore cost you some efficiency. An example of a type
conversion that don't actually do anything would be conversion from int
to long, on a platform where int and long have the same representation.
However, those costs are usually negligible. It's usually far more
important to consider other issues, such as the amount of space required
by the new type, the amount of precision it provides, the range of
values it can represent, and the speed with which operations in that
type are executed.

Type conversion can occur during preprocessing, but the only place it
can occur is in #if conditions, where the only possible types are
intmax_t, and uintmax_t, so the only possible conversion are between
those two types. Furthermore, since casts are not permitted, the only
conversions that occur are implicit.

The standard does not impose any rigid distinctions between what can be
done at compile time and what can be done at run time. It talks about
"translation" of a program and "execution" of a program, but gives
implementors a lot of freedom about how they actually implement those
processes.

Constant expressions (including those which involve type conversions)
can be evaluated at compile time, and usually are. The main exception is
the evaluation of floating point expressions, which many compilers will
defer until runtime, particularly if compiled on a machine which might
have a different FPU than the one where the code will be running.
However, unless it occurs in context where a integer constant expression
is required, any constant expression can have it's evaluation deferred
until run time.

Evaluation of non-constant expressions (including those that involve
type conversion) can usually only occur at runtime, because they depend
upon inputs provided to the program which are not known at compile time.
However, that is not always an issue. If, for instance, you write a
function that takes no input, calculates the value of pi using a series
expansion, and returns that value, a compiler is permitted to, in
effect, execute that function at compile time, determine the value it
would return, and replace all calls to that function with the value it
would have returned. Any type conversions that occurred in such code
would occur at compile time.
 
K

KOFKS

That's probably the case because "operator promotion" is a meaningless
phrase, so you won't be able to find it any FAQ.
I choose this phrase by exact quotation from a copy of C-FAQ at the
year 1995(Maybe too old). In II.12 of this FAQ the title is "What is
operator promotion?", while at the explanation part of II.11(Are there
any problems with performing mathematical operations on different
variable types) , there is the definition of operator promotion of
this FAQ: "The process of automatic type conversion is called
'operator promotion.'"

Thanks a lot for your detailed explanation.
And as for that, I want to know how type conversion is done. Is it
some function like code block not revealed to programmer inserted to
the complied code or being called at running time?
 
J

James Kuyper

KOFKS said:
I choose this phrase by exact quotation from a copy of C-FAQ at the
year 1995(Maybe too old). In II.12 of this FAQ the title is "What is
operator promotion?", while at the explanation part of II.11(Are there
any problems with performing mathematical operations on different
variable types) , there is the definition of operator promotion of
this FAQ: "The process of automatic type conversion is called
'operator promotion.'"

Using Google, I found one document on Google matching your description:

ftp://220.113.41.171/mirror/byr/Documents/Programming/C.C++.C%23/Sams%20-%20C%20Programming%20-%20Just%20the%20FAQS/02%20-%20Variables%20and%20Data%20Storage.pdf

It describes what the C standard calls the "usual arithmetic conversions".
Thanks a lot for your detailed explanation.
And as for that, I want to know how type conversion is done. Is it
some function like code block not revealed to programmer inserted to
the complied code or being called at running time?

That depends upon the type conversion being performed, and the
implementation of C being used. There's few general statements that can
be made about it, that have any useful content. It could be implemented
by a function call, but it would be more common to implement the
conversions with in-line code, unless the conversion is a rather
complicated one (such as conversions involving complex numbers, or
conversions involving int64_t on a machine with no direct hardware
support for types larger than 16 bits).

If you're planning to write a compiler or link C code to assembly code,
or are just plain curious, you'll need to learn a lot of details about
the specific platform you're targeting; the answers are off-topic in
this newsgroup, you'll get better answers in a group specific to that
platform; and you won't be able to make any use of those answers when
porting your code to a substantially different platform.

If you aren't planning to write your own compiler or link in assembly
code, all you really need to know about type conversion is when it
occurs, and what the result will be.
 
K

Keith Thompson

KOFKS said:
I choose this phrase by exact quotation from a copy of C-FAQ at the
year 1995(Maybe too old). In II.12 of this FAQ the title is "What is
operator promotion?", while at the explanation part of II.11(Are there
any problems with performing mathematical operations on different
variable types) , there is the definition of operator promotion of
this FAQ: "The process of automatic type conversion is called
'operator promotion.'"

Where did you find this?

The "official" comp.lang.c FAQ is at <http://www.c-faq.com/>. Neither
of those questions appears in the current version, and it doesn't use
Roman numerals for it sections.
 
G

Gene

I'm trying to do some image processing work, and the result is
floating point number. Using "float" type will comsume less memory,
while lots of functions in math.h and some other libs return a
"double" type. So I want to know whether the operator promotion from
"float" to "double", "int" to "float“ or "int" to "double" cause
efficiency loss.  When does operator promotion happen? preprocessing,
compiling or running?
I try to get information from FAQ or searching here, but get
inadequate explaination.
May someone help?
TIA~

The right thing to do is write your code using typedefs and macros so
that you can easily recompile with both float and double and go for
the one that gives you the best performance.
 
K

KOFKS

The right thing to do is write your code using typedefs and macros so
that you can easily recompile with both float and double and go for
the one that gives you the best performance.

That's a good suggestion, for I just worry about the performance
besides little curiosity. By simple testing, I will get to know how to
make a choise. As the comprehensive explanation is not within the
scope of C, I think maybe it is better to read more before asking this
question here any more.

Thanks to all for paying attention and spending time offering good
suggestions.

I have downloaded a local version of C-FAQ from <http://www.c-faq.com/
, and I think it will help me a lot to use standard phrase for better
expression. Thanks to James Kuyper and Keith Thompson.
 
G

Guest

I choose this phrase by exact quotation from a copy of C-FAQ at the
year 1995(Maybe too old).

it's not that old...
I use this one http://c-faq.com/
In II.12 of this FAQ

my version doesn't use roman numerals. Do you mean 2.12 or 11.12?
(neither seems to match what you've got)
the title is "What is operator promotion?",

I searched the entire FAQ for the word "promotion" and failed to
find your text.

while at the explanation part of II.11(Are there
any problems with performing mathematical operations on different
variable types) , there is the definition of operator promotion of
this FAQ: "The process of automatic type conversion is called
'operator promotion.'"

I searched for "automatic" and failed to find your text
 
K

KOFKS

my version doesn't use roman numerals. Do you mean 2.12 or 11.12?
(neither seems to match what you've got)


I searched the entire FAQ for the word "promotion" and failed to
find your text.

Refer to James Kuyper's second post, if you really want to know the
document what I quote.
It's from here
ftp://220.113.41.171/mirror/byr/Documents/Programming/C.C++.C%23/Sams%20-%20C%20Programming%20-%20Just%20the%20FAQS/02%20-%20Variables%20and%20Data%20Storage.pdf
 
K

KOFKS

...
my version doesn't use roman numerals. Do you mean 2.12 or 11.12?
(neither seems to match what you've got)


I searched the entire FAQ for the word "promotion" and failed to
find your text.

Refer to James Kuyper's second post, if you really want to know the
document that I quote.
It can be got from here
ftp://220.113.41.171/mirror/byr/Documents/Programming/C.C++.C%23/Sams%20-%2=
0C%20Programming%20-%20Just%20the%20FAQS/02%20-%20Variables%20and
%20Data%20=
Storage.pdf
 

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
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top