473,804 Members | 2,225 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

conversion warning

Hi

Recently I've made a stupid bug. Here is a snippet:

long long a, b;
....
int c = min(a, b);

But this bug could have been prevented if g++ would warn me about loss
of precision. Do you know any flag that can turn on this kind of
warning? I tried -Wconversion, but I believe it's working only on
float <-double conversion.

Alexandru

Sep 8 '07 #1
3 1661
Alexandru Mosoi wrote:
Recently I've made a stupid bug. Here is a snippet:

long long a, b;
...
int c = min(a, b);
Note that C++ does not have 'long long' type.
But this bug could have been prevented if g++ would warn me about loss
of precision. Do you know any flag that can turn on this kind of
warning? I tried -Wconversion, but I believe it's working only on
float <-double conversion.
If you need an answer to a G++ specific question, consider posting
to the G++ specific newsgroup, please.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 8 '07 #2

Alexandru Mosoi wrote in message...
Hi
Recently I've made a stupid bug. Here is a snippet:

long long a, b;
...
int c = min(a, b);

But this bug could have been prevented if g++ would warn me about loss
of precision. Do you know any flag that can turn on this kind of
warning? I tried -Wconversion, but I believe it's working only on
float <-double conversion.
Alexandru
Why not just check it:

{
long long a, b;
...
if( min(a, b) long long( std::numeric_li mits<int>::max( ) ) ){
throw std::runtime_er ror(" result too large for type int.");
}
int c = min(a, b);
}

Or, truncate it:

int c = ( min(a, b) & 0x7fffffff ); // assuming sizeof int == 4

Check if it was truncated:

if( long long( c ) != min(a, b) ){
std::cout<<"Tru ncated"<<'\n';
}

For compiler questions, you need to ask on an NG for your compiler.
For GCC g++, gnu.g++.help
I think if you just turn on warnings (-Wall -W), it will tell you of
incorrect assignments. ( I've got a big project in progress, or I'd test
that out.)

Also:
-Wlong-long
Warn if long long type is used. This is default. To inhibit the warning
messages, use -Wno-long-long. Flags -Wlong-long and -Wno-long-long are taken
into account only when -pedantic flag is used.

--
Bob R
POVrookie
Sep 8 '07 #3
On Sep 9, 12:08 am, "BobR" <removeBadB...@ worldnet.att.ne twrote:
Alexandru Mosoi wrote in message...
Hi
Recently I've made a stupid bug. Here is a snippet:
long long a, b;
...
int c = min(a, b);
But this bug could have been prevented if g++ would warn me about loss
of precision. Do you know any flag that can turn on this kind of
warning? I tried -Wconversion, but I believe it's working only on
float <-double conversion.
Alexandru

Why not just check it:

{
long long a, b;...

if( min(a, b) long long( std::numeric_li mits<int>::max( ) ) ){
throw std::runtime_er ror(" result too large for type int.");
}
int c = min(a, b);

}

Or, truncate it:

int c = ( min(a, b) & 0x7fffffff ); // assuming sizeof int == 4

Check if it was truncated:

if( long long( c ) != min(a, b) ){
std::cout<<"Tru ncated"<<'\n';
}
None of you solution is good for me because I accidentally wrote _int
c_ instead of _long long c_. However I would have avoided the mistake
if compiler was a little bit clever. I've done the bug in a
programming contest where you don't really care about portability and
stuff like that. If it compiles than it perfect. However little
warnings are more than welcome.
>
For compiler questions, you need to ask on an NG for your compiler.
For GCC g++, gnu.g++.help
hmm... i've posted a message, i'm waiting for a reply.
I think if you just turn on warnings (-Wall -W), it will tell you of
incorrect assignments. ( I've got a big project in progress, or I'd test
that out.)

Also:
-Wlong-long
Warn if long long type is used. This is default. To inhibit the warning
messages, use -Wno-long-long. Flags -Wlong-long and -Wno-long-long are taken
into account only when -pedantic flag is used.
>
--
Bob R
POVrookie

Sep 9 '07 #4

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

Similar topics

5
7432
by: Anton Pervukhin | last post by:
Hello! Imagine the situation you have a class that interprets the command line of the program you are executing. This class is written by third party and has a main function parse with arguments the same as main: ClassA::parse(char **argv, int argc) While trying to write test cases for this function I came across the warnings appeared when I tried to initialize 'char** argv' straightly
3
4463
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
1
1358
by: Josué Andrade Gomes | last post by:
Take this code: 1: int main() 2: { 3: unsigned short x = { 1, 2, 3, 4, 5 }; 4: unsigned short sum = 0; 5: 6: for (int i = 0; i < 5; ++i) { 7: sum += x; 8: } 9: }
2
6310
by: tim | last post by:
We've started to use a coding standards checker at work. The following code results in a warning: 38: CPlainText::CPlainText(const char *szPath,bool bTimeStamp,bool bSaveLast) 39: :CLog()//call base class constructor to initialize members 40: { 41: 42: m_bTimeStamp=bTimeStamp; 43: try
6
2675
by: Gilles Rochefort | last post by:
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, corners_x; std::transform(tmp_x,tmp_x+4,corners_x, bind2nd( multiplies<float>(),zfactor) );
2
2017
by: chauhan.alok | last post by:
hi I am a beginner to vc++. I am opening a file("c:\Mydoc.doc") and reading data from it and after that i just want to write data of 1.2 MB part from first file("c:\Mydoc.doc") to New file("c:\Newdoc1.doc") and remaining to other file("c;\Newdoc2.doc"). Code is Giving warning . Pls Help me How these waring can be removed? Code is:
11
3171
by: santosh | last post by:
Hello all, Conversion macros along the name of INT8_C, INT16_C etc, are defined in stdint.h to convert their argument into suitable representations for their corresponding types, i.e. int8_t, int16_t etc. My questions are: 1. Should the conversion macros be used even when assigning from a variable of the same type?
8
1625
by: Roman Mashak | last post by:
Hello, this piece of code, compiled with gcc-3.4.4 (-ansi -pedantic -W -Wall), doesn't generate any warnings regarding conversion from 'double' to 'unsigned int'. Is it normal or a bug of compiler? #include <stdlib.h> #include <stdio.h> void f1(unsigned int j)
8
14178
by: d major | last post by:
I was very puzzled about the conversion between float and long, I cann't understand why a long val can convert to a float, as the below codes show: typedef unsigned long u_long; float val = 3.14159; u_long nw_val = *((u_long *) &val); than the nw_val equal to 1078530000, I made such conversion: float d_val = *((float*)&nw_val);
9
26358
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" );
0
10600
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...
1
10354
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,...
0
10097
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
9175
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...
0
6867
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
5535
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...
1
4313
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
2
3835
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3002
bsmnconsultancy
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...

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.