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,c orners_x, bind2nd( multiplies<floa t>(),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 6 2669
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,c orners_x, bind2nd( multiplies<floa t>(),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)
"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,c orners_x, bind2nd( multiplies<floa t>(),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.
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)
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 ).
In article <VZ************ *************** ***@giganews.co m>,
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.CSU A.Berkeley.EDU
E. Mark Ping wrote: In article <VZ************ *************** ***@giganews.co m>, 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) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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.
If I try std::bind2nd() with one of these classes, I get an error about a reference to a reference.
What's the proper way to declare functors taking const T& parameters so that they play nicely with adapters?
Would it be something like the...
|
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 have seen in Effective STL, Scott passes normal functions into
places that expect functors. I'm not sure how this works, as I thought
functors had to be classes.
|
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 compiles
fine.
What is the warning shown ?
|
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 a combination of other
fields).
public enum ABCD
{
A, // warning, not initialized
B = 1, // ok
|
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 calling the constructor 'MyString::MyString(const
wchar_t *)'
c:\SrNet\jury\JuryTest.h(21) : see declaration of
'MyString::MyString'
The class "StringData" uses a, whatever you call it, operator const
| |
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 operator()(const std::string& s) {/*manipulate
string*/; return newString;}
};
|
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 problem is that its a bit
unsatisfactory and I'd like to improve it.
In the constructor of TClassA
Functors<TClassA, std::string*Functor = new Functors<TClassA,
std::string>(this, &TClassA::Display);
|
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 method,
operator () that does the comparison. Fine, no problem. What they fail
to tell me is, what is the advantage to wrapping some comparison
function in a class and calling it operator()?
I also read that "functors" are supposed to be a...
|
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( "aString" );
|
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...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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...
|
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...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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...
|
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();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |