473,395 Members | 1,537 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,395 software developers and data experts.

conversion warning with STL combination of functors


Hello,

I wrote the following code to wrap some existing C functions, and I get
some warning about converting float to int.

float zfactor;
int tmp_x[4], corners_x[4];

std::transform(tmp_x,tmp_x+4,corners_x, bind2nd( multiplies<float>(),zfactor) );
I know that I can solve the problem by writing a dedicated functor, but
I'd like to know if it exists a proper solution to do it in just one line
as above. I have to precise that I don't want to use SGI extensions such
as compose1, I look for a standard STL form.

Any ideas will be appreciated !

Regards,
Gilles Rochefort



Dec 28 '05 #1
6 2651
Gilles Rochefort wrote:
Hello,

I wrote the following code to wrap some existing C functions, and I get
some warning about converting float to int.

float zfactor;
int tmp_x[4], corners_x[4];

std::transform(tmp_x,tmp_x+4,corners_x, bind2nd( multiplies<float>(),zfactor) );
I know that I can solve the problem by writing a dedicated functor, but
I'd like to know if it exists a proper solution to do it in just one line
as above. I have to precise that I don't want to use SGI extensions such
as compose1, I look for a standard STL form.


The code coverts a float to an int, so the warning is, technically,
correct. Turn it off or ignore it if the code is, in fact, correct.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Dec 28 '05 #2
"Gilles Rochefort" <gi**************@online.fr> wrote in message
news:pa***************************@online.fr...

Hello,

I wrote the following code to wrap some existing C functions, and I get
some warning about converting float to int.

float zfactor;
int tmp_x[4], corners_x[4];

std::transform(tmp_x,tmp_x+4,corners_x, bind2nd(
multiplies<float>(),zfactor) );
I know that I can solve the problem by writing a dedicated functor, but
I'd like to know if it exists a proper solution to do it in just one line
as above. I have to precise that I don't want to use SGI extensions such
as compose1, I look for a standard STL form.

Any ideas will be appreciated !

Regards,
Gilles Rochefort


Well, it looks like you are multiplying a float by an int. So the warning
is, in fact, correct because of the conversion involved.

Multiplying a float by an int can get complicated because you either need to
convert the float to an int first, or convert the int to a float. In most
cases you would want to convert the int to a float. The reason being:
float x = 0.25;
int y = 4;

Now, mulitplying x times y can produce either 0 or 1 depending on which
conversion is done.

I think you need to look at what you are doing and rethink the math.

Jan 2 '06 #3
Jim Langston wrote:

Multiplying a float by an int can get complicated because you either need to
convert the float to an int first, or convert the int to a float. In most
cases you would want to convert the int to a float. The reason being:
float x = 0.25;
int y = 4;

Now, mulitplying x times y can produce either 0 or 1 depending on which
conversion is done.


Given those definitions, the expression x*y has type double. Both
operands are converted to double and then multiplied.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jan 2 '06 #4

Pete Becker wrote:
Jim Langston wrote:

Multiplying a float by an int can get complicated because you either need to
convert the float to an int first, or convert the int to a float. In most
cases you would want to convert the int to a float. The reason being:
float x = 0.25;
int y = 4;

Now, mulitplying x times y can produce either 0 or 1 depending on which
conversion is done.


Given those definitions, the expression x*y has type double. Both
operands are converted to double and then multiplied.


Do they need to? float * int could return double but that doesn't mean
that the integer has to be converted first. In fact I would hope that
when multiplying by a constant 4, the compiler would be clever enough
to simply add 2 to the exponent. Even if it's a variable y I would hope
the compiler might be able to perform an optimal float*int (presumably
more efficient than float * float ).

Jan 3 '06 #5
In article <VZ******************************@giganews.com>,
Pete Becker <pe********@acm.org> wrote:
float x = 0.25;
int y = 4;

Given those definitions, the expression x*y has type double. Both
operands are converted to double and then multiplied.


Okay, I'm baffled. I would have expected a conversion to float.
Section 5/9 of the standard suggests the same as I understand it.
What am I missing?

quoting the standard:
Many binary operators that expect operands of arithmetic or enumeration
type cause conversions and yield result types in a similar way. The purpose
is to yield a common type, which is also the type of the result. This
pattern is called the usual arithmetic conversions, which are defined as
follows:
- If either operand is of type long double, the other shall be converted to
long double.
- Otherwise, if either operand is double, the other shall be converted to
double.
- Otherwise, if either operand is float, the other shall be converted to
float.
- Otherwise, the integral promotions (4.5) shall be performed on both
operands.54)
- Then, if either operand is unsigned long the other shall be converted to
unsigned long.
- Otherwise, if one operand is a long int and the other unsigned int, then if
a long int can represent all the values of an unsigned int, the unsigned int
shall be converted to a long int; otherwise both operands shall be converted
to unsigned long int.
- Otherwise, if either operand is long, the other shall be converted to long.
- Otherwise, if either operand is unsigned, the other shall be converted to
unsigned.
--
Mark Ping
em****@soda.CSUA.Berkeley.EDU
Jan 7 '06 #6
E. Mark Ping wrote:
In article <VZ******************************@giganews.com>,
Pete Becker <pe********@acm.org> wrote:
float x = 0.25;
int y = 4;


Given those definitions, the expression x*y has type double. Both
operands are converted to double and then multiplied.

Okay, I'm baffled. I would have expected a conversion to float.
Section 5/9 of the standard suggests the same as I understand it.
What am I missing?


Nothing. The type of the x*y is, indeed, float.

For some reason I thought a floating-point was performed. But 5.6/2
(Multiplicative operators) says "... The usual arithmetic conversions
are performed on the operands and determine the type of the result."
You've given the details of the usual arithmetic conversions, and
there's no floating-point conversion there.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jan 7 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: red floyd | last post by:
Disclaimer: VS.NET 2003 (haven't checked any other compiler). I'm writing functors for my classes. Because the objects in my containers are large, I'm making my functors take const T& parameters....
2
by: nsgi_2004 | last post by:
Hi, I have been learning about functors and at first everything was clear. Make a class and overload operator () so that an object of the functor can be thought of as a function. However, I...
16
by: jose_luis_fdez_diaz_news | last post by:
Hi, If I don't include <libgen.h> I get then warnig below in regcmp call: warning: improper pointer/integer combination: op "=" but if I include it the warning is not shown, but them program...
0
by: cody | last post by:
I propose that if an enum has FlagsAttribute set and its member have not they values explicitly assigned I'd propose that the compiler should emit a warning. Also if fields are not power of two (or...
3
by: Steve Richter | last post by:
here is a warning I am getting in a C++ .NET compile: c:\SrNet\jury\JuryTest.cpp(55) : warning C4927: illegal conversion; more than one user-defined conversion has been implicitly applied while...
4
by: tryptik | last post by:
Hello all, I have a question about iterators. I have a container of functors that operate on an std::string. The functors go something like this: class Functor { std::string...
2
by: Jon Slaughter | last post by:
I'm trying to mess with functors and the way I want it to work is that when I create a functor it will automatically add itself to an array. The attached code demonstrates what I mean. The...
4
by: Christopher | last post by:
I used to just use a plain old function pointer is a call to std::sort. My colleagues are telling me that I need to use a "functor". Ok, I google and see that a functor is a class with a public...
9
by: Eric | last post by:
I am working on a large, old code base and attempting to move it to GCC 4.2. Throughout the code, there is stuff like: char *aVar = "aString"; or void aFunc( char *aVar) { ... } aFunc(...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.